1. 私有仓库安装
docker-registry 是官⽅提供的⼯具,可以⽤于构建私有的镜像仓库。可以通过获取官⽅ registry 镜像来运⾏。先安装好docker。执行如下命令安装
$ docker run -d -p 5000:5000 --restart=always --name registry registry
默认情况下,仓库会被创建在容器的 /var/lib/registry ⽬录下。你可以通过 -v 参数来将镜像⽂件存放在本地的指定路径。例如下⾯的例⼦将上传的镜像放到本地的 /opt/data/registry(若没有先创建) ⽬录。
$ docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry --restart=always --name registry registry
-d 后台运行
-p 5000:5000 前面是Docker主机的端口,后面是容器的端口,意为将容器的5000映射到主机的5000端口
-v 是一个bind mount,以为把主机的/opt/data/registry挂载到容器的/var/lib/registry目录
--restart=always Docker重启时容器自动启动
--name registry 容器起个名字
2. 使能http推送镜像
这是因为 Docker 默认不允许⾮ HTTPS ⽅式推送镜像。我们可以通过 Docker 的配置选项来取消这个
限制。
对于systemd 系统Centos7,打开客户端主机的daemon.json,vi /etc/docker/daemon.json添加"insecure-registries":["192.168.110.137:5000"]
{
"registry-mirrors":["http://hub-mirror.c.163.com"],
"insecure-registries":["192.168.110.137:5000"]
}
重启服务
systemctl daemon-reload
systemctl restart docker
对于upstart系统Ubuntu,编辑 /etc/default/docker ⽂件,在其中的 DOCKER_OPTS 中增加如下内
容:
DOCKER_OPTS="--registry-mirror=https://registry.docker-cn.com --insecure-registries=192.168.110.137:5000
重启服务
sudo service docker restart
3. push镜像
先拉去一个镜像,或者直接使用已经下载的镜像打tag,然后推送到仓库
- 拉取最新的Ubuntu:
docker pull ubuntu
- 打TAG:
docker tag ubuntu:latest 192.168.110.137:5000/ubuntu:latest
- push到私有仓库:
docker push 192.168.110.137:5000/ubuntu
- 查看结果:
curl 192.168.110.137:5000/v2/_catalog
[root@localhost registry]# docker pull ubuntu
Using default tag: latest
latest: Pulling from library/ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:747d2dbbaaee995098c9792d99bd333c6783ce56150d1b11e333bbceed5c54d7
Status: Downloaded newer image for ubuntu:latest
[root@localhost registry]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1d622ef86b13 4 weeks ago 73.9MB
registry latest 708bc6af7e5e 4 months ago 25.8MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@localhost registry]# docker tag ubuntu:latest 192.168.110.137:5000/ubuntu:latest
[root@localhost registry]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.137:5000/ubuntu latest 1d622ef86b13 4 weeks ago 73.9MB
ubuntu latest 1d622ef86b13 4 weeks ago 73.9MB
registry latest 708bc6af7e5e 4 months ago 25.8MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@localhost registry]# docker push 192.168.110.137:5000/ubuntu
The push refers to repository [192.168.110.137:5000/ubuntu]
8891751e0a17: Pushed
2a19bd70fcd4: Pushed
9e53fd489559: Pushed
7789f1a3d4e9: Pushed
latest: digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8 size: 1152
[root@localhost registry]# curl 192.168.110.137:5000/v2/_catalog
{"repositories":["ubuntu"]}
[root@localhost registry]#
4. pull私有仓库的镜像
docker pull 192.168.110.137:5000/ubuntu:latest
[root@localhost docker]# docker pull 192.168.110.137:5000/ubuntu:latest
latest: Pulling from ubuntu
d51af753c3d3: Pull complete
fc878cd0a91c: Pull complete
6154df8ff988: Pull complete
fee5db0ff82f: Pull complete
Digest: sha256:5747316366b8cc9e3021cd7286f42b2d6d81e3d743e2ab571f55bcd5df788cc8
Status: Downloaded newer image for 192.168.110.137:5000/ubuntu:latest
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.110.137:5000/ubuntu latest 1d622ef86b13 4 weeks ago 73.9MB
hello-world latest bf756fb1ae65 4 months ago 13.3kB
[root@localhost docker]#