一 。私有registry简介
Docker Hub公共仓库由于在国外,速度相对较慢,同maven仓库,yum仓库一样可以创建一个本地的代理仓库 供局域网使用 一般docker提供了registry的仓库用于搭建
二 。registry配置
1》安装registry镜像
docker下载registry镜像
[root@localhost shell]# docker pull registry
Using default tag: latest
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:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8
Status: Downloaded newer image for docker.io/registry:latest
新建一个目录 用于存储将来下载或者保存的镜像
mkdir /docker_images
启动容器(注意registry镜像默认是将上传的镜像保存在 /var/lib/registry目录下 所以挂载到这个目录)
docker run -d -p 5000:5000 -v /docker_images:/var/lib/registry --name reg registry
其中
-p 5000:5000 接口映射到 registry的5000端口
-v把registry的镜像路径/docker_images映射到本机的/docker_image
-name 给容器制定一个名称
确定 5000端口是否开启
yum -y install net-tools 如果没有netstat命令安装先
[root@localhost shell]# netstat -aon | grep 5000
tcp6 0 0 :::5000 :::* LISTEN off (0.00/0/0)
2》制作镜像
使用 centos镜像 启动一个容器 然后反向制作一个images
获取cenos镜像
docker pull centos
启动一个容器
[root@localhost shell]# docker run -itd centos
ae324123ef5c30754843b4246b52d6195ed36a0e86b8e22530e29cc1382cc328
查看启动的容器id
[root@localhost shell]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae324123ef5c centos "/bin/bash" 3 seconds ago Up 3 seconds
通过id制作一个镜像
[root@localhost shell]# docker commit ae324123ef5c mycentos:jiaozi
sha256:9eea42e7d7079836ec994777cf113f5268a97da026b59ec410115e7abe4560a5
[root@localhost shell]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos jiaozi 9eea42e7d707 5 seconds ago 200 MB
docker.io/registry latest b2b03e9146e1 3 weeks ago 33.3 MB
docker.io/centos latest 49f7960eb7e4 8 weeks ago 200 MB
3》将镜像添加到私有registry中
给当前做的镜像 第一列repository添加远程registry的ip和端口 使用tag功能 tag以下 就会多出一个镜像 指向原始镜像
[root@localhost docker_images]# docker tag mycentos:jiaozi localhost:5000/mycentos:jiaozi
[root@localhost docker_images]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/mycentos jiaozi 9eea42e7d707 16 minutes ago 200 MB
mycentos jiaozi 9eea42e7d707 16 minutes ago 200 MB
docker.io/registry latest b2b03e9146e1 3 weeks ago 33.3 MB
docker.io/centos latest 49f7960eb7e4 8 weeks ago 200 MB
执行push到私有镜像中
[root@localhost docker]# docker push localhost:5000/mycentos:jiaozi
The push refers to a repository [localhost:5000/mycentos]
dbe7d6f855a8: Pushed
bcc97fbfc9e1: Pushed
jiaozi: digest: sha256:9a3b9836eb52efc6f09ddf7aca821784f47700dba2c6b34019f3743c34e646dc size: 736
如果不成功 修改 /etc/docker/daemon.json 添加
{"insecure-registries":["127.0.0.1:5000"] }
这里不知道为何 添加了-v挂载就一直重试无法成功 实际启动registry 改成了
docker run -d -p 5000:5000 --name reg registry
接下来测试删掉本地镜像
docker rmi localhost:5000/mycentos:jiaozi
docker rmi mycentos:jiaozi
查看确定删除
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/registry latest b2b03e9146e1 3 weeks ago 33.3 MB
从私有仓库pull 成功
[root@localhost /]# docker pull localhost:5000/mycentos:jiaozi
Trying to pull repository localhost:5000/mycentos ...
jiaozi: Pulling from localhost:5000/mycentos
7dc0dca2b151: Pull complete
3018713012e7: Pull complete
Digest: sha256:9a3b9836eb52efc6f09ddf7aca821784f47700dba2c6b34019f3743c34e646dc
Status: Downloaded newer image for localhost:5000/mycentos:jiaozi
[root@localhost /]# ^C
[root@localhost /]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost:5000/mycentos jiaozi 9eea42e7d707 46 minutes ago 200 MB
docker.io/registry latest b2b03e9146e1 3 weeks ago 33.3 MB
运行
[root@localhost /]# docker run -itd localhost:5000/mycentos:jiaozi
a9cd1c7142da764cb0f5e9ed92ee7d591f2aa70dbe808b5fee391c83f96d0907
查看实际私有仓库上传的文件保存的位置(/var/lib/registry/docker/registry/v2/repositories)
[root@localhost /]# docker exec -it reg /bin/sh
/ # cd /var/lib/registry
/var/lib/registry # ls
docker
/var/lib/registry # cd docker
/var/lib/registry/docker # ls
registry
/var/lib/registry/docker # cd registry
/var/lib/registry/docker/registry # ls
v2
/var/lib/registry/docker/registry # cd v2
/var/lib/registry/docker/registry/v2 # ls
blobs repositories
/var/lib/registry/docker/registry/v2 # cd repositories
/var/lib/registry/docker/registry/v2/repositories # ls
mycentos