Skip to content

Latest commit

 

History

History
33 lines (30 loc) · 1.75 KB

模块三作业说明.md

File metadata and controls

33 lines (30 loc) · 1.75 KB

题目:用docker制作容器镜像

  1. 构建本地镜像
  2. 编写 Dockerfile 将练习 2.2 编写的 httpserver 容器化
  3. 将镜像推送至 docker 官方镜像仓库
  4. 通过 docker 命令本地启动 httpserver
  5. 通过 nsenter 进入容器查看 IP 配置
  6. 作业需编写并提交 Dockerfile 及源代码。

操作步骤

  1. 创建.dockerignore文件,忽略掉不必要的文件,请见源码根目录
  2. 创建Dockerfile文件,使用多阶段构建,第二阶段base image使用alpine,否则容器无法运行,请见源码根目录
  3. 构建镜像: docker build . -t kuopenx/httpserver:0.0.1
  4. 推送镜像: docker push kuopenx/httpserver:0.0.1, 镜像仓库地址:https://hub.docker.com/r/kuopenx/httpserver
  5. 本地启动镜像:docker run -d kuopenx/httpserver:0.0.1,以下为运行状态信息:
docker ps
CONTAINER ID   IMAGE                      COMMAND          CREATED         STATUS        PORTS     NAMES
81a21bb70410   kuopenx/httpserver:0.0.1   "./httpserver"   3 seconds ago   Up 1 second   80/tcp    happy_galileo
  1. 获取该容器进程的pid:PID=$(docker inspect -f "{{ .State.Pid }}" happy_galileo)
  2. 查看容器的IP配置:nsenter -t $PID -n ip a,以下为实际操作时的IP配置:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
62: eth0@if63: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever