Docker2 docker commit方法镜像制作
一、前期准备
1、下载一个centos镜像,进入容器,安装wget
docker pull centos
docker run -it centos bash
[root@web1 ~]# docker run -it centos bash #进入容器 [root@4f1f1ca319f2 /]# [root@4f1f1ca319f2 /]# [root@4f1f1ca319f2 /]# cd [root@4f1f1ca319f2 ~]# ls anaconda-ks.cfg [root@4f1f1ca319f2 ~]# cd /etc/yum.repos.d/ #配置yum源 [root@4f1f1ca319f2 yum.repos.d]# ll total 32 -rw-r--r-- 1 root root 1664 Nov 23 2018 CentOS-Base.repo -rw-r--r-- 1 root root 1309 Nov 23 2018 CentOS-CR.repo -rw-r--r-- 1 root root 649 Nov 23 2018 CentOS-Debuginfo.repo -rw-r--r-- 1 root root 630 Nov 23 2018 CentOS-Media.repo -rw-r--r-- 1 root root 1331 Nov 23 2018 CentOS-Sources.repo -rw-r--r-- 1 root root 5701 Nov 23 2018 CentOS-Vault.repo -rw-r--r-- 1 root root 314 Nov 23 2018 CentOS-fasttrack.repo [root@4f1f1ca319f2 yum.repos.d]# yum install wget -y #安装wget,默认没有wget安装
2、安装软件源
mkdir backup mv c* /etc/yum.repo.d/bakcup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget https://mirrors.aliyun.com/centos/7.6.1810/extras/x86_64/Packages/epel-release-7-11.noarch.rpm
3、安装软件,比如nginx
yum install nginx -y
二、制作镜像
说明,下面制作镜像先用之前的nginx镜像做的副本,下面才是centos镜像
1、使用docker commit 来提交容器副本
-m 提交时的描述文字
dock commit -m 容器 镜像名 docker commit -m 'add nginx images' mynginx zxg/my_nginx docker commit -m 'add nginx images' mynginx zxg/my_nginx:v1 docker images docker ps -a
[root@web1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f1f1ca319f2 centos "bash" About an hour ago Up About an hour musing_lichterman [root@web1 ~]# docker commit --help Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] Create a new image from a container's changes Options: -a, --author string Author (e.g., "John Hannibal Smith <hannibal@a-team.com>") -c, --change list Apply Dockerfile instruction to the created image (default []) --help Print usage -m, --message string Commit message -p, --pause Pause container during commit (default true) ###制作镜像 [root@web1 ~]# docker commit -m 'add nginx images' mynginx zxg/my_nginx sha256:f07837869dfc057273c8fd4dad1ddd7f391abb7c75dd4ecf8f84e33e84bc6b0d [root@web1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zxg/my_nginx latest f07837869dfc 8 seconds ago 126 MB docker.io/nginx latest e445ab08b2be 6 days ago 126 MB docker.io/alpine latest b7b28af77ffe 2 weeks ago 5.58 MB docker.io/centos latest 9f38484d220f 4 months ago 202 MB ###加版本号 [root@web1 ~]# docker commit -m 'add nginx images' mynginx zxg/my_nginx:v1 sha256:b164f4c07c6407420327a48dc0259c52f31cc3ae13ca0b292b8ac5cad1b0052b [root@web1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zxg/my_nginx v1 b164f4c07c64 5 seconds ago 126 MB zxg/my_nginx latest f07837869dfc 46 seconds ago 126 MB docker.io/nginx latest e445ab08b2be 6 days ago 126 MB docker.io/alpine latest b7b28af77ffe 2 weeks ago 5.58 MB docker.io/centos latest 9f38484d220f 4 months ago 202 MB [root@web1 ~]# docker run -d --name my_nginx zxg/my_nginx nginx b326027dcf4275190f7766a65510d73a5f6b15e60875254e5f729f108f809a4e [root@web1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4f1f1ca319f2 centos "bash" About an hour ago Up About an hour musing_lichterman ###查看进程 [root@web1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b326027dcf42 zxg/my_nginx "nginx" 38 seconds ago Exited (0) 35 seconds ago my_nginx 4f1f1ca319f2 centos "bash" About an hour ago Up About an hour musing_lichterman 64b4e32991c7 nginx "nginx -g 'daemon ..." 3 days ago Exited (0) 3 days ago mynginx1 f8b2da5476ed nginx "nginx -g 'daemon ..." 3 days ago Exited (0) 3 days ago mynginx 6dbdfaf6da3a alpine "sh" 4 days ago Created distracted_shannon aee506fe7b5a alpine "sh" 4 days ago Created infallible_haibt 70620c73b9a0 alpine "sh" 4 days ago Created gallant_volhard 7655cbf87bb0 alpine "sh" 4 days ago Created agitated_brahmagupta 33fb949372e8 fce289e99eb9 "/hello" 4 days ago Created elastic_dijkstra 9de47616aea4 fce289e99eb9 "/hello" 4 days ago Created confident_fermi
centos镜像制作过程如下:
[root@web1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2940076be5f7 centos "bash" About an hour ago Up About an hour suspicious_khorana [root@web1 ~]# docker commit -m 'add centos-nginx' 2940076be5f7 zxg/centos_nginx:v1 sha256:7c6604cacec13e7caf5b4f99c514009c43df88cf59c69b2ff5a608dfc8216eae [root@web1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zxg/centos_nginx v1 7c6604cacec1 13 seconds ago 698 MB docker.io/atlassian/jira-software latest c4b90dede4f3 2 days ago 624 MB zxg/my_nginx v1 b164f4c07c64 3 weeks ago 126 MB zxg/my_nginx latest f07837869dfc 3 weeks ago 126 MB docker.io/nginx latest e445ab08b2be 4 weeks ago 126 MB docker.io/alpine latest b7b28af77ffe 5 weeks ago 5.58 MB docker.io/centos latest 9f38484d220f 5 months ago 202 MB [root@web1 ~]#
[root@web1 ~]# docker run -it --name centos-nginx zxg/centos_nginx:v1 /bin/bash [root@e93232ab8601 /]# [root@e93232ab8601 /]# rpm -qa nginx nginx-1.12.2-3.el7.x86_64 [root@e93232ab8601 /]#
另外开一个窗口
[root@web1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e93232ab8601 zxg/centos_nginx:v1 "/bin/bash" 57 seconds ago Up 55 seconds centos-nginx
2、制作好的镜像可以上传官网(需要注册账号)
docker login https:hub.docker.com docker tag b326027dcf42 docker.io/myname/mynginx #打标签 docker push docker.io/myname/mynginx
转载请注明出处:https://www.cnblogs.com/zhangxingeng/p/11326365.html