Docker镜像
Docker镜像含有启动容器所需要的文件系统及其内容,因此,其用于创建并启动docker容器
- 采用分层构建机制,最底层为bootfs,其之为rootfs
- bootfs:用于系统引导的文件系统,包括bootloader和kernel,容器启动完成后会被卸载以节约内存资源
- rootfs:位于bootfs之上,表现为docker容器的跟文件系统;
①传统模式中,系统启动时,内核挂载rootfs时会首先将其挂载为"只读"模式,完整性自检完成后将其重新挂载为读写模式
②docker中,rootfs由内核挂载为"只读"模式,而后通过“联合挂载”技术额外挂载一个“可写”层
基于容器制作镜像
一、分布构建
1、基于busybox镜像,启动一个名为b1的容器
[root@node1 ~]# docker run --name b1 -it busybox / # ls bin dev etc home proc root sys tmp usr var / # mkdir -p /data/html / # / # vi /data/html/index.html / #
2、以b1容器制作镜像,此时仓库、标签都为空
[root@node1 ~]# docker commit -p b1 sha256:f586aa9b0f116e2c2ed625f94057923d9f2f1173f851ab7da81131bdacb8a9c0 [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> f586aa9b0f11 24 seconds ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB
3、为其打上标签
[root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> f586aa9b0f11 2 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]# docker tag f586aa9b0f11 mustark/httpd:v0.1-1 [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.1-1 f586aa9b0f11 4 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB
同一镜像打多个标签
[root@node1 ~]# docker tag mustark/httpd:v0.1-1 mustark/httpd:latest [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd latest f586aa9b0f11 7 minutes ago 1.2MB mustark/httpd v0.1-1 f586aa9b0f11 7 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB
此时删除其中一个镜像,不会产生影响。类似“硬链接”
[root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd latest 0d3bbdaa506f About a minute ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f About a minute ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]# [root@node1 ~]# docker image rm mustark/httpd:latest Untagged: mustark/httpd:latest [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.1-1 0d3bbdaa506f 2 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]#
二、一步构建
修改默认CMD,打上标签、仓库
[root@node1 ~]# docker commit -a "Mustark <1005403299@qq.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p b1 mustark/httpd:v0.2 sha256:f36ec7798fe63e4e13eb339e74cf4ecd0b15bfd126bf142b302265907daec619 [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 4 seconds ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f 9 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB
基于mustark/httpd:v0.2这个镜像启动一个容器
[root@node1 ~]# docker run --name t2 mustark/httpd:v0.2 [root@node1 ~]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5a94ded28cfe mustark/httpd:v0.2 "/bin/httpd -f -h /d…" 19 seconds ago Up 18 seconds t2 fcba9b952998 busybox "sh" 30 minutes ago Up 30 minutes b1 125c76bcd270 redis:4-alpine "docker-entrypoint.s…" 6 hours ago Up 6 hours 6379/tcp kvstor1 fac034deb8a7 nginx:1.14-alpine "nginx -g 'daemon of…" 6 hours ago Up 6 hours 80/tcp web01
进入容器获取地址
[root@node1 ~]# docker inspect t2
访问172.17.0.5
[root@node1 ~]# curl 172.17.0.5 <h1>Busybox httpd server.</h1>
保存制作的镜像
一、docker hub
创建一个Repositories
推镜像前先登录,默认login登录为docker hub
[root@node1 ~]# docker login -u mustark 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
进行推送镜像
[root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 18 minutes ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f 28 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]# docker push mustark/httpd The push refers to repository [docker.io/mustark/httpd] 8c5f5b80130c: Pushed 683f499823be: Mounted from library/busybox v0.1-1: digest: sha256:289223ea342ab3c5142b122d915684da6f265b62d0b0c48ad91be4cd450daf47 size: 734 8c5f5b80130c: Layer already exists 683f499823be: Layer already exists v0.2: digest: sha256:cde5b6de7d22af4be7f824b9fc08e365407a2b50a41067b919f77e5e53144fa9 size: 734
此时docker hub下也会同步有这些镜像
二、阿里云
创建命名空间
创建镜像仓库
第三方镜像仓库都会有自己的镜像管理规则,例如登录、镜像命名
修改镜像名字
[root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 18 minutes ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f 28 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]# docker tag mustark/httpd:v0.2 registry.cn-shenzhen.aliyuncs.com/mustark/httpd:v0.2 [root@node1 ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 36 minutes ago 1.2MB registry.cn-shenzhen.aliyuncs.com/mustark/httpd v0.2 f36ec7798fe6 36 minutes ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f 45 minutes ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB
登录(登录密码是Registry密码,不是阿里云账号登录的密码)
[root@node1 ~]# docker login --username=chen_虫儿飞 registry.cn-shenzhen.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 Login Succeeded
推送镜像到仓库
[root@node1 ~]# docker push registry.cn-shenzhen.aliyuncs.com/mustark/httpd The push refers to repository [registry.cn-shenzhen.aliyuncs.com/mustark/httpd] 8c5f5b80130c: Pushed 683f499823be: Pushed v0.2: digest: sha256:cde5b6de7d22af4be7f824b9fc08e365407a2b50a41067b919f77e5e53144fa9 size: 734
镜像导出
[root@node1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 About an hour ago 1.2MB registry.cn-shenzhen.aliyuncs.com/mustark/httpd v0.2 f36ec7798fe6 About an hour ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f About an hour ago 1.2MB busybox latest 3a093384ac30 3 weeks ago 1.2MB redis 4-alpine 37abb58bfd68 4 weeks ago 30MB nginx 1.14-alpine c5b6f731fbc0 4 weeks ago 17.7MB quay.io/coreos/flannel v0.10.0-amd64 f0fad859c909 12 months ago 44.6MB [root@node1 ~]# docker save -o myimages.gz mustark/httpd:v0.2 mustark/httpd:v0.1-1 [root@node1 ~]# ls anaconda-ks.cfg git_test myimages.gz
镜像导入,将打包好的镜像发送到另外一个节点node2
[root@node1 ~]# scp myimages.gz root@10.0.0.62:/ root@10.0.0.62's password: myimages.gz
在node2节点上导入镜像
[root@node2 ~]# docker load -i myimages.gz 683f499823be: Loading layer [==================================================>] 1.416MB/1.416MB 8c5f5b80130c: Loading layer [==================================================>] 4.608kB/4.608kB Loaded image: mustark/httpd:v0.2 Loaded image: mustark/httpd:v0.1-1 [root@node2 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mustark/httpd v0.2 f36ec7798fe6 About an hour ago 1.2MB mustark/httpd v0.1-1 0d3bbdaa506f 2 hours ago 1.2MB