制作Docker镜像有两种方式:第一种、docker commit,保存容器(Container)的当前状态到镜像后,然后生成对应的image;第二种、docker build,使用Dockerfile文件自动化制作镜像。
(1).docker commit
启动一个实例,安装apache
[root@youxi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 3 weeks ago 202MB
hub.c.163.com/library/tomcat latest 72d2be374029 2 years ago 292MB
[root@youxi1 ~]# docker run -it centos:latest /bin/bash
[root@985ef7e0c4ca /]# yum -y install httpd
[root@985ef7e0c4ca /]# exit
exit
根据容器当前状态制作一个镜像,语法docker commit <容器ID> [仓库]:[标签]
[root@youxi1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985ef7e0c4ca centos:latest "/bin/bash" 4 minutes ago Exited (0) 2 minutes ago sharp_kare
9a81af9b4134 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago pensive_dijkstra
3ecc9bafd429 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago sleepy_wilbur
[root@youxi1 ~]# docker commit 985ef7e0c4ca centos:httpd
sha256:bdd371e6f1cf035bd501c09b28a2b2827900973a0defa9e35eadcd2f2f55e5e4
[root@youxi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd bdd371e6f1cf 14 seconds ago 346MB
centos latest 67fa590cfc1c 3 weeks ago 202MB
hub.c.163.com/library/tomcat latest 72d2be374029 2 years ago 292MB
启动新创建的镜像,查看是否存在httpd服务
[root@youxi1 ~]# docker run -it centos:httpd /bin/bash
[root@34ac01c03fe3 /]# rpm -qa httpd //可以看到是存在的
httpd-2.4.6-89.el7.centos.1.x86_64
(2).docker build
使用docker build创建镜像时,需要使用Dockerfile文件自动化制作镜像。Dockerfile类似源码编译./configure后产生的Makefile。
创建工作目录,制作Dockerfile
[root@youxi1 ~]# mkdir /docker-build
[root@youxi1 ~]# vim /docker-build/Dockerfile
FROM centos:latest #以哪个镜像为基础
MAINTAINER <youxi@163.com> #镜像创建者
RUN yum -y install httpd #运行安装httpd命令
ADD start.sh /usr/local/bin/start.sh #将本地文件拷贝到镜像中,权限为0755(?),uid和gid为0
ADD index.html /var/www/html/index.html
CMD /usr/local/bin/start.sh #实例启动后执行的命令,一般跟需要开机启动的服务或脚本
创建start.sh和index.html
[root@youxi1 ~]# echo "/usr/sbin/httpd -DFOREGROUND" > /docker-build/start.sh
[root@youxi1 ~]# chmod +x /docker-build/start.sh
[root@youxi1 ~]# echo "docker image build test" > /docker-build/index.html
删除已有的centos:httpd
[root@youxi1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
34ac01c03fe3 centos:httpd "/bin/bash" About an hour ago Exited (0) 43 minutes ago gifted_cerf
985ef7e0c4ca centos:latest "/bin/bash" About an hour ago Exited (0) About an hour ago sharp_kare
9a81af9b4134 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago pensive_dijkstra
3ecc9bafd429 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago sleepy_wilbur
[root@youxi1 ~]# docker stop 34ac01c03fe3
34ac01c03fe3
[root@youxi1 ~]# docker rm 34ac01c03fe3
34ac01c03fe3
[root@youxi1 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
985ef7e0c4ca centos:latest "/bin/bash" About an hour ago Exited (0) About an hour ago sharp_kare
9a81af9b4134 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago pensive_dijkstra
3ecc9bafd429 centos:latest "/bin/bash" 2 days ago Exited (0) 2 days ago
[root@youxi1 ~]# docker rmi centos:httpd
Untagged: centos:httpd
Deleted: sha256:bdd371e6f1cf035bd501c09b28a2b2827900973a0defa9e35eadcd2f2f55e5e4
Deleted: sha256:e5f250b390d99269c53a6150c776f5af4e74b965f4c86977fea7582b9e624870
[root@youxi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 67fa590cfc1c 3 weeks ago 202MB
hub.c.163.com/library/tomcat latest 72d2be374029 2 years ago 292MB
使用build创建新的镜像,语法docker build -t [仓库名]:[标签] [Dockerfile文件路径]
[root@youxi1 ~]# docker build -t centos:httpd /docker-build/
[root@youxi1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos httpd 565f0e4e1ef3 About a minute ago 346MB
centos latest 67fa590cfc1c 3 weeks ago 202MB
hub.c.163.com/library/tomcat latest 72d2be374029 2 years ago 292MB
(3).Docker镜像发布
1)发布到本地
语法:docker save -o [tar包名] [仓库名]:[标签]
[root@youxi1 ~]# docker save -o docker.id-centos-httpd-image.tar centos:httpd
[root@youxi1 ~]# ll -h
总用量 346M
-rw-------. 1 root root 1.6K 5月 14 09:20 anaconda-ks.cfg
-rw------- 1 root root 339M 9月 14 15:26 docker.id-centos-httpd-image.tar
2)发布到网上
一般先到https://hub.docker.com/(DockerHub)上注册一个账号,并创建一个存储库
使用命令登录
[root@youxi1 ~]# docker login -u [用户名] -p [密码]
WARNING! Using --password via the CLI is insecure. Use --password-stdin. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
上传镜像。注意:上传前需要修改仓库名,否则上传有问题。
[root@youxi1 ~]# docker tag centos:httpd ghhh4512/centos:httpd //这里的ghhh4512是我的Docker用户名,centos是建立的存储库
[root@youxi1 ~]# docker push ghhh4512/centos:httpd
The push refers to repository [docker.io/ghhh4512/centos]
7873e44b6044: Pushed
0e86f20c1056: Pushed
b3e0fc83922e: Pushed
877b494a9f30: Pushed
httpd: digest: sha256:464ffb58cbc2334a936bea7081db41b257afb5b21dfcce6dd27863dc9b777855 size: 1155