1、建立镜像git
在现有镜像的基础上启动一个容器
docker
[root@docker ~]# docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos centos7 7322fbe74aa5 6 days ago 172.2 MB centos centos6.6 8b44529354f3 9 weeks ago 202.6 MB [root@docker ~]# docker run -t -i centos:centos6.6 /bin/bash [root@06ba542f19c9 /]# hostname 06ba542f19c9 #这个是容器ID,惟一值
在容器中添加一个httpd服务
centos
[root@06ba542f19c9 /]# rpm -qa |grep httpd [root@06ba542f19c9 /]# yum install httpd -y [root@06ba542f19c9 /]# rpm -qa |grep httpd httpd-tools-2.2.15-39.el6.centos.x86_64 httpd-2.2.15-39.el6.centos.x86_64 [root@06ba542f19c9 /]# exit exit
在咱们使用exit退出后,容器已经被咱们改变了,使用docker commit来提交新的信息
bash
[root@docker ~]# docker commit -m "add httpd" -a "root" 06ba542f19c9 centos:centos6.6_v1 ae2ffc5dbfcc7944a624c55bc081b9514c83b420450d1ea9c67afc0b677bd686 #-m 来指定提交的说明信息 #-a 能够指定更新的用户信息 #06ba542f19c9 这是咱们上面启动容器后生成的ID #centos:centos6.6_v1 前面的一部分表示那个repo,后面的一部分表示TAG #查看提交后的结果 [root@docker ~]# docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE centos centos6.6_v1 ae2ffc5dbfcc 9 seconds ago 274.2 MB centos centos7 7322fbe74aa5 6 days ago 172.2 MB centos centos6.6 8b44529354f3 9 weeks ago 202.6 MB
咱们使用新建立的镜像来启动容器
ide
[root@docker ~]# docker run -t -i centos:centos6.6_v1 /bin/bash [root@5fc242e53248 /]# hostname 5fc242e53248 [root@5fc242e53248 /]# rpm -qa |grep httpd #在前面添加的httpd也存在 httpd-tools-2.2.15-39.el6.centos.x86_64 httpd-2.2.15-39.el6.centos.x86_64
在上面的例子中咱们是使用的docker commit来建立的镜像,下面咱们使用dockerfile来建立镜像ui
[root@docker git]# mkdir git [root@docker git]# cd git [root@docker git]# cat Dockerfile FROM centos:centos6.6 #已哪一个镜像做为基础 MAINTAINER lyao #维护者的信息 RUN yum install git -y #要安装的软件包 [root@docker git]# docker build -t "git" . #-t 添加注释 #查看结果 [root@docker git]# docker p_w_picpaths REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE #新添加的镜像 git latest 8aa77ab9e593 6 minutes ago 301.1 MB centos centos6.6_v1 ae2ffc5dbfcc 4 days ago 274.2 MB centos centos7 7322fbe74aa5 11 days ago 172.2 MB centos centos6.6 8b44529354f3 9 weeks ago 202.6 MB