仓库(Repository)是集中存放镜像的地方。又分为公有仓库(public)和私有仓库(private)
一个容易混淆的的概念是注册服务器(Registry)。实际上注册服务器是存放仓库的具体服务器,每个服务器上都有很多个仓库,而每个仓库下面都有多个镜像。从这方面来说,仓库可以认为是一个具体的项目或目录。例如对于仓库地址dl.dockerpool.com/centos来说,dl.dockerpool.com是注册服务器地址,centos是仓库名。
1、Docker Hub 公共镜像市场
Docker Hub是官方提供的最大的公共镜像仓库,对于大部分需求都可以通过Docker Hub中直接下载来实现,首先需要注册一个账号,docker login 登录之后可以上传个人制作的镜像到Docker Hub。
2、第三方镜像市场
国内云服务商基本都提供了Docker镜像市场,以时速云为例,下载镜像需要在名称前面添加注册服务器的具体地址。
docker pull index.tenxcloud.com/docker_library/nginx
3、搭建本地私有仓库
安装Docker后,可以通过官方提供的registry镜像来简单搭建一套本地私有仓库环境。
以registry镜像启动容器,-p会把容器的端口映射到宿主机上,:左边为宿主机监听端口,:右边为容器监听端口
#这里将自动下载registry镜像并启动容器,创建本地私有仓库。
[[email protected] ~]# docker run -d -p 5000:5000 registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
d6a5679aa3cf: Pull complete
ad0eac849f8f: Pull complete
2261ba058a15: Pull complete
f296fda86f10: Pull complete
bcd4a541795b: Pull complete
Digest: sha256:5a156ff125e5a12ac7fdec2b90b7e2ae5120fa249cf62248337b6d04abc574c8
Status: Downloaded newer image for registry:latest
43ba02ffae514e0c7c855c1a53a5175d1ffda5533f19e123ee237c793bbca4f5
[[email protected] ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43ba02ffae51 registry "/entrypoint.sh /etc…" About a minute ago Up About a minute
0.0.0.0:5000->5000/tcp dazzling_chatterjee
默认的情况下,会将仓库创建在容器的/var/lib/registry目录下,可以通过-v参数来将镜像文件存放在本地的指定路径上
[[email protected] ~]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry 10a42d43c3e063befe86437a4de2fd766aa2be1a2f2c81fca31d561c437803c4 本地将启动一个私有仓库服务,监听端口:5000 tcp6 0 0 :::5000 :::* LISTEN 17639/docker-proxy
在另外一台机器上测试上传下载镜像
[[email protected] ~]## docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx 1.14.1 8552ee3809ee 8 days ago 109MB
#使用tag标记下镜像,格式为IP:PORT USERNAME/NAME:TAG
[[email protected] ~]# docker tag nginx:1.14.1 139.159.143.74:5000/test/nginx:1.14.1
#上传标记的镜像
[[email protected] ~]# docker push 139.159.143.74:5000/test/nginx:1.14.1
The push refers to repository [139.159.143.74:5000/test/nginx]
Get https://139.159.143.74:5000/v2/: http: server gave HTTP response to HTTPS client
由于新版的docker对安全性比较高,会要求仓库支持SSL/TLS。对于内部使用的仓库,可以自行配置证书或者关闭对仓库的安全性检查,处理方法:更改配置文件:
vim /usr/lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd --insecure-registry 139.159.143.74:5000 修改完配置文件需要进行重启docker [[email protected] ~]#systemctl daemon-reload && systemctl restart docker #上传镜像 [[email protected] ~]# docker push 139.159.143.74:5000/test/nginx:1.14.1 The push refers to repository [139.159.143.74:5000/test/nginx] aa35a8f6772c: Pushed 4291995119da: Pushed 237472299760: Pushed 1.14.1: digest:sha256:e206d21e43c255647beb9ef6948ee0ca3fb912b2a872a005d87ab7c60cc5428b size:948
#查询镜像
#查询镜像 curl <仓库地址>/v2/_catalog [[email protected] ~]# curl http://139.159.143.74:5000/v2/_catalog {"repositories":["test/nginx"]} #查询镜像tag(版本) curl <仓库地址>/v2/<镜像名>/tags/list [[email protected] ~]# curl http://139.159.143.74:5000/v2/test/nginx/tags/list {"name":"test/nginx","tags":["1.14.1"]}
其他Docker服务器下载,删除镜像
#下载镜像
docker pull 139.159.143.74:5000/test/nginx:1.14.1
#删除镜像
[[email protected] ~]# docker rmi 139.159.143.74:5000/test/nginx:1.14.1
Untagged: 139.159.143.74:5000/test/nginx:1.14.1
Untagged: 139.159.143.74:5000/test/[email protected]:e206d21e43c255647beb9ef6948ee0ca3fb912b2a872a005d87ab7c60cc5428b