部署私有仓库并设置仓库用户和密码

首先docker部署完成的前提:

docker pull registry
创建几个目录与容器内的目录映射:将容器内的文件映射到本地,以此来达到持久化的效果
mkdir  /opt/auth
mkdir  /opt/registry
yum install -y httpd-tools (可以把密码加密)
htpasswd  -Bbn   test  123456   > /opt/auth/htpasswd     加密密码
[root@mayi-2 auth]# cat htpasswd
test:$2y$05$SNE8IfyVZ5tq6Nw9Q2Sb5OfxhSAT6RaMsSCAgjleynhLivYv0sGXm
 docker run -d -p 5000:5000 \
     -v /opt/auth/:/auth/  \
     -v /opt/registry/:/var/lib/registry  \
     -e "REGISTRY_AUTH=htpasswd" \
     -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
     -e  "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
     registry

解释:
 docker run -d -p 5000:5000 \
     -v /opt/auth/(本地目录):/auth/(容器目录)  \
     -v /opt/registry/(本地目录):/var/lib/registry  \      (容器目录)
     -e "REGISTRY_AUTH=htpasswd" \                          (以 htpasswd 的方式认证)
     -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \     (注册认证)
     -e  "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \     (认证的用户密码)
     registry

测试成果:

[root@mayi-2 auth]# docker login 127.0.0.1:5000
Username: test
Password:
Login Succeeded
或者:
[root@mayi-2 opt]#  curl -XGET --user test:123456  http://127.0.0.1:5000/v2/_catalog
{"repositories":["registry"]}

后续:

上面的启动方式重启docker后,容器进程会消失。虽然做了持久化。但是容器不会自动起来
所以增加启动参数【--restart=always】,保证重启后仓库数据也会存在

 docker run -d -p 5000:5000 \
     --restart=always \
      --name registry \
     -v /opt/auth/:/auth/  \
     -v /opt/registry/:/var/lib/registry  \
     -e "REGISTRY_AUTH=htpasswd" \
     -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
     -e  "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" \
     registry
[root@mayi-2 opt]# systemctl restart docker
[root@mayi-2 opt]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

[root@mayi-2 opt]#  docker run -d -p 5000:5000 --restart=always --name registry2  -v /opt/auth/:/auth/   -v /opt/registry/:/var/lib/registry   -e "REGISTRY_AUTH=htpasswd"  -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm"  -e  "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd"  registry
15bf7d3451a6d0b17876d2b0c44e0fa24fd4a032b6f8edd23fa4f2418348c19d
[root@mayi-2 opt]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
15bf7d3451a6        registry            "/entrypoint.sh /etc…"   6 seconds ago       Up 5 seconds        0.0.0.0:5000->5000/tcp   registry2
[root@mayi-2 opt]#  curl -XGET --user test:123456  http://127.0.0.1:5000/v2/_catalog
{"repositories":["registry"]}
[root@mayi-2 opt]# systemctl restart docker
[root@mayi-2 opt]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
15bf7d3451a6        registry            "/entrypoint.sh /etc…"   56 seconds ago      Up 3 seconds        0.0.0.0:5000->5000/tcp   registry2
[root@mayi-2 opt]#  curl -XGET --user test:123456  http://127.0.0.1:5000/v2/_catalog
{"repositories":["registry"]}

后续:部署registry-web,页面访问

你可能感兴趣的:(部署私有仓库并设置仓库用户和密码)