云计算基础课程作业
注意: 如果您还没有安装 Docker,请先参考 CentOS 7 安装 Docker 的安装指南。
本文档记录了云计算基础课程中的几项Docker相关作业,包括Docker镜像创建、Nginx镜像构建、桥接网络配置以及卷操作。
作业11:创建Docker镜像
(1)建立项目目录
mkdir -p ~/docker-layers-test && cd ~/docker-layers-test(2)创建 app.py
cat > app.py <<'EOF'#!/usr/bin/pythonprint("Hello, World!")EOF(3)创建 Dockerfile
cat > Dockerfile <<'EOF'FROM ubuntu:16.04COPY ./ /appRUN apt-get -y update && apt-get install -y pythonCMD python /app/app.pyEOF(4)构建镜像
docker build -t img-layers-test .(5)查看分层信息
docker history img-layers-test输出5后截图上传即可

作业12:“自建 Docker 注册中心(Registry)
1 启动本地私有仓库(Registry 容器)
docker run -d -p 5000:5000 \ --restart=always \ --name myregistry \ -v /opt/data/registry:/var/lib/registry \ registry执行成功会返回一长串容器 ID。
2 确认仓库在线
curl http://127.0.0.1:5000/v2/_catalog应返回:
{"repositories":[]}(空列表说明仓库已启动,暂无镜像)
3 给本地镜像打”私有仓库标签”
docker tag hello-world 127.0.0.1:5000/hello-world:v14 推送到自建中心
docker push 127.0.0.1:5000/hello-world:v1首次推送会分层上传,结尾出现:
v1: digest: sha256:xxxx... size: xxxx5 验证仓库里已有镜像
curl http://127.0.0.1:5000/v2/_catalog现在返回:
{"repositories":["hello-world"]}截图交作业即可

作业13:构建Nginx镜像
下面给出”一条命令”就能跑起来的 Docker 版 nginx 镜像构建/运行 完整流程,你只需按顺序执行并截图即可提交。
1 准备目录和文件(可复用)
mkdir -p ~/docker-nginx && cd ~/docker-nginx2 编写 Dockerfile(基于官方 alpine 镜像,体积最小)
cat > Dockerfile <<'EOF'FROM nginx:alpineCOPY index.html /usr/share/nginx/html/EXPOSE 80CMD ["nginx", "-g", "daemon off;"]EOF3 准备一个简单的首页
cat > index.html <<'EOF'<!DOCTYPE html><html><head><title>Docker Nginx</title></head><body><h1>Hello from Docker Nginx!</h1></body></html>EOF4 构建镜像(注意最后有个点)
docker build -t my-nginx:1.0 .5 运行容器(映射到主机 8080)
docker run -d --name nginx-test -p 8080:80 my-nginx:1.06 验证 & 截图
- 浏览器访问
http://127.0.0.1:8080,出现 “Hello from Docker Nginx!” - 终端执行:
docker images | grep my-nginxdocker ps
或者使用curl 127.0.0.1:8080
- 把以上两个命令截图就行了

作业14:Docker桥接网络配置
作业说明:
根据视频,完成docker网络的配置 ,并将主要的配置过程截图提交。 -将容器连接到默认桥接网络
-创建用户自定义桥接网络并连接容器
下面给出完整、可截图的「默认桥接 + 自定义桥接」两步操作,全部命令行一次性复制即可。
0 环境清理(可选)
docker stop $(docker ps -aq) 2>/dev/nulldocker rm $(docker ps -aq) 2>/dev/null1 默认桥接网络(docker0)演示
① 运行两个无网络参数的容器,会自动接到 bridge(默认)
docker run -dit --name default1 alpine ashdocker run -dit --name default2 alpine ash② 查看网络与容器 IP
docker network lsdocker network inspect bridge | grep -A 20 "Containers"③ 连通性测试(default1)
docker attach default1ping www.baidu.comctrl+c暂停ping之后exit退出然后截图上传即可

2 用户自定义桥接网络
① 创建新网络 my-bridge
docker network create my-bridge② 运行两个容器并 显式指定网络
docker run -dit --name custom1 --network my-bridge alpine ashdocker run -dit --name custom2 --network my-bridge alpine ash③ 查看新网络详情
docker network inspect my-bridge | grep -A 30 "Containers"④ 连通性测试(custom1 → custom2,可用容器名直接 ping,这是自定义桥接的特性)
docker exec custom1 ash -c "ping -c 3 custom2"④ 之后截图直接交 
作业15:Docker卷操作
作业说明:
根据视频,完成docker卷的操作 ,并将主要的配置过程截图提交
下面把”作业15:docker卷操作”完整流程一次性贴给你,按顺序执行即可截图上交。
1 创建卷
docker volume create test-vol2 列出所有卷
docker volume ls预期能看到:
DRIVER VOLUME NAMElocal test-vol3 查看卷详细信息
docker volume inspect test-vol重点字段(截图用):
"Mounts": [{ "Type": "volume", "Name": "test-vol", "Source": "/var/lib/docker/volumes/test-vol/_data", "Destination": "/world", "Driver": "local", "RW": true}]4 启动容器并挂载该卷(验证读写)
docker run -dit --name vol-test \ -v test-vol:/world \ alpine ash5 容器内写数据验证卷生效
docker exec vol-test ash -c "echo hello-docker > /world/test.txt"docker exec vol-test ash -c "cat /world/test.txt"应输出 hello-docker
6 宿主机侧查看卷内容(证明数据已落盘)
sudo cat /var/lib/docker/volumes/test-vol/_data/test.txt同样出现 hello-docker 即成功。
截图

7 清理:先删容器,再删卷
docker stop vol-testdocker rm vol-testdocker volume rm test-vol8 最终验证卷已消失
docker volume ls列表里不再有 test-vol。
再截图

部分信息可能已经过时