第 1 题:基础命令(10分)
题目: 拉取 ubuntu:22.04 镜像,并运行一个交互式容器,进入 /tmp 目录,创建一个名为 test.txt 的文件,内容为 hello docker,然后退出容器。
请写出完整命令:
[root@localhost ~]# docker pull ubuntu:22.04
22.04: Pulling from library/ubuntu
f63eb04151bc: Pull complete
Digest: sha256:962f6cadeae0ea6284001009daa4cc9a8c37e75d1f5191cf0eb83fe565b63dd7
Status: Downloaded newer image for ubuntu:22.04
docker.io/library/ubuntu:22.04
[root@localhost ~]# docker run -it ubuntu:22.04
root@7998c348f6f7:/# cd /tmp
root@7998c348f6f7:/tmp# echo "hello docker" > test.txt
root@7998c348f6f7:/tmp# exit
exit
第 2 题:容器管理(15分)
题目: 基于 nginx:alpine 镜像,在后台运行一个名为 web 的容器,映射宿主机 8888 端口到容器 80 端口,然后查看容器日志。
请写出命令:
[root@localhost ~]# docker run -d --name web -p 8888:80 nginx:alpine
Unable to find image 'nginx:alpine' locally
alpine: Pulling from library/nginx
6a0ac1617861: Pull complete
82736a35d0e7: Pull complete
583599bb7d38: Pull complete
aee4e54b3865: Pull complete
781ff50d2644: Pull complete
453da7dbc73e: Pull complete
4a8b0b2a5b19: Pull complete
612c0c1df4c5: Pull complete
Digest: sha256:5616878291a2eed594aee8db4dade5878cf7edcb475e59193904b198d9b830de
Status: Downloaded newer image for nginx:alpine
b1f1d5b637f361037e87b4f29d1637c7e7f92e7d28fb77b49c3b058afe19e423
[root@localhost ~]# docker logs web
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/05/13 03:15:59 [notice] 1#1: using the "epoll" event method
2026/05/13 03:15:59 [notice] 1#1: nginx/1.29.8
2026/05/13 03:15:59 [notice] 1#1: built by gcc 15.2.0 (Alpine 15.2.0)
2026/05/13 03:15:59 [notice] 1#1: OS: Linux 3.10.0-1160.119.1.el7.x86_64
2026/05/13 03:15:59 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2026/05/13 03:15:59 [notice] 1#1: start worker processes
2026/05/13 03:15:59 [notice] 1#1: start worker process 30
2026/05/13 03:15:59 [notice] 1#1: start worker process 31
2026/05/13 03:15:59 [notice] 1#1: start worker process 32
2026/05/13 03:15:59 [notice] 1#1: start worker process 33
2026/05/13 03:15:59 [notice] 1#1: start worker process 34
2026/05/13 03:15:59 [notice] 1#1: start worker process 35
[root@localhost ~]#
第 3 题:进入容器内部(10分)
题目: 进入第 2 题中运行的 web 容器内部,修改 /usr/share/nginx/html/index.html 文件内容为 <h1>My Docker Test</h1>,然后退出容器并用 curl 验证。
请写出命令:
[root@localhost ~]# docker exec -it web sh
/ # echo '<h1>My Docker Test</h1>' > /usr/share/nginx/html/index.html
/ # exit
[root@localhost ~]# curl http://localhost:8888
<h1>My Docker Test</h1>
[root@localhost ~]#
第 4 题:数据持久化 - Bind Mount(15分)
题目: 在宿主机创建目录 /home/user/mysite,创建一个 index.html 文件内容为 <h1>Bind Mount Test</h1>。然后运行一个 nginx:alpine 容器,将宿主机目录挂载到容器的 /usr/share/nginx/html,端口映射 8889:80,验证访问。
请写出命令:
[root@localhost ~]# mkdir -p /home/user/mysite
[root@localhost ~]# echo '<h1>Bind Mount Test</h1>' > /home/user/mysite/index.html
[root@localhost ~]# docker run -d --name bind-nginx -p 8889:80 -v /home/user/mysite/:/usr/share/nginx/html nginx:alpine
9267e902cd6fbdec47f845353fd16ba8afcee278c74106b01f089010361323ca
[root@localhost ~]# curl http://localhost:8889
<h1>Bind Mount Test</h1>
[root@localhost ~]#
第 5 题:Dockerfile 编写(20分)
题目: 创建一个简单的 Python 应用,目录结构如下:
└── Dockerfile
app.py 内容:
print("Hello from Docker!")
编写 Dockerfile,要求:
基础镜像
python:3.9-alpine工作目录
/app复制
app.py到镜像容器启动时执行
python app.py
请写出 Dockerfile 内容:
FROM python:3.9-alpine
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]
第 6 题:Docker Compose(20分)
题目: 编写 docker-compose.yml,定义两个服务:
web:使用上题构建的镜像myapp:latest(或直接 build 当前目录),映射端口5000:5000(假设 app.py 改为 Flask 应用监听 5000)redis:使用redis:alpine镜像,持久化数据到redis-data卷
要求:web 依赖 redis,使用自定义网络。
请写出 docker-compose.yml:
version: '3.8' services:
web:build: . ports: - "5000:5000" depends_on: - redis networks: - mynet redis: image: redis:alpine volumes: - redis-data:/data networks: - mynet networks: mynet: volumes: redis-data:
第 7 题:网络(10分)
题目: 创建自定义网络 app-net,运行两个 alpine 容器 box1 和 box2 在 app-net 网络上,验证 box1 可以通过容器名 box2 ping 通。
请写出命令:
[root@localhost ~]# docker network create app-net 16a37d18ba212fb42f788013c07e710c6cc3b732fd544c5e697c6c711b2a070b [root@localhost ~]# docker run -d --name box1 --network app-net alpine sleep infinity Unable to find image 'alpine:latest' locally latest: Pulling from library/alpine 6a0ac1617861: Already exists Digest: sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11 Status: Downloaded newer image for alpine:latest b169616d9520c3e147302c51f3c7d23ab736baf752c683d3ef893a72c0138d3e [root@localhost ~]# docker run -d --name box2 --network app-net alpine sleep infinity c3b4cd5fe50e8aad29857e39a2d65f70987b9a663d14a3803c3139cb95774bb5 [root@localhost ~]# docker exec box1 ping -c 2 box2 PING box2 (172.18.0.3): 56 data bytes 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.106 ms 64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.131 ms --- box2 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.106/0.118/0.131 ms [root@localhost ~]#
运维场景最实用的几个 yml 修改技巧
你不需要从头写,但要知道怎么改:
| 场景 | 修改位置 | 示例 |
|---|---|---|
| 改映射端口 | ports: | - "8080:80" → - "9090:80" |
| 加环境变量 | environment: | - MYSQL_ROOT_PASSWORD=123 |
| 改持久化目录 | volumes: | - ./data:/var/lib/mysql |
| 加依赖服务 | depends_on: | - db |
| 改容器名 | container_name: | my-app |
| 限制资源 | deploy: / resources: | cpus: '0.5', memory: 256M |
遇到不认识的 yml 字段 → 去 Compose 文件参考 查,不要说“我不会”。
