docker 手动构建新镜像的方法

2020-06-17 06:52:27易采站长站整理


[root@bab3b6991467 ~]# exit
exit

查看此时容器的状态:


[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bab3b6991467 centos "/bin/bash" 37 minutes ago Exited (0) 21 seconds ago web

利用docker diff查看该容器进行了哪些修改,由于输出太多,这里不给予显示了

利用docker commit将web容器进行加层成一个新镜像:


[root@docker ~]# docker commit --help
Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes

-m, --message string Commit message
-a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>")

现在开始commit:


[root@docker ~]# docker commit -m "compile nginx on centos" web wadeson/centos_nginx:v1
sha256:210a202d37b8d2c31155c29adf0c7c0b49cfab7ff38234109919de7f4e76d1de

查看本地镜像:


[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wadeson/centos_nginx v1 210a202d37b8 33 seconds ago 464MB
nginx latest c59f17fe53b0 4 days ago 108MB
ubuntu latest 747cb2d60bbe 3 weeks ago 122MB
centos latest 196e0ce0c9fb 6 weeks ago 197MB

可以看见刚刚docker commit的新镜像了,现在由此镜像进行启动一个container提供nginx服务:


[root@docker ~]# docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"
c12669357e2b09a05a396ac480a04dd1956303b784f894b615d4edb889a737ab

然后查看container:


[root@docker ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c12669357e2b wadeson/centos_nginx:v1 "/usr/local/nginx/..." 41 seconds ago Up 40 seconds 0.0.0.0:80->80/tcp thirsty_murdock

可以看见nginx服务已经开启了,于是进行访问:

 于是整个手动构建就成功了

针对上面的一些命令做下解释:


docker run -d -p80:80 wadeson/centos_nginx:v1 /usr/local/nginx/sbin/nginx -g "daemon off;"

后面运行的命令都是旨在container的命令,由于没有进行环境变量设置,所以全路径,而nginx -g这个参数是指可以在外面添加指令到nginx的配置文件中,daemon off是指nginx服务不运行在后端而是在前台运行(container中的服务必须运行在前台)