# 1.1 快速上手

> **版本说明**：本节示例基于 Docker v29.x 编写。示例中使用的 `nginx:alpine` 镜像标签为演示用途，请查阅 [Docker Hub - nginx](https://hub.docker.com/_/nginx) 确认最新可用版本。

本节将通过一个简单的 Web 应用例子，带你快速体验 Docker 的核心流程：构建镜像、运行容器。

## 为什么选择 Nginx + HTML 作为入门例子？

在学习 Docker 之前，我们先来理解为什么这个例子是最适合初学者的。Docker 的核心价值在于**一致性交付**——无论你在本地、云端还是他人的机器上运行容器，应用的行为都是完全一致的。这个 Nginx + 静态 HTML 的例子之所以被广泛采用，是因为它展现了 Docker 工作流的三个核心阶段：

1. **镜像定义（Image Layer）**：通过 Dockerfile 描述如何把应用打包成一个自包含的单元
2. **镜像构建（Build）**：执行 `docker build`，Docker 根据 Dockerfile 逐层构建镜像
3. **容器运行（Runtime）**：通过 `docker run` 启动容器实例，应用真正开始提供服务

Nginx 是一个轻量级、使用广泛的 Web 服务器，学习完这个例子后，你可以轻松扩展到部署 Node.js、Python、Go 等任何语言的应用。

## 1.1.1 准备代码

创建一个名为 `hello-docker` 的文件夹，并在其中创建一个 `index.html` 文件：

```html
<h1>Hello, Docker!</h1>
```

## 1.1.2 编写 Dockerfile

在同级目录下创建一个名为 `Dockerfile` (无后缀) 的文件：

```dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
```

## 1.1.3 构建镜像

打开终端，进入该目录，执行构建命令：

```bash
$ docker build -t my-hello-world .
```

* `docker build`：构建命令
* `-t my-hello-world`：给镜像起个名字 (标签)
* `.`：指定上下文路径为当前目录

## 1.1.4 运行容器

使用刚才构建的镜像启动一个容器：

```bash
$ docker run -d -p 8080:80 my-hello-world
```

* `docker run`：运行命令
* `-d`：后台运行
* `-p 8080:80`：将宿主机的 8080 端口映射到容器的 80 端口

## 1.1.5 访问测试

打开浏览器访问 <http://localhost:8080>，你应该能看到 “Hello, Docker!”。

## 1.1.6 清理

停止并删除容器：

```bash
# 查看正在运行的容器 ID

$ docker ps

# 停止容器

$ docker stop <CONTAINER_ID>

# 删除容器

$ docker rm <CONTAINER_ID>
```

恭喜！你已经完成了第一次 Docker 实战。接下来请阅读 [Docker 核心概念](/docker_practice/di-yi-bu-fen-ru-men-pian/02_basic_concept.md)做深入了解。


---

# Agent Instructions: 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://yeasy.gitbook.io/docker_practice/di-yi-bu-fen-ru-men-pian/01_introduction/1.1_quickstart.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.
