23步:Docker 技能验证测试
23步:Docker 技能验证测试

第 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 应用,目录结构如下:

myapp/
├── app.py

└── 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 容器 box1box2app-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 文件参考 查,不要说“我不会”。





推荐阅读

docker volume

第一步:创建数据卷 docker volume create my-data 部分含义docker volumeDocker 数据卷管理命令create创建新数据卷my-data数据卷名称(可自定义)作用:在 /var/lib/docker/volumes/my-data/_data 创建一个目录,用来持久化数据。第二步:查看数据卷docker volume ls第三步:查看数据卷详情docker volume inspect my

89 0 赞2026-05-20分享技术分享

24步:常用操作

容器日志问题默认情况下,容器将 stdout/stderr 输出到 json-file 日志驱动,日志文件存在宿主机 /var/lib/docker/containers/&lt;container-id&gt;/&lt;container-id&gt;-json.log。如果不加限制,日志会无限增长,占满磁盘。1. 查看日志 docker logs &lt;容器名&gt; # 打印全部 docker logs --tail 100

58 0 赞2026-05-13分享技术专题

第 22 步:端口映射与网络隔离原理

关键理解:默认 bridge 网络:容器 IP 是 172.17.0.x,宿主机无法直接访问容器端口,必须用&nbsp;-p 8080:80&nbsp;做端口映射自定义 bridge 网络:容器间可以通信,但外界访问仍需端口映射为什么刚才的&nbsp;my-net&nbsp;容器不能从外部访问?因为它们没有&nbsp;-p&nbsp;参数,宿主机不知道如何转发流量第 22 步操作:测试端口映射 + 自定义网络1. 运行一个 Nginx

88 0 赞2026-05-12分享技术专题

第 21 步:Docker 网络基础原理

Docker 默认提供三种网络驱动:网络模式特点适用场景bridge默认网络,容器通过 NAT 访问外网,容器间需端口映射单机、简单场景host容器直接使用宿主机网络栈(无隔离)性能敏感、端口不冲突none无网络完全隔离、特殊安全场景overlay跨多台宿主机通信(Swarm/K8s)集群场景你之前用的默认 bridge 网络的问题:容器间不能直接用容器名通信(只能用 IP)需要手动&nbsp;--link(已废弃)自定义 bridge

81 0 赞2026-05-12分享技术专题

第 18 -19步:Docker Compose 原理

问题场景:一个 Web 应用通常需要:Nginx(前端)+ Python/Node(后端)+ MySQL/Redis(数据库)手动启动 3 个容器,还要配置网络、卷、依赖顺序 → 繁琐且容易出错解决方案:docker-compose.yml一个 YAML 文件定义所有容器的配置(镜像、端口、卷、网络、依赖)一条命令&nbsp;docker compose up -d&nbsp;启动全部一条命令&nbsp;docker compose d

95 0 赞2026-05-12分享技术专题

第 20 步:修改代码并热更新(使用 Compose)

在生产开发中,你修改代码后希望快速看到效果,有两种方式:重建镜像:docker compose up -d --build(会重新构建 web 镜像)挂载源码:开发时把代码目录挂载进去,改代码自动生效(无需重建)我们演示第二种(更接近真正的开发体验)。

84 0 赞2026-05-12分享技术专题

第 17 步:上传镜像到 Docker Hub(分享给别人)

前提:&nbsp;在&nbsp;https://hub.docker.com&nbsp;注册一个免费账号(如果还没有)原理:Docker Hub 是公共镜像仓库(类似 GitHub for 镜像)上传命令:docker push 用户名/镜像名:标签上传前需要先&nbsp;docker tag&nbsp;打上正确标签第 17 步操作(如果你有 Docker Hub 账号)1. 登录 Docker Hubbash docker logi

92 0 赞2026-05-12分享技术专题

第 16 步:更新镜像 - 修改代码后重新构建

场景:&nbsp;你修改了代码,需要更新容器。核心原则:&nbsp;不要手动进入容器改文件,而是重新构建镜像&nbsp;→ 删除旧容器 → 运行新容器。第 16 步操作1. 修改 app.py,改变返回内容bash cd ~/my-python-app cat &gt; app.py &lt;&lt; 'EOF' from http.server import HTTPServer, BaseHTTPRequestHandler i

90 0 赞2026-05-12分享技术专题

第 15 步:优化 Dockerfile(减小体积 + 利用缓存)

当前 Dockerfile 的问题:任何代码修改都会导致&nbsp;COPY . /app&nbsp;重新运行,但&nbsp;apt&nbsp;之类的不需要重新安装镜像包含 Python 编译工具链(slim&nbsp;已经较小,但还可以更小)优化技巧:问题优化方法体积大使用&nbsp;alpine&nbsp;基础镜像(约 50MB vs 122MB)依赖变化频繁先&nbsp;COPY requirements.txt&nbsp;安装依

87 0 赞2026-05-12分享技术专题

第 14 步:Dockerfile 基础原理

制作自己的镜像 是 Docker 最核心的技能,也是从“使用者”变成“创造者”的关键一步

59 0 赞2026-05-12分享技术专题
admin

发文31·粉丝0

目录

沉浸

点赞

分享

收藏

文章目录
广告