一、docker镜像解析
Docker镜像含有启动容器所需要的文件系统及其内容,因此,
其用于创建并启动docker容器
采用分层构建机制,最底层为bootfs,其之为rootfs
bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完
成后会被卸载以节约内存资源;
rootfs:位于bootfs之上,表现为docker容器的根文件系统;
传统模式中,系统启动之时,内核挂载rootfs时会首先将其挂载为“只读”模式,
完整性自检完成后将其重新挂载为读写模式;
docker中,rootfs由内核挂载为“只读”模式,而后通过“联合挂载 ”技术额外挂载一个“可写”层;
二、镜像打标签
docker tag
语法:
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
示例:
基于ID打标
[root@centos7 ~]# docker tag 4085594806b3 chenwenj/httpd:v2
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd v1 4085594806b3 3 hours ago 1.22MB
chenwenj/httpd v2 4085594806b3 3 hours ago 1.22MB
busybox latest be5888e67be6 3 weeks ago 1.22MB
nginx 1.14-alpine 8a2fb25a19f5 13 months ago 16MB
基于名称和标签打标
[root@centos7 ~]# docker tag chenwenj/httpd:v3 chenwenj/httpd:latest
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd latest 74fa40de95fa 3 hours ago 1.22MB
chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
为私有Registry打标
示例:docker tag 9133dae37bd8 myregistry:5000/busybox/httpd:v0.1
[root@centos7 ~]# docker tag chenwenj/httpd:v3 registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd:v3
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
三、基于容器制作镜像
1、启动容器,执行需要的修改操作
[root@centos7 ~]# docker run --name busy1 -it busybox
/ # mkdir -p /data/html
/ # vi /data/html/index.html
/ # cat /data/html/index.html
<h1>Busybox test page</h1>
2、提交镜像
注意:此时容器不能关闭
[root@centos7 ~]# docker commit -a "Chenwj<cwj@cwj.com>" -p busy1 chenwenj/httpd:v2
sha256:64f652f7cc635455df00c79dce01e341ca1dde737866b0c8d546b334980538bf
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd v2 64f652f7cc63 24 seconds ago 1.22MB
新制作的镜像运行为容器
[root@centos7 ~]# docker run --name web2 -it chenwenj/httpd:v2
/ # ls
bin data dev etc home proc root sys tmp usr var
/ # cat /data/html/index.html
<h1>Busybox test page</h1>
/ #
在提交镜像的同时,也可以修改默认运行的命令
[root@centos7 ~]# docker commit -a "Chenwj<cwj@cwj.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p web2 chenwenj/httpd:v3
sha256:b2b9f5beb07d23126ae9dc36a3eb2fc925bfcc101de4a32232bd33e39d210acf
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd v3 17514a5c90d6 21 seconds ago 1.22MB
chenwenj/httpd v2 64f652f7cc63 12 minutes ago 1.22MB
busybox latest be5888e67be6 3 weeks ago 1.22MB
新制作的镜像运行为容器
[root@centos7 ~]# docker run --name web3 chenwenj/httpd:v3
查看运行的容器状况,可以看出新制作的容器已经修改了默认运行命令。
[root@centos7 ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
38b2335b6b9b chenwenj/httpd:v3 "/bin/httpd -f -h /d…" 29 seconds ago Up 28 seconds web3
5e89153e97de chenwenj/httpd:v2 "sh" 2 hours ago Up 2 hours web2
6ce6ede2319f busybox "sh" 7 hours ago Up 2 hours busy1
3、推送镜像
推送到docker hub
登录
[root@centos7 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: chenwenj
Password:
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
docker push
[root@centos7 ~]# docker push chenwenj/httpd:v2
The push refers to repository [docker.io/chenwenj/httpd]
9b564183233a: Pushed
5b0d2d635df8: Mounted from library/busybox
v1: digest: sha256:ff7c926495a6f5efd8e2cfa83b2f2a1ffc6427484552b116682dd7732519f33c size: 734
推送到阿里云的镜像仓库
登录阿里云Docker Registry
[root@centos7 ~]# sudo docker login --username=猪中梁朝伟 registry.cn-hangzhou.aliyuncs.com
Password:
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
给镜像重新打上标签。由于不是默认的docker hub仓库,因此这里这里打标时要指定服务器地址。
示例:$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd:[镜像版本号]
[root@centos7 ~]# docker tag chenwenj/httpd:v3 registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd:v3
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
推送到阿里云Docker Registry
[root@centos7 ~]# docker push registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd:v3
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd]
22998cf8f3fd: Pushed
5b0d2d635df8: Pushed
v3: digest: sha256:ba743748299ed0574fbeea4141e3f90410340924381e4cc98f5f7bc28a33b877 size: 734
四、删除镜像
现在本地一共有5个镜像,有三个镜像是基于同一个镜像ID(一个镜像关联多个tag),此时如果删除其中一个镜像,其实并不是删除镜像本身,只是把其标签去除。
[root@centos7 ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
chenwenj/httpd latest 74fa40de95fa 3 hours ago 1.22MB
chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
registry.cn-hangzhou.aliyuncs.com/chenwenj/httpd v3 74fa40de95fa 3 hours ago 1.22MB
busybox latest be5888e67be6 3 weeks ago 1.22MB
nginx 1.14-alpine 8a2fb25a19f5 13 months ago 16MB
此时镜像并不会被删除,只是Untagged,只有最后一个标签被删除时,镜像才会被删除。
[root@centos7 ~]# docker image rm chenwenj/httpd:latest
Untagged: chenwenj/httpd:latest
镜像正在被某个容器引用时是无法删除的
[root@centos7 ~]# docker image rm busybox
Error response from daemon: conflict: unable to remove repository reference "busybox" (must force) - container 7adf8cf4e55b is using its referenced image be5888e67be6