前面已经使用dockerfile构建了镜像,但是构建的镜像仅仅只能在本地使用,如果我们想要在别的服务器上使用这些镜像怎么办呢?当然,我们可以将dockerfile拷贝到别的服务器上,再次构建镜像就可以使用,但是这样就增加了工作量,而且有的构建可能耗时比较久,这个在某些时候是无法容忍的。除了重新构建,我们还可以将镜像导出,然后拷贝到其他服务器上,再次导入即可。但如果想统一管理,标准化构建和版本控制,就像svn、git那样,Docker也有自己的镜像仓库。
当Docker请求使用一个容器镜像时,如果该镜像尚未在本地操作系统中,Docker会从“某处”拉取镜像到本地,一般而言这里的“某处”指的是Docker Hub,但也可以通过Docker配置文件指定到其他位置,这些位置被称为registry。所谓的Docker Registry是镜像仓库,用于管理、保存镜像,用户可以从仓库pull或push镜像。镜像仓库分为公共仓库(Docker Hub)和私有仓库,但无论是公共仓库或私有仓库,都可以为用户提供镜像仓库的服务。
搭建私有镜像仓库:
# 1. 从Docker Hub上拉取registry镜像[root@centos7 ~]# docker pull registryUsing default tag: latestTrying to pull repository docker.io/library/registry ... latest: Pulling from docker.io/library/registryc87736221ed0: Pull complete 1cc8e0bb44df: Pull complete 54d33bcb37f5: Pull complete e8afc091c171: Pull complete b4541f6d3db6: Pull complete Digest: sha256:e9fbfbf29d2f0110e67e662459a549a813be233131b04c002d9f7d567e144648Status: Downloaded newer image for docker.io/registry:latest# 2. 创建一个目录,用于存放镜像[root@centos7 ~]# mkdir /registry/repos -p# 3. 创建一个运行registry镜像的容器,这里指定--restart=always[root@centos7 ~]# docker run -itd --name registry --restart=always -p 5000:5000 -v /registry/repos:/var/lib/registry registry 1757e40146f06479aa0bc273a9a1d9198b241748b5b486522b19e3d291f3e15d[root@centos7 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1757e40146f0 registry "/entrypoint.sh /e..." 4 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp registry# 4. 查看registry镜像仓库中的repo,默认为空[root@centos7 ~]# curl http://192.168.49.40:5000/v2/_catalog{"repositories":[]}# 5. 修改docker配置文件,添加私有仓库地址[root@centos7 ~]# vi /etc/docker/daemon.json [root@centos7 ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://registry.docker-cn.com"], "insecure-registries": ["192.168.49.40:5000"]}# 该文件默认不存在,需要手工创建;另外,这里第一个仓库是做docker hub加速用的,第二个是我们需要添加的registry仓库地址。# 6. 重启docker服务(因为我是在运行docker registry的宿主机上进行docker镜像仓库的管理示例)[root@centos7 ~]# systemctl restart docker# 7. 因为前面在创建registry容器的时候指定了--restart=always,所以容器已经自动运行起来[root@centos7 ~]# docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES1757e40146f0 registry "/entrypoint.sh /e..." 10 minutes ago Up 7 seconds 0.0.0.0:5000->5000/tcp registry# 8. 查看当前的镜像列表[root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MBcentos_ssh v1 d8b7ce88a878 10 days ago 228 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB# 9. 给docker镜像打新的标签[root@centos7 ~]# docker tag centos_ssh:v1 192.168.49.40:5000/centos_ssh:v1.0[root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MB192.168.49.40:5000/centos_ssh v1.0 d8b7ce88a878 10 days ago 228 MBcentos_ssh v1 d8b7ce88a878 10 days ago 228 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB# 10. 上传镜像到registry仓库中[root@centos7 ~]# docker push 192.168.49.40:5000/centos_ssh:v1.0The push refers to a repository [192.168.49.40:5000/centos_ssh]3e156a1d0a35: Pushed 071d8bd76517: Pushed v1.0: digest: sha256:636a631790f69d63fafdb9adae02f4e853863d774a235a6ef8fdb46e78ab19d5 size: 740# 11. 再次查看registry镜像仓库中的repo[root@centos7 ~]# curl http://192.168.49.40:5000/v2/_catalog{"repositories":["centos_ssh"]}# 12. 使用命令删除本地的docker镜像[root@centos7 ~]# docker rmi centos_ssh:v1Untagged: centos_ssh:v1Untagged: 192.168.49.40:5000/centos_ssh@sha256:636a631790f69d63fafdb9adae02f4e853863d774a235a6ef8fdb46e78ab19d5[root@centos7 ~]# docker rmi 192.168.49.40:5000/centos_ssh:v1.0Untagged: 192.168.49.40:5000/centos_ssh:v1.0Deleted: sha256:d8b7ce88a878f95991b6ce7f4b48ce88fa2056361f9016274782da05b635b771Deleted: sha256:912fddb1f69986ea525400ac7d829873268663dc112a0f826aa132d2ba0d4984Deleted: sha256:6f49ee38e97a94d648843f742bf4044e05ed6bed424d2b1e186f74f0d570a1e9Deleted: sha256:9ff080e7a2d35468c0cbc366d667702224125198beb0c573a96d92ef97445f60Deleted: sha256:34a008d1efb633fb57fcbdae681e7c35ecdd473bd33e51b844ac994ce41772db# 13. 确认本地已经不存在centos_ssh镜像[root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB# 14. 尝试从registry仓库中拉取centos_ssh镜像[root@centos7 ~]# docker pull 192.168.49.40:5000/centos_ssh:v1.0Trying to pull repository 192.168.49.40:5000/centos_ssh ... v1.0: Pulling from 192.168.49.40:5000/centos_ssha02a4930cb5d: Already exists 591783ae0f02: Pull complete Digest: sha256:636a631790f69d63fafdb9adae02f4e853863d774a235a6ef8fdb46e78ab19d5Status: Downloaded newer image for 192.168.49.40:5000/centos_ssh:v1.0[root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MB192.168.49.40:5000/centos_ssh v1.0 d8b7ce88a878 10 days ago 228 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB# 15. 可以使用如下命令查看registry中镜像的标签列表[root@centos7 ~]# curl http://192.168.49.40:5000/v2/centos_ssh/tags/list{"name":"centos_ssh","tags":["v1.0"]}
使用公共镜像仓库:
# 1. 登录docker hub[root@centos7 ~]# docker loginLogin 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 (jerry12356): jerry12356 Password: Login Succeeded# 2. 给本地镜像打标签root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MB192.168.49.40:5000/centos_ssh v1.0 d8b7ce88a878 10 days ago 228 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB[root@centos7 ~]# docker tag myjava:v1 jerry12356/myjava:v1.0# 3. 将镜像上传到docker hub[root@centos7 ~]# docker push jerry12356/myjava:v1.0The push refers to a repository [docker.io/jerry12356/myjava]8f405b2c34eb: Pushing [==================================================>] 400.6 MBedfbb99d80ee: Pushed 90571a9ad256: Pushing [==================================================>] 189.8 MB071d8bd76517: Mounted from library/centos 8f405b2c34eb: Pushed 90571a9ad256: Pushed v1.0: digest: sha256:0c1a76b67672186cd42d67bc51d3f1de062f15bb064e2ef578dd5347774be77c size: 1166# 4. 删除本地的myjava镜像[root@centos7 ~]# docker rmi jerry12356/myjava:v1.0Untagged: jerry12356/myjava:v1.0Untagged: jerry12356/myjava@sha256:0c1a76b67672186cd42d67bc51d3f1de062f15bb064e2ef578dd5347774be77c[root@centos7 ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/registry latest f32a97de94e1 6 days ago 25.8 MBmyjava v1 b0d98340393f 9 days ago 800 MB192.168.49.40:5000/centos_ssh v1.0 d8b7ce88a878 10 days ago 228 MBmynginx v1 5cd531df9a2d 11 days ago 361 MBdocker.io/nginx latest f09fe80eb0e7 5 weeks ago 109 MBdocker.io/busybox latest 3a093384ac30 2 months ago 1.2 MBdocker.io/centos latest 1e1148e4cc2c 3 months ago 202 MB# 5. 重新从docker hub上拉取myjava镜像[root@centos7 ~]# docker pull jerry12356/myjava:v1.0Trying to pull repository docker.io/jerry12356/myjava ... v1.0: Pulling from docker.io/jerry12356/myjavaDigest: sha256:0c1a76b67672186cd42d67bc51d3f1de062f15bb064e2ef578dd5347774be77cStatus: Downloaded newer image for docker.io/jerry12356/myjava:v1.0