> For the complete documentation index, see [llms.txt](https://moekiwisama-1.gitbook.io/minecraft-docker/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://moekiwisama-1.gitbook.io/minecraft-docker/ji-yu-alpine-zhi-zuo-image/bian-xie-dockerfile.md).

# 编写 Dockerfile

我们能不能把刚刚的那些操作制作成 Docker Image呢？当然可以，我们需要用到 Dockerfile

Dockerfile 是一个用来描述 Docker Image 制作过程的一个文件。\
根据上文的描述，启动 Minecraft 服务器需要那么几个步骤：

* 获取 azul/zulu-openjdk-alpine:16:0.0
* 创建服务器目录 /app
* 获取一份 paper-spigot
* 添加开服脚本，同意 Eula
* 启动开服脚本

实际上，编写 Dockerfile 也是那么几个步骤

### 准备步骤

首先我们创建一个目录来存放我们需要用到的东西

```bash
mkdir docker-playground
cd docker-playground
```

获取我们需要的东西

* 一份 paper
* 一份 eula
* 一份脚本

我们可以先获取 paper

```bash
mkdir temp
wget -O "temp/paper-1.17.jar"  https://papermc.io/api/v1/paper/1.17/latest/download
```

然后存放一份eula到scripts目录

```bash
mkdir scripts
echo "eula=false" > scripts/eula.txt
```

最后我们需要开服脚本

```bash
echo "sed -i 's/false/true/g' eula.txt" > scripts/start.sh
echo "java -Xmx2G -jar paper-1.17.jar" >> scripts/start.sh
```

当然，你也可以用你喜欢的编辑器来准备这些文件，不一定要用 echo\
我们还需要一个叫做 Dockerfile 的空文件，等会儿我们再编写它\
至此为止，我们已经有了我们所需要的的所有东西

```
├── Dockerfile
├── scripts
│   ├── eula.txt
│   └── start.sh
└── temp
    └── paper-1.17.jar
```

### 编写 Dockerfile

Dockerfile 的内容跟前文描述的几乎一致，主要用到的指令几乎完全可以按照字面意思理解

```bash
FROM azul/zulu-openjdk-alpine:16.0.0 # 基于azul/zulu-openjdk-alpine:16:0.0
RUN mkdir /app # 创建服务器目录 /app
ADD ./temp/paper-1.17.jar /app # 复制本地的paper到image
ADD ./scripts/start.sh /app # 添加开服脚本
ADD ./scripts/eula.txt /app # 添加 eula
WORKDIR /app # 设置工作目录
ENTRYPOINT ["sh", "/app/start.sh"] # 执行开服脚本
```

这样，Dockerfile 就制作完了，我们可以开始构建镜像了

```bash
docker build -t localhost/my-minecraft-server-image:0.0.1 .
```

这样就可以制作一个localhost/my-minecraft-server-image，版本号为0.0.1的镜像了

### 试一试刚刚制作的 Image

我们可以将服务运行在25567端口

```bash
docker run -it -p 25567:25565 localhost/my-minecraft-server-image:0.0.1
```

![](/files/-McyAOuroGPdkzYcTpA-)

当然，Minecraft 也是可以连接上去的


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://moekiwisama-1.gitbook.io/minecraft-docker/ji-yu-alpine-zhi-zuo-image/bian-xie-dockerfile.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
