建立本地镜像库,可有效缩短抽取镜像所需的时间,更重要的是可以将修改后的镜像上传到自定义的镜像库,从而易于实现在各docker客户端间的镜像推送。
首先,需要一台装有docker的主机,在该主机上从docker官方镜像库中抽取registry镜像。
#docker pull registry latest: Pulling from registry f15ce52fc004: Pull complete c4fae638e7ce: Pull complete a4c5be5b6e59: Pull complete 8693db7e8a00: Pull complete c75fbf48538e: Pull complete 87c008f1a79b: Pull complete 5a7b60cbc471: Pull complete 81569492134a: Pull complete 0aed6b236dec: Pull complete f3d8a78718d2: Pull complete 32e4b1196a41: Pull complete 15f0e3b29c8b: Pull complete 4b0669413b8f: Pull complete 07d93e41c370: Pull complete Digest: sha256:f374c0d9b59e6fdf9f8922d59e946b05fbeabaed70b0639d7b6b524f3299e87b Status: Downloaded newer image for registry:latest |
在宿主机上为镜像存储建立目录 /data/registry
mkdir -p /data/registry
然后从registry启动一个实例,并将 /data/registry 映射到实例的 /tmp (v2版本的registry则位于registry实例的 /var/lib/registry )
#docker run -d -v /data/registry:/tmp --name registry --net=none registry f1fe8b1e0d9995f1dbd476f8a06506da004c69c513146f161fd3797299513b30 # docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f1fe8b1e0d99 registry:latest "docker-registry" 4 seconds ago Up 4 seconds registry |
由于我采用的网络模式为桥接,因此在此不指定端口映射并制定 –net=none 参数以便后续设置静态地址。
启动后,使用pipework来为刚才启动的实例设置网络连接
pipework bridge0 registry 172.16.71.1/24@172.16.71.10
一旦部署成功,用浏览器打开registry的5000端口可以看到响应
#curl http://172.16.71.1:5000/ "\"docker-registry server\"" |
由于私建的镜像库没有设置加密连接(https),因此使用时会被docker客户端拒绝,为了使其能正常服务,需要在各客户端docker的启动配置文件 /etc/default/docker 的启动参数中加入
–insecure-registry 你的服务器IP:5000
改完的配置文件如下图
# Docker Upstart and SysVinit configuration file # Customize location of Docker binary (especially for development testing). #DOCKER="/usr/local/bin/docker" # Use DOCKER_OPTS to modify the daemon startup options. #DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" # If you need Docker to use an HTTP proxy, it can also be specified here. #export http_proxy="http://127.0.0.1:3128/" # This is also a handy place to tweak where Docker's temporary files go. #export TMPDIR="/mnt/bigdrive/docker-tmp" DOCKER_OPTS="--storage-driver=overlay --graph=/data/docker_instance --bridge=bridge0 –insecure-registry 172.16.71.1:5000" |
接着试着将一个已经修改后的镜像上传至我们搭建的镜像库
#docker tag mysql 172.16.71.1:5000/mysql #docker push 172.16.71.1:5000/mysql The push refers to a repository [172.16.71.1:5000/mysql] (len: 1) Sending image list Pushing repository 172.16.71.1:5000/mysql (1 tags) 3690474eb5b4: Image successfully pushed 28eec73de504: Image successfully pushed 564148e4490c: Image successfully pushed a0f8f0620fa4: Image successfully pushed 16e9b799a3bf: Image successfully pushed 497156b1e9e7: Image successfully pushed Pushing tag for rev [497156b1e9e7] on {http://172.16.71.1:5000/v1/repositories/mysql/tags/latest} |
看看宿主机的 /data/registry 目录已经有刚才上传的镜像了
#ls /data/registry docker-registry.db registry |
在另一台主机上抽取刚才上传的镜像
#docker pull 172.16.71.1:5000/mysql Pulling repository 172.16.71.1:5000/mysql 497156b1e9e7: Download complete 3690474eb5b4: Download complete 28eec73de504: Download complete 564148e4490c: Download complete a0f8f0620fa4: Download complete 16e9b799a3bf: Download complete Status: Downloaded newer image for 172.16.71.1:5000/mysql:latest |
看下拉下来的镜像
#docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE 172.16.71.1:5000/mysql latest 497156b1e9e7 2 weeks ago 376.1 MB |
如果觉得镜像的名字太长难记,可以使用 docker tag 命令来对其进行重命名镜像
#docker tag -f 172.16.71.1:5000/mysql mysql:latest #docker rmi 172.16.71.1:5000/mysql Untagged: 172.16.71.1:5000/mysql:latest #docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE mysql latest 497156b1e9e7 2 weeks ago 376.1 MB |
请注意,本站的所有文章均要求阁下在转载时注明出处和原作者,阁下转载本站文章即表示阁下同意并遵守此规程,除非特别注明转载出处,否则文章即为其发布者所著。本站及文章作者保留文章的著作权并有权在阁下违反上述规程时予以追究。
本文链接地址: 初识DOCKER(15)–建立本地镜像库