自学内容网 自学内容网

docker仓库

一、创建 Docker 镜像仓库

  1. 启动私有仓库容器
    通过 Docker 启动一个私有镜像仓库。这里使用官方的 registry 镜像。
#docker run -d:在后台运行一个容器。
#-p 5000:5000:将容器内的 5000 端口映射到宿主机的 5000 端口,私有仓库将通过此端口访问。
#--restart=always:保证容器在主机重启后自动启动。
#--name registry:为该容器命名为 registry。
#-v /opt/myregistry:/var/lib/registry:将主机目录 /opt/myregistry 挂载到容器的 /var/lib/registry,用于持久化存储仓库数据。
[root@localhost lib]# docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry registry

Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
1cc3d825d8b2: Pull complete 
85ab09421e5a: Pull complete 
40960af72c1c: Pull complete 
e7bb1dbb377e: Pull complete 
a538cc9b1ae3: Pull complete 
Digest: sha256:ac0192b549007e22998eb74e8d8488dcfe70f1489520c3b144a6047ac5efbe90
Status: Downloaded newer image for registry:latest
e88bcb2919efad55b76fc7c00f1fad60954b83f18e01c35da565dd20a4b9815c
[root@localhost lib]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    39286ab8a5e1   5 weeks ago     188MB
registry     latest    75ef5b734af4   11 months ago   25.4MB
  1. 修改 Docker 配置文件支持 HTTP 访问
    默认情况下,Docker 只允许使用 HTTPS 安全地与仓库进行通信。为了允许使用 HTTP 访问你的私有仓库,需要修改 Docker 的配置文件。
vi /etc/docker/daemon.json
#加入
"insecure-registries": ["192.168.29.210:5000"]

在这里插入图片描述
3. 重启 Docker 服务
修改配置后需要重启 Docker 服务,以便让修改生效。

[root@localhost lib]# systemctl restart docker

二、操作镜像

  1. 修改镜像标签
    以nginx镜像举例,现在你需要为这个镜像打上标签,并推送到私有仓库。以下是将 nginx:latest 镜像重命名为适合上传私有仓库的标签。
# docker tag:用于给现有的镜像打标签。
# 192.168.29.210:5000/eden/nginx:1.0:新标签格式为 [仓库地址]/[命名空间]/[镜像名]:[版本号]。
[root@localhost ~]# docker tag nginx:latest 192.168.29.210:5000/eden/nginx:1.0
  1. 推送镜像到私有仓库
    打完标签后,将镜像推送到你的私有仓库:
# docker push:将本地镜像上传到指定仓库。
[root@localhost ~]# docker push  192.168.29.210:5000/eden/nginx:1.0
The push refers to repository [192.168.29.210:5000/eden/nginx]
11de3d47036d: Pushed 
16907864a2d0: Pushed 
2bdf51597158: Pushed 
0fc6bb94eec5: Pushed 
eda13eb24d4c: Pushed 
67796e30ff04: Pushed 
8e2ab394fabf: Pushed 
1.0: digest: sha256:596c783ac62b9a43c60edb876fe807376cd5022a4e25e89b9a9ae06c374299d4 size: 1778

  1. 拉取镜像
    从你的私有仓库中拉取该镜像进行测试:
[root@localhost ~]# docker pull 192.168.29.210:5000/eden/nginx:1.0
1.0: Pulling from eden/nginx
Digest: sha256:596c783ac62b9a43c60edb876fe807376cd5022a4e25e89b9a9ae06c374299d4
Status: Image is up to date for 192.168.29.210:5000/eden/nginx:1.0
192.168.29.210:5000/eden/nginx:1.0
[root@localhost ~]# docker images
REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
nginx                            latest    39286ab8a5e1   5 weeks ago     188MB
192.168.29.210:5000/eden/nginx   1.0       39286ab8a5e1   5 weeks ago     188MB
registry                         latest    75ef5b734af4   11 months ago   25.4MB
  1. 查看私有仓库中的镜像列表
    通过 curl 查看私有仓库中有哪些镜像:
[root@localhost ~]# curl 192.168.29.210:5000/v2/_catalog
{"repositories":["eden/nginx"]}

三、增加 Web UI
为了方便管理仓库中的镜像,我们可以为私有仓库增加一个 Web UI。

  1. 拉取 Web UI 镜像
    首先,拉取一个 Docker Registry Web UI 的镜像。这个镜像允许你通过浏览器查看和管理仓库中的镜像。
[root@localhost ~]# docker pull hyper/docker-registry-web
Using default tag: latest
latest: Pulling from hyper/docker-registry-web
04c996abc244: Pull complete 
d394d3da86fe: Pull complete 
bac77aae22d4: Pull complete 
b48b86b78e97: Pull complete 
09b3dd842bf5: Pull complete 
69f4c5394729: Pull complete 
b012980650e9: Pull complete 
7c7921c6fda1: Pull complete 
e20331c175ea: Pull complete 
40d5e82892a5: Pull complete 
a414fa9c865a: Pull complete 
0304ae3409f3: Pull complete 
13effc1a664f: Pull complete 
e5628d0e6f8c: Pull complete 
0b0e130a3a52: Pull complete 
d0c73ab65cd2: Pull complete 
240c0b145309: Pull complete 
f1fd6f874e5e: Pull complete 
40b5e021928e: Pull complete 
88a8c7267fbc: Pull complete 
f9371a03010e: Pull complete 
Digest: sha256:723ffa29aed2c51417d8bd32ac93a1cd0e7ef857a0099c1e1d7593c09f7910ae
Status: Downloaded newer image for hyper/docker-registry-web:latest
docker.io/hyper/docker-registry-web:latest
[root@localhost ~]# docker images
REPOSITORY                       TAG       IMAGE ID       CREATED         SIZE
192.168.29.210:5000/eden/nginx   1.0       39286ab8a5e1   5 weeks ago     188MB
nginx                            latest    39286ab8a5e1   5 weeks ago     188MB
registry                         latest    75ef5b734af4   11 months ago   25.4MB
hyper/docker-registry-web        latest    0db5683824d8   7 years ago     599MB
  1. 启动 Web UI
    启动 Web UI 并连接到你的私有仓库:
#--link registry:将 Web UI 容器与私有仓库容器连接。
#-e REGISTRY_URL:指定私有仓库的地址。
[root@localhost ~]# docker run -d --restart=always -p 8080:8080 --name registry-web --link registry -e REGISTRY_URL=http://192.168.29.210:5000/v2 -e REGISTRY_NAME=localhost:5000 hyper/docker-registry-web
676d60ff47f3491c7c3c76f5894b2a82ce9a63ae719ee23e9193032c719d77ba
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE                       COMMAND                   CREATED          STATUS          PORTS                                       NAMES
676d60ff47f3   hyper/docker-registry-web   "start.sh"                35 seconds ago   Up 32 seconds   0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   registry-web
e88bcb2919ef   registry                    "/entrypoint.sh /etc…"   34 minutes ago   Up 33 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp   registry
  1. 访问 Web UI
    在浏览器中访问 http://192.168.29.210:8080,你将看到私有仓库中的镜像列表。
    在这里插入图片描述
    四、设置带 Basic 认证的仓库
    如果你想要为私有仓库添加认证功能,以下是具体步骤。

  2. 安装 httpd-tools 工具
    首先,安装 httpd-tools 工具来生成认证用户和密码。

[root@localhost ~]# yum install httpd-tools -y
  1. 创建认证文件
    创建认证目录并生成用户名为 admin 、密码为 123456 的认证文件:
[root@localhost ~]# mkdir -p /opt/registry-var/auth/
[root@localhost ~]# htpasswd -Bbn admin 123456 > /opt/registry-var/auth/htpasswd
  1. 启动带认证的私有仓库
    现在,启动带有 Basic 认证功能的私有仓库:
[root@localhost ~]# docker stop registry
registry
#REGISTRY_AUTH=htpasswd:启用 htpasswd 认证。
#REGISTRY_AUTH_HTPASSWD_PATH:认证文件的路径。
#注意 因为前面实验已经有了registry  这里容器取名要为registry2 不要重复 
[root@localhost ~]# docker run -d -p 5000:5000 --restart=always --name registry2 -v /opt/registry-var/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry 
e516541c2d0523faba30cbf65238b8092dab24a1b58b3179f0aa611633d1bad5

  1. 登录并推送镜像
    需要登录私有仓库才能进行镜像操作:
[root@localhost ~]# docker login 192.168.29.210:5000
Username: admin
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

登录成功后,可以正常推送镜像

[root@localhost ~]# docker push 192.168.29.210:5000/eden/nginx:1.0
The push refers to repository [192.168.29.210:5000/eden/nginx]
11de3d47036d: Pushed 
16907864a2d0: Pushed 
2bdf51597158: Pushed 
0fc6bb94eec5: Pushed 
eda13eb24d4c: Pushed 
67796e30ff04: Pushed 
8e2ab394fabf: Pushed 
1.0: digest: sha256:596c783ac62b9a43c60edb876fe807376cd5022a4e25e89b9a9ae06c374299d4 size: 1778

四、查看私有 Docker 仓库中的镜像,可以通过多种方式进行操作,包括使用命令行工具和 Web UI。以下是一些具体的步骤和方法:


curl http://192.168.29.210:5000/v2/_catalog

#查看特定镜像的标签
curl http://192.168.29.210:5000/v2/eden/nginx/tags/list

#使用 Web UI 查看  前提是你已经启动了 docker-registry-web
http://192.168.29.210:8080/

#登录并查看
curl -u admin:123456 http://192.168.29.210:5000/v2/_catalog

原文地址:https://blog.csdn.net/qq_43527128/article/details/142498954

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!