Files
docker_practice/01_introduction/quickstart.md

66 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 快速上手 (5分钟)
本节将通过一个简单的 Web 应用例子带你快速体验 Docker 的核心流程构建镜像运行容器
## 1. 准备代码
创建一个名为 `hello-docker` 的文件夹并在其中创建一个 `index.html` 文件
```html
<h1>Hello, Docker!</h1>
```
## 2. 编写 Dockerfile
在同级目录下创建一个名为 `Dockerfile` (无后缀) 的文件
```dockerfile
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
```
## 3. 构建镜像
打开终端进入该目录执行构建命令
```bash
$ docker build -t my-hello-world .
```
* `docker build`: 构建命令
* `-t my-hello-world`: 给镜像起个名字标签
* `.`: 指定上下文路径为当前目录
## 4. 运行容器
使用刚才构建的镜像启动一个容器
```bash
$ docker run -d -p 8080:80 my-hello-world
```
* `docker run`: 运行命令
* `-d`: 后台运行
* `-p 8080:80`: 将宿主机的 8080 端口映射到容器的 80 端口
## 5. 访问测试
打开浏览器访问 [http://localhost:8080](http://localhost:8080),你应该能看到 "Hello, Docker!"。
## 6. 清理
停止并删除容器
```bash
# 查看正在运行的容器 ID
$ docker ps
# 停止容器
$ docker stop <CONTAINER_ID>
# 删除容器
$ docker rm <CONTAINER_ID>
```
恭喜你已经完成了第一次 Docker 实战接下来请阅读 [Docker 核心概念](../02_basic_concept/README.md) 做深入了解