一、环境准备
准备两套CentOS 7.5,一套为Docker Registry,一套为Docker Client,都请安装Docker,方法请参见:https://blog.csdn.net/twingao/article/details/80934212。
Docker Registry ip addr: 192.168.232.25 hostname: registry
Docker Client ip addr: 192.168.232.12 hostname: docker3
# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
# docker version
Client:
Version: 1.13.1
API version: 1.26
Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
Go version: go1.9.4
Git commit: 94f4240/1.13.1
Built: Fri May 18 15:44:33 2018
OS/Arch: linux/amd64
Server:
Version: 1.13.1
API version: 1.26 (minimum version 1.12)
Package version: docker-1.13.1-63.git94f4240.el7.centos.x86_64
Go version: go1.9.4
Git commit: 94f4240/1.13.1
Built: Fri May 18 15:44:33 2018
OS/Arch: linux/amd64
Experimental: false
二、Docker Registry安装
官方在Docker Hub上提供了Docker Registry的镜像。我们可以直接使用该镜像构建一个Registry容器来搭建我们的Registry私有仓库。
# docker search registry
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/registry The Docker Registry 2.0 implementation for... 2058 [OK]
docker.io docker.io/konradkleine/docker-registry-frontend Browse and modify your Docker registry in ... 194 [OK]
docker.io docker.io/hyper/docker-registry-web Web UI, authentication service and event r... 139 [OK]
docker.io docker.io/atcol/docker-registry-ui A web UI for easy private/local Docker Reg... 106 [OK]
docker.io docker.io/distribution/registry WARNING: NOT the registry official image!!... 56 [OK]
docker.io docker.io/marvambass/nginx-registry-proxy Docker Registry Reverse Proxy with Basic A... 43 [OK]
docker.io docker.io/google/docker-registry Docker Registry w/ Google Cloud Storage dr... 35
docker.io docker.io/jhipster/jhipster-registry JHipster Registry, based on Netflix Eureka... 25 [OK]
docker.io docker.io/confluentinc/cp-schema-registry Official Confluent Docker Images for Schem... 20
docker.io docker.io/deis/registry Docker image registry for the Deis open so... 12
docker.io docker.io/joxit/docker-registry-ui Docker registry v2 web User Interface 11 [OK]
docker.io docker.io/klausmeyer/docker-registry-browser Web Interface for the Docker Registry HTTP... 11 [OK]
docker.io docker.io/openshift/origin-docker-registry The integrated OpenShift V3 registry 11
docker.io docker.io/landoop/schema-registry-ui UI for Confluent's Schema Registry 7 [OK]
docker.io docker.io/cblomart/rpi-registry docker registry 2 for raspbery pi 5
docker.io docker.io/allingeek/registry A specialization of registry:2 configured ... 4 [OK]
docker.io docker.io/elasticio/docker-registry-ecs Docker image to run Docker private registr... 4 [OK]
docker.io docker.io/pallet/registry-swift Add swift storage support to the official ... 4 [OK]
docker.io docker.io/aibaars/docker-registry2-gcs Docker Registry2 w/ Google Cloud Storage d... 1
docker.io docker.io/metadata/registry Metadata Registry is a tool which helps yo... 1 [OK]
docker.io docker.io/webhippie/registry Docker images for registry 1 [OK]
docker.io docker.io/convox/registry 0
启动Registry容器。
# docker run -d -p 5000:5000 -v /opt/registry/data:/var/lib/registry --privileged=true --restart=always --name registry registry:latest
Unable to find image 'registry:latest' locally
Trying to pull repository docker.io/library/registry ...
latest: Pulling from docker.io/library/registry
4064ffdc82fe: Pull complete
c12c92d1c5a2: Pull complete
4fbc9b6835cc: Pull complete
765973b0f65f: Pull complete
3968771a7c3a: Pull complete
Digest: sha256:003a106b827ab7f5bd7140d08020b16c87cd6bcac024b01fe6247f87632f2978
Status: Downloaded newer image for docker.io/registry:latest
e0e8e26f0e296868d54f53f35f55ea4c1a5b763fa7804c67ef676fa023c05d6a
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e0e8e26f0e29 registry:latest "/entrypoint.sh /e..." 45 seconds ago Up 43 seconds 0.0.0.0:5000->5000/tcp registry
打开浏览器访问http://192.168.232.25:5000/v2/,出现下图表示安装成功。
三、Docker Registry使用
制作私有仓库镜像
先从官方Docker Hub上拉取busybox镜像。
# docker pull busybox
Using default tag: latest
Trying to pull repository docker.io/library/busybox ...
latest: Pulling from docker.io/library/busybox
07a152489297: Pull complete
Digest: sha256:74f634b1bc1bd74535d5209589734efbd44a25f4e2dc96d78784576a3eb5b335
Status: Downloaded newer image for docker.io/busybox:latest
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 2cb0d9787c4d 3 days ago 1.85 kB
docker.io/registry latest b2b03e9146e1 7 days ago 33.3 MB
docker.io/tomcat latest 2d084b11164d 10 days ago 463 MB
docker.io/busybox latest 8c811b4aec35 7 weeks ago 1.15 MB
修改该镜像的tag。
# docker tag docker.io/busybox:latest 192.168.232.25:5000/busybox:latest
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/hello-world latest 2cb0d9787c4d 3 days ago 1.85 kB
docker.io/registry latest b2b03e9146e1 7 days ago 33.3 MB
docker.io/tomcat latest 2d084b11164d 10 days ago 463 MB
192.168.232.25:5000/busybox latest 8c811b4aec35 7 weeks ago 1.15 MB
docker.io/busybox latest 8c811b4aec35 7 weeks ago 1.15 MB
推送该镜像到Registry私有仓库。
docker push 192.168.232.25:5000/busybox:latest
The push refers to a repository [192.168.232.25:5000/busybox]
Get https://192.168.232.25:5000/v1/_ping: http: server gave HTTP response to HTTPS client
因为Docker与Docker Registry交互默认使用https,然而此处搭建的Docker Registry只提供http服务,所以当和Registry私有仓库交互时会失败,为了解决这个问题需要在启动Docker时配置Registry不安全选项。
vi /etc/docker/daemon.json
{
"registry-mirrors": [
"http://68cdf6cf.m.daocloud.io"
],
"insecure-registries":["192.168.232.25:5000"]
}
重启Docker,并重启registry容器。
# systemctl restart docker
# docker start registry
重新推送busybox镜像到Registry私有仓库,并成功。
# docker push 192.168.232.25:5000/busybox:latest
The push refers to a repository [192.168.232.25:5000/busybox]
432b65032b94: Pushed
latest: digest: sha256:74f634b1bc1bd74535d5209589734efbd44a25f4e2dc96d78784576a3eb5b335 size: 527
从私有仓库拉取镜像
在Docker Client终端拉取Registry私有仓库镜像。
查询Registry私有仓库镜像列表。
# curl http://192.168.232.25:5000/v2/_catalog
{"repositories":["busybox"]}
查询busybox镜像的标签列表。
# curl http://192.168.232.25:5000/v2/busybox/tags/list
{"name":"busybox","tags":["latest"]}
从Registry私有仓库拉取镜像。
# docker pull 192.168.232.25:5000/busybox:latest
Trying to pull repository 192.168.232.25:5000/busybox ...
Get https://192.168.232.25:5000/v1/_ping: http: server gave HTTP response to HTTPS client
报相同的错误,请进行相同的配置,并重启Docker。然后重新拉取busybox镜像。
# docker pull 192.168.232.25:5000/busybox:latest
Trying to pull repository 192.168.232.25:5000/busybox ...
latest: Pulling from 192.168.232.25:5000/busybox
07a152489297: Pull complete
Digest: sha256:74f634b1bc1bd74535d5209589734efbd44a25f4e2dc96d78784576a3eb5b335
Status: Downloaded newer image for 192.168.232.25:5000/busybox:latest
查看镜像列表。
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.232.25:5000/busybox latest 8c811b4aec35 7 weeks ago 1.15 MB
四、Docker CE
Docker CE没有/etc/docker/daemon.json配置文件。请修改/usr/lib/systemd/system/docker.service。
# vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --registry-mirror=http://xxxxxxxx.m.daocloud.io --insecure-registry 192.168.232.25:5000
生效配置并重启Docker。
# systemctl daemon-reload
# systemctl restart docker
拉取Registry私有仓库的镜像。
# docker pull 192.168.197.10:5000/tomcat:latest
latest: Pulling from tomcat
55cbf04beb70: Pull complete
1607093a898c: Pull complete
9a8ea045c926: Pull complete
1290813abd9d: Pull complete
8a6b982ad6d7: Pull complete
abb029e68402: Pull complete
8cd067dc06dc: Pull complete
1b9ce2097b98: Pull complete
d6db5874b692: Pull complete
25b4aa3d52c5: Pull complete
53ec227dabf0: Pull complete
242938ace8b4: Pull complete
Digest: sha256:cded14cf64697961078aedfdf870e704a52270188c8194b6f70c778a8289d87e
Status: Downloaded newer image for 192.168.197.10:5000/tomcat:latest