您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
03-Docker镜像管理基础
发布时间:2020-08-30 21:57:56编辑:雪饮阅读()
每个docker容器都是一个rootfs
[root@localhost ~]# docker exec -it kvstorl /bin/sh
/data # ls /
bin data dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/data # exit
[root@localhost ~]# docker exec -it web1 /bin/sh
/ # ls /
bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var
/ # exit
注意:用exec的前提是该容器已经启动
在第三方docker镜像站下载镜像
比如我在该站点找到一个docker镜像
那么下载如
[root@localhost ~]# docker pull quay.io/coreos/flannel:v0.10.0-amd64
Trying to pull repository quay.io/coreos/flannel ...
v0.10.0-amd64: Pulling from quay.io/coreos/flannel
ff3a5c916c92: Pull complete
8a8433d1d437: Pulling fs layer
306dc0ee491a: Download complete
856cbd0b7b9c: Download complete
af6d1e4decc6: Waiting
制作镜像
制作镜像我们必须先启动一个容器,比如这里启动busybox然后建立一点新的内容假定为我们基于该容器制作的镜像,加工的内容就是我们建立的这些文件
[root@localhost ~]# docker run --name bl -it busybox
/ # ls
bin dev etc home proc root run sys tmp usr var
/ # mkdir -p /data/html
/ # vi /data/html/index.html
/ #
制作镜像时我们所基于的容器就不能关闭,所以我们另外开一个会话进行制作,另外制作时候为了防止有新的文件写入等可能会导致制作镜像时候某些文件不完整,所以要用-p来实现制作过程中容器是暂停的
[root@localhost ~]# docker commit -p bl
sha256:f0f7ae2f422395b070490ffbc089936b6774c3a0533cdfd925eba5041163ff77
然后我们新制作的镜像就出来了
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> f0f7ae2f4223 2 minutes ago 1.22 MB
docker.io/busybox latest 018c9d7b792b 4 weeks ago 1.22 MB
docker.io/redis 4-alpine e3dd0e49bca5 4 months ago 20.4 MB
docker.io/nginx 1.14-alpine 8a2fb25a19f5 16 months ago 16 MB
打标签
上面我们基于一个容器创建了一个新的镜像,现在我们要给这个镜像添加一个tag
添加tag时要指定镜像id,然后是命名空间/镜像名:tag的形式
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
<none> <none> f0f7ae2f4223 2 minutes ago 1.22 MB
docker.io/busybox latest 018c9d7b792b 4 weeks ago 1.22 MB
docker.io/redis 4-alpine e3dd0e49bca5 4 months ago 20.4 MB
docker.io/nginx 1.14-alpine 8a2fb25a19f5 16 months ago 16 MB
[root@localhost ~]# docker tag f0f7ae2f4223 foe/httpd:v0.1-1
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
foe/httpd v0.1-1 f0f7ae2f4223 4 minutes ago 1.22 MB
docker.io/busybox latest 018c9d7b792b 4 weeks ago 1.22 MB
docker.io/redis 4-alpine e3dd0e49bca5 4 months ago 20.4 MB
docker.io/nginx 1.14-alpine 8a2fb25a19f5 16 months ago 16 MB
添加标签(基于已经存在tag)
Tag可以不止一个,当已经存在tag时候,我们继续添加tag则我们还可以这样添加,如
[root@localhost ~]# docker tag foe/httpd:v0.1-1 foe/httpd:latest
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
foe/httpd latest f0f7ae2f4223 8 minutes ago 1.22 MB
删除镜像
[root@localhost ~]# docker image rm foe/httpd:latest
Untagged: foe/httpd:latest
运行我们创建的镜像
[root@localhost ~]# docker run --name t1 -it foe/httpd:v0.1-1
/ # ls /data/html
index.html
/ # cat /data/html/index.html
this is index
/ #
这里可以看到我们刚才新增的内容已经出现了
创建新镜像并配置默认启动命令及同时打标签
上面创建的镜像默认启动命令都是bash即/bin/sh,这里要实现直接配置默认命令。
上面创建的镜像默认没有打标签,这里要实现创建的同时也打上标签。
则如
[root@localhost ~]# docker commit -a "foe <foe@foe.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p bl foe/httpd:v0.2
sha256:a2dbea4945739aba661a079fae087d4b344292776981dee5b2902688278e17b7
httpd的-f参数是让httpd运行在前台,-h参数是指定html目录,后面那个参数自然就是html目录咯
那我们可以看到我们这又一个镜像创建成功了
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
foe/httpd v0.2 a2dbea494573 4 minutes ago 1.22 MB
foe/httpd v0.1-1 f0f7ae2f4223 24 minutes ago 1.22 MB
docker.io/busybox latest 018c9d7b792b 4 weeks ago 1.22 MB
docker.io/redis 4-alpine e3dd0e49bca5 4 months ago 20.4 MB
docker.io/nginx 1.14-alpine 8a2fb25a19f5 16 months ago 16 MB
运行我们这次新建的容器
[root@localhost ~]# docker run --name t2 foe/httpd:v0.2
另外一个会话可以看到我们这次新建的容器已经运行了
[root@localhost ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
44fd298d3df4 foe/httpd:v0.2 "/bin/httpd -f -h ..." 46 seconds ago Up 45 seconds t2
9f5860b986a7 foe/httpd:v0.1-1 "sh" 13 minutes ago Up 13 minutes t1
a12aee4c7924 busybox "sh" 29 minutes ago Up 29 minutes bl
6e2f3d9d2545 redis:4-alpine "docker-entrypoint..." 19 hours ago Up 2 hours 6379/tcp kvstorl
9f1ed90b19a9 nginx:1.14-alpine "nginx -g 'daemon ..." 19 hours ago Up 2 hours 80/tcp web1
那么当我们用inspect再次得到这个运行的容器的ip地址如用curl访问也是ok的
"IPAddress": "172.17.0.6",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:06"
}
}
}
}
]
[root@localhost ~]# curl 172.17.0.6
this is index
由于我们上面默认指定了该容器默认运行的是httpd所以我们只需要启动该容器就自动运行了httpd而不用像之前一样还要在运行的这个容器内部还要手动运行一次httpd
上传到docker官方的镜像源中
这里由于官方镜像命名空间名称的字数限制,所以这里新建一个稍微长点的空间名的镜像
要推到docker官方镜像源则我们的镜像命名空间和镜像名要和我们在该官方镜像源中的仓库的命名空间名和镜像名都要保持一致,所以本地建立镜像如
[root@localhost ~]# docker commit -a "towards <towards@towards.com>" -c 'CMD ["/bin/httpd","-f","-h","/data/html"]' -p bl towards/httpd:v0.2
sha256:023aaf0a80ce889a9b50f45efcff0f9adccd6784c4dde1f7cdea00f5451f1100
那么我在官方注册的用户名为towards,然后创建仓库时默认命名空间就是我的用户名
然后创建的镜像名也要保持一致,这里就填写为httpd,然后描述就是随便填写咯
那么我们要推到官方源我们就得先登录该官方源
[root@localhost ~]# docker login -u towards
Password:
Login Succeeded
然后才开始推送哈
[root@localhost ~]# docker push towards/httpd
The push refers to a repository [docker.io/towards/httpd]
a7081b07b631: Pushed
514c3a3e64d4: Layer already exists
v0.2: digest: sha256:2575080d6b94d563954019be21d5d7f9b86a536c220c6f43e67a8b45588ffb58 size: 734
然后回到hub.docker.com就可以看到了
配置阿里云镜像加速
首先我们来到这里获取你的镜像加速,初次进来你可能要设置下你在阿里云这里的镜像源密码,然后就可以像下面一样获取到了。
这里我们是centos所以要选择centos的
然后配置到我们的docker配置文件中
[root@localhost ~]# cat /etc/docker/daemon.json
{
"registry-mirrors":["https://7bezldxe.mirror.aliyuncs.com","https://iktw4ld3.mirror.aliyuncs.com"]
}
推到阿里云
要想推到阿里云,那么我们在阿里云这里要先创建仓库
安装阿里云的推送提示
则我们需要先打一个tag
[root@localhost ~]# docker tag towards/httpd:v0.2 registry.cn-qingdao.aliyuncs.com/towards/httpd:v0.2
然后我们退出原来的登录信息重新登录到阿里云(如果你没有设置仓库密码需要先设置)
[root@localhost ~]# docker logout
Removing login credentials for https://index.docker.io/v1/
[root@localhost ~]# docker login --username=1509272975@qq.com registry.cn-qingdao.aliyuncs.com
Password:
Login Succeeded
然后我们push下
[root@localhost ~]# docker push registry.cn-qingdao.aliyuncs.com/towards/httpd
The push refers to a repository [registry.cn-qingdao.aliyuncs.com/towards/httpd]
596613877625: Pushed
514c3a3e64d4: Pushed
v0.2: digest: sha256:e9002c9cccc01f1cced49faa3727ca2f0134eb1330e9a685c0762f9fcf58b09c size: 734
然后在阿里云这里就能看到了
打包镜像
有时候我们要将本地的一个镜像给另外一个服务器,而如果按照我们现在(可称为传统)的方式就是先找一个公共的镜像源比如阿里云或者官方然后推送上去,然后到另外一个服务器再pull下来,这样很麻烦,我们可以用镜像打包,然后直接发给另外一个服务器,另外一个服务器再导入即可。
打包如:
[root@localhost ~]# docker save -o towards.gz towards/httpd:v0.1-1 towards/httpd:v0.2
Error response from daemon: reference does not exist
会发现这里打包失败,这里很可能是因为:
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
registry.cn-qingdao.aliyuncs.com/towards/httpd v0.2 023aaf0a80ce About an hour ago 1.22 MB
towards/httpd v0.2 023aaf0a80ce About an hour ago 1.22 MB
foe/httpd v0.2 a2dbea494573 About an hour ago 1.22 MB
foe/httpd v0.1-1 f0f7ae2f4223 2 hours ago 1.22 MB
docker.io/busybox latest 018c9d7b792b 4 weeks ago 1.22 MB
docker.io/redis 4-alpine e3dd0e49bca5 4 months ago 20.4 MB
docker.io/nginx 1.14-alpine 8a2fb25a19f5 16 months ago 16 MB
会发现这里towards/httpd刚才可能是增加了一个阿里云的tag,而我们这里打包应该是与官方有关的吧(个人想法)
那么我们用刚才没有打阿里云tag的这个试试
[root@localhost ~]# docker save -o foes.gz foe/httpd:v0.1-1 foe/httpd:v0.2
果然成功了
然后我们推送到我们另外一个服务器
[root@localhost ~]# scp foes.gz 192.168.108.129:/root/
The authenticity of host '192.168.108.129 (192.168.108.129)' can't be established.
ECDSA key fingerprint is SHA256:aLubfw4XMyHjpjyq7dK+D7Z0DG35zq/EbkmZY2meXdI.
ECDSA key fingerprint is MD5:2e:40:a5:f4:6b:bd:02:c0:c7:1a:d5:12:8d:d6:fc:66.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.108.129' (ECDSA) to the list of known hosts.
root@192.168.108.129's password:
foes.gz 100% 1444KB 71.7MB/s 00:00
导入镜像
刚才我们打包镜像并传入到另外一个服务器了,那么我们这个另外的服务器就也安装docker并开启服务然后就可以导入传入过来的这个镜像了
[root@localhost ~]# docker load -i foes.gz
514c3a3e64d4: Loading layer [==================================================>] 1.442 MB/1.442 MB
3359f3881ff7: Loading layer [==================================================>] 6.656 kB/6.656 kB
Loaded image: foe/httpd:v0.1-1
596613877625: Loading layer [==================================================>] 7.68 kB/7.68 kB
Loaded image: foe/httpd:v0.2
然后我们可以看到导入也ok了
[root@localhost ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
foe/httpd v0.2 a2dbea494573 3 hours ago 1.22 MB
foe/httpd v0.1-1 f0f7ae2f4223 3 hours ago 1.22 MB
关键字词:docker,push,导入,导出,load
上一篇:02-Docker基础用法
下一篇:04-容器虚拟化网络概述