全国直销电话:4006-854-568
IT-technology
以人为本,众志成城,以“用户至上”.“服务上乘”为原则,
追求产品和服务高质量,努力实现与客户之间真诚有效的沟通,
不断地圆梦、奔跑与腾飞。
新闻动态   NEWS
【容器专题7】Docker仓库(Repository)使用命令及创建私有仓库-北京赛维博信科技发展有限公司
来源:本文摘自网络,如有侵权请联系删除 | 作者:svbx001 | 发布时间: 2023-03-03 | 1660 次浏览 | 分享到:

1.什么是Docker仓库?

Docker仓库(Repository)是集中存放Docker镜像的地方,它类似与代码仓库。

根据存储的镜像公开分享与否,Docker仓库分为两种形式:

  • 公开仓库(Public)

  • 私有仓库(Private)

目前,最大的公开仓库是官方的Docker Hub,存放了数量庞大的镜像供用户下载。国内的公开仓库包括Docker Pool等,可以提供稳定的国内访问。如果用户不希望公开分享自己的镜像文件,Docker也支持用户在本地网络内创建一个只能自己访问的私有仓库。当用户创建了自己的镜像之后就可以使用push将它上传到指定的公有或则私有仓库。这样用户下次在另一台机器上使用该镜像时,只需将其从仓库pull下来就可以了。

这里需要注意理解Docker RepositoryDocker Registry两者的区别:

项目Docker RepositoryDocker Registry
中文含义docker仓库docker注册服务器
功能特点1)集中存放某一类镜像
2)每个镜像文件通过不同的标签tag来进行区分
1)存放仓库的地方
2)一个注册服务器可以存放多个仓库

例如存放Centos操作系统镜像的仓库,称为Centos仓库,其中可能包括不同版本的镜像。

2.Docker Hub公共仓库

https://hub.docker.com/

目前Docker官方维护了一个公共仓库Docker Hub,其中已经包括了数量超过15,000的镜像。大部分需求都可以通过在Docker Hub中直接下载镜像来实现。可以在docker hub上免费注册一个Docker账号。

然后通过执行docker login -u命令交互式的输入用户名及密码来完成在命令行界面登录Docker Hub。可以通过 docker logout 退出登录。

[root@newdocker2 ~]# docker login
Authenticating with existing credentials...
Stored credentials invalid or expired
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (moonrong): 
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@newdocker2 ~]# 

可以通过docker search命令来查找官方仓库中的镜像,并利用docker pull命令来将它下载到本地。

[root@newdocker2 ~]# docker search centos
NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                                       DEPRECATED; The official build of CentOS.       7516      [OK]       
kasmweb/centos-7-desktop                     CentOS 7 desktop for Kasm Workspaces            33                  
……

可以看到返回了很多包含关键字的镜像,其中包括镜像名字、描述、收藏数、是否官方创建、是否自动创建。OFFICIAL官方的镜像说明是官方项目组创建和维护的,AUTOMATED 资源允许用户验证镜像的来源和内容。根据是否是官方提供,可将镜像资源分为两类:

一种是类似centos这样的镜像,被称为基础镜像根镜像。这些基础镜像由Docker公司创建、验证、支持、提供。这样的镜像往往使用单个单词作为名字。

还有一种类型,比如dokken/centos-7镜像,它是由Docker的用户创建并维护的,往往带有用户名称前缀。可以通过**前缀username/**来指定使用某个用户提供的镜像,比如这里是dokken用户。

2.1拉取镜像到本地

前面找到需要的镜像后,就可以下载(拉取)到本地,如下两例:

[root@newdocker2 ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Status: Image is up to date for centos:latest
docker.io/library/centos:latest
[root@newdocker2 ~]# 

[root@newdocker2 ~]# docker pull couchbase/centos7-systemd
Using default tag: latest
latest: Pulling from couchbase/centos7-systemd
a02a4930cb5d: Pull complete 
c702ea4a22bc: Pull complete 
5742391e1f8f: Pull complete 
Digest: sha256:cb7ea664010ab553676c7f0c63fc28562ed990d3b35087e716f5d4089fc49e5a
Status: Downloaded newer image for couchbase/centos7-systemd:latest
docker.io/couchbase/centos7-systemd:latest
[root@newdocker2 ~]# 
2.2将镜像推送到Docker hub

前面我们已通过用户moonrong登录到Docker hub上,这样可将自己做的镜像上传(推送)Docker hub

先查看本地镜像:

[root@newdocker2 ~]# docker image ls
REPOSITORY                  TAG        IMAGE ID       CREATED         SIZE
nginx                       1.23.3     3f8a00f137a0   3 weeks ago     142MB
redis                       latest     2e50d70ba706   8 months ago    117MB
nginx                       <none>     55f4b40fe486   8 months ago    142MB
busybox                     latest     62aedd01bd85   8 months ago    1.24MB
ubuntu                      latest     27941809078c   8 months ago    77.8MB
nginx                       <none>     0e901e68141f   9 months ago    142MB
nginx                       latest     605c77e624dd   14 months ago   141MB
centos                      7.9.2009   eeb6ee3f44bd   17 months ago   204MB
centos                      latest     5d0da3dc9764   17 months ago   231MB
couchbase/centos7-systemd   latest     a7192f6d537a   4 years ago     411MB
[root@newdocker2 ~]# 

然后将nginx:1.23.3镜像通过tag将其标记为moonrong/nginx:1.23.3

[root@newdocker2 ~]# docker tag nginx:1.23.3 moonrong/nginx:1.23.3
[root@newdocker2 ~]# 

再列出本地镜像:

[root@newdocker2 ~]# docker image ls -a
REPOSITORY                  TAG        IMAGE ID       CREATED         SIZE
nginx                       1.23.3     3f8a00f137a0   3 weeks ago     142MB
moonrong/nginx              1.23.3     3f8a00f137a0   3 weeks ago     142MB
redis                       latest     2e50d70ba706   8 months ago    117MB
nginx                       <none>     55f4b40fe486   8 months ago    142MB
busybox                     latest     62aedd01bd85   8 months ago    1.24MB
ubuntu                      latest     27941809078c   8 months ago    77.8MB
nginx                       <none>     0e901e68141f   9 months ago    142MB
nginx                       latest     605c77e624dd   14 months ago   141MB
centos                      7.9.2009   eeb6ee3f44bd   17 months ago   204MB
centos                      latest     5d0da3dc9764   17 months ago   231MB
couchbase/centos7-systemd   latest     a7192f6d537a   4 years ago     411MB
[root@newdocker2 ~]# 

现在第二行多了一个仓库名为:moonrong/nginx,TAG为 3f8a00f137a0 ,创建时间为3周前,大小为142MB的镜像。

接着用docker push将其推送到docker hub上:

[root@newdocker2 ~]# docker push moonrong/nginx:1.23.3
The push refers to repository [docker.io/moonrong/nginx]
3ea1bc01cbfe: Preparing 
a76121a5b9fd: Preparing 
2df186f5be5c: Preparing 
21a95e83c568: Preparing 
81e05d8cedf6: Preparing 
4695cdfb426a: Waiting 
……
3ea1bc01cbfe: Mounted from library/nginx 
a76121a5b9fd: Mounted from library/nginx 
2df186f5be5c: Mounted from library/nginx 
21a95e83c568: Mounted from library/nginx 
81e05d8cedf6: Mounted from library/nginx 
4695cdfb426a: Mounted from library/nginx 
1.23.3: digest: sha256:7f797701ded5055676d656f11071f84e2888548a2e7ed12a4977c28ef6114b17 size: 1570
[root@newdocker2 ~]#

然后输入搜索命令查找docker hub上moonrong用户的镜像:

[root@newdocker2 ~]# docker search moonrong
NAME                  DESCRIPTION   STARS     OFFICIAL   AUTOMATED
moonrong/test-nginx                 0                    
moonrong/hello                      0                    
moonrong/nginx                      0                    
[root@newdocker2 ~]# 

或者WEB页也能看到:

3.Docker私有仓库创建

很多时候,通过容器部署的应用以后要在其他机子上部署,如果直接将生成的镜像提交到docker hub上很不安全,也不方便使用,为此,Docker官方提供了docker-registry工具 ,可以用于构建私有的镜像仓库。

3.1创建私有仓库

这里可以通过官方registry镜像来运行一个私有仓库:

[root@newdocker2 /]# docker run -d -p 5000:5000 -v /myrepository:/var/lib/registry registry
66e39749e6f291a5df613d27ecaab7341911d57b96ff423da52ad4290e57eeb0
[root@newdocker2 /]# 

默认情况下,仓库会被创建在容器的/var/lib/registry目录下,这里指定为/myrepository目录下存放本地仓库镜像文件。

3.2标记镜像

创建好私有仓库之后,就可以使用 docker tag 来标记一个镜像,然后推送它到仓库。例如私有仓库地址为127.0.0.1:5000,先在本机查看已有的镜像。

[root@newdocker2 /]# docker image ls -a
REPOSITORY                  TAG        IMAGE ID       CREATED         SIZE
nginx                       1.23.3     3f8a00f137a0   3 weeks ago     142MB
moonrong/nginx              1.23.3     3f8a00f137a0   3 weeks ago     142MB
redis                       latest     2e50d70ba706   8 months ago    117MB
nginx                       <none>     55f4b40fe486   8 months ago    142MB
busybox                     latest     62aedd01bd85   8 months ago    1.24MB
ubuntu                      latest     27941809078c   8 months ago    77.8MB
nginx                       <none>     0e901e68141f   9 months ago    142MB
nginx                       latest     605c77e624dd   14 months ago   141MB
registry                    2          b8604a3fe854   15 months ago   26.2MB
registry                    latest     b8604a3fe854   15 months ago   26.2MB
centos                      7.9.2009   eeb6ee3f44bd   17 months ago   204MB
centos                      latest     5d0da3dc9764   17 months ago   231MB
couchbase/centos7-systemd   latest     a7192f6d537a   4 years ago     411MB
[root@newdocker2 /]# 

使用docker tagredis:latest这个镜像标记为127.0.0.1:5000/redis:v2

[root@newdocker2 /]# docker tag redis:latest 127.0.0.1:5000/redis:v2
[root@newdocker2 /]# 

接着查看标记结果:

[root@newdocker2 /]# docker image ls 127.0.0.1:5000/redis
REPOSITORY             TAG       IMAGE ID       CREATED        SIZE
127.0.0.1:5000/redis   v2        2e50d70ba706   8 months ago   117MB
[root@newdocker2 /]# 
3.3上传已标记的镜像

同样,用docker push推送镜像:

[root@newdocker2 /]# docker push 127.0.0.1:5000/redis:v2
The push refers to repository [127.0.0.1:5000/redis]
be56018ff479: Pushed 
58bcc523fc92: Pushed 
4ca33072d026: Pushed 
cf3ae502d7fa: Pushed 
5659b3a1146e: Pushed 
08249ce7456a: Pushed 
v2: digest: sha256:31120dcdd310e9a65cbcadd504f4fe60a185bd634ab7c6a35e3e44a941904d97 size: 1573
[root@newdocker2 /]# 

这里已上传到私有仓库的目录下,可以用tree命令查看目录结构:

[root@newdocker2 /]# tree /myrepository/
/myrepository/
└── docker
    └── registry
        └── v2
            ├── blobs
            │   └── sha256
            │       ├── 2e
            │       │   └── 2e50d70ba706ed644d475612b998641687dfb2415016b803ca3eb811c7096714
            │       │       └── data
            │       ├── 31
            │       │   └── 31120dcdd310e9a65cbcadd504f4fe60a185bd634ab7c6a35e3e44a941904d97
            │       │       └── data
            │       ├── 58
            │       │   └── 58024fcab1ef56e1c0d86a80b3abeab24bf1c6042573eab7645eb11e16ccdca8
            │       │       └── data
            │       ├── 95
            │       │   └── 954286b64dd1be62e03bc13e93fb8d477476e40a0b076bf4c1f9712caf20b9e8
            │       │       └── data
            │       ├── b0
            │       │   └── b09642bd3b882657a3d239f48a3f44f0c07e7776a243a575b93504f1f9b49a43
            │       │       └── data
            │       ├── b8
            │       │   └── b85a868b505ffd0342a37e6a3b1c49f7c71878afe569a807e6238ef08252fcb7
            │       │       └── data
            │       ├── d5
            │       │   └── d5d7c0a1681bceddc89c27f660ace9c824dd5591fff81cf3bfd1603d7bb50d15
            │       │       └── data
            │       └── e0
            │           └── e0678a951c8d72b1b826c6d76742580957d64c02081823e69b104971e8c65dcf
            │               └── data
            └── repositories
                └── redis
                    ├── _layers
                    │   └── sha256
                    │       ├── 2e50d70ba706ed644d475612b998641687dfb2415016b803ca3eb811c7096714
                    │       │   └── link
                    │       ├── 58024fcab1ef56e1c0d86a80b3abeab24bf1c6042573eab7645eb11e16ccdca8
                    │       │   └── link
                    │       ├── 954286b64dd1be62e03bc13e93fb8d477476e40a0b076bf4c1f9712caf20b9e8
                    │       │   └── link
                    │       ├── b09642bd3b882657a3d239f48a3f44f0c07e7776a243a575b93504f1f9b49a43
                    │       │   └── link
                    │       ├── b85a868b505ffd0342a37e6a3b1c49f7c71878afe569a807e6238ef08252fcb7
                    │       │   └── link
                    │       ├── d5d7c0a1681bceddc89c27f660ace9c824dd5591fff81cf3bfd1603d7bb50d15
                    │       │   └── link
                    │       └── e0678a951c8d72b1b826c6d76742580957d64c02081823e69b104971e8c65dcf
                    │           └── link
                    ├── _manifests
                    │   ├── revisions
                    │   │   └── sha256
                    │   │       └── 31120dcdd310e9a65cbcadd504f4fe60a185bd634ab7c6a35e3e44a941904d97
                    │   │           └── link
                    │   └── tags
                    │       └── v2
                    │           ├── current
                    │           │   └── link
                    │           └── index
                    │               └── sha256
                    │                   └── 31120dcdd310e9a65cbcadd504f4fe60a185bd634ab7c6a35e3e44a941904d97
                    │                       └── link
                    └── _uploads

43 directories, 18 files
[root@newdocker2 /]# 
3.4修改非https方式推送镜像的问题

有时候,建立的私有仓库需要需要局域网内团队成员使用,这时候127.0.0.0:5000就不能访问,为此,需要改成真实IP地址,比如192.168.250.239:5000形式作为仓库地址,但是这里遇到一个问题,就是无法成功推送镜像,这时候需要修改/etc/docker/daemon.json文件:

[root@newdocker2 /]# cat /etc/docker/daemon.json
{
  "registry-mirror": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "192.168.250.239:5000"
  ]
}
[root@newdocker2 /]# 

然后重启docker。


 

服务热线

1391-024-6332