仓库(Repository)是集中存放镜像的地方。公有仓库使用 Docker Hub。本文主要讲解私有仓库使用。
目前 Docker 官方维护了一个公共仓库 Docker Hub。大部分需求都可以通过在 Docker Hub 中直接下载镜像来实现。
在 https://hub.docker.com 免费注册一个 Docker 账号。
登录需要输入用户名和密码,登录成功后,我们就可以从 docker hub 上拉取自己账号下的全部镜像。
#登录
docker login
#退出
docker logout
#拉取镜像
#以 ubuntu 为关键词进行搜索:
docker search ubuntu
#使用 docker pull 将官方 ubuntu 镜像下载到本地:
docker pull ubuntu
#推送镜像
docker tag ubuntu:18.04 username/ubuntu:18.04
docker push username/ubuntu:18.04
在使用 Docker 一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库(如 Dockerhub)
进行管理并不方便;另外有时候只是希望在内部用户之间进行分享,不希望暴露出去。在这种情况下,就有必要搭建一个本地私有镜像仓库。
本文主要介绍Registry ,Registry 是一个无状态的、高度可扩展的服务器端应用程序,它存储并允许您分发 Docker 映像。
官方API的地址:Registry API。
使用手册:Docker Registry。
GitHub地址:https://github.com/docker-archive/docker-registry
基于容器安装运行
docker run -d -p 5000:5000 --restart=always --name registry registry
启动后,服务监听在本地的 5000 端口,可以通过访问 http://Iocalhost:5000/v2/
测试启动成功。
默认的存储位置为/var/lib/registry
, 可以通过-v
参数来映射本地的路径到容器内。例如,下面将镜像存储到本地/opt/data/registry
目录:
docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker run -d -p 5000:5000 --restart=always --name registry registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
213ec9aee27d: Already exists
5299e6f78605: Already exists
4c2fb79b7ce6: Already exists
74a97d2d84d9: Already exists
44c4c74a95e4: Already exists
Digest: sha256:83bb78d7b28f1ac99c68133af32c93e9a1c149bcd3cb6e683a3ee56e312f1c96
Status: Downloaded newer image for registry:latest
a60a46a741239911d98e655a300be666581dcc5362a21a5c05349617cf9d4cef
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry
508da8cc02aa4df34c607fbe9e67f1e250058f2eb7b227551640d51d527a1682
docker: Error response from daemon: driver failed programming external connectivity on endpoint sad_babbage (285a90bebb1309062280ecf1819632e80f9a49721ba641010fc41bf7d295d3d2): Bind for 0.0.0.0:5000 failed: port is already allocated.
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a60a46a74123 registry "/entrypoint.sh /etc…" About a minute ago Up About a minute 0.0.0.0:5000->5000/tcp, :::5000->5000/tcp registry
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker tag hello-world:latest 127.0.0.1:5000/hello-world:latest
docker tag hello-world:latest 127.0.0.1:5000/hello-world:latest
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
admin_web latest 7eae7d20d465 7 days ago 55.5MB
redis alpine 9192ed4e4955 13 days ago 28.5MB
python 3.6-alpine 3a9e80fa4606 8 months ago 40.7MB
hello-world latest feb5d9fea6a5 11 months ago 13.3kB
docker/compose 1.29.2 32d8a4638cd8 15 months ago 76.2MB
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker tag hello-world:latest 127.0.0.1:5000/hello-world:latest
docker push 127.0.0.1:5000/hello-world
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker push 127.0.0.1:5000/hello-world
Using default tag: latest
The push refers to repository [127.0.0.1:5000/hello-world]
e07ee1baac5f: Pushed
latest: digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4 size: 525
curl localhost:5000/v2/_catalog
[root@iZhp33j6fklnmhbf0lz2obZ project]# curl localhost:5000/v2/_catalog
{"repositories":["hello-world"]}
docker pull 127.0.0.1:5000/hello-world:latest
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker image rm 127.0.0.1:5000/hello-world:latest
Untagged: 127.0.0.1:5000/hello-world:latest
Untagged: 127.0.0.1:5000/hello-world@sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker pull 127.0.0.1:5000/hello-world:latest
latest: Pulling from hello-world
Digest: sha256:f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
Status: Downloaded newer image for 127.0.0.1:5000/hello-world:latest
127.0.0.1:5000/hello-world:latest
[root@iZhp33j6fklnmhbf0lz2obZ project]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
admin_web latest 7eae7d20d465 7 days ago 55.5MB
redis alpine 9192ed4e4955 13 days ago 28.5MB
registry latest 3a0f7b0a13ef 13 days ago 24.1MB
python 3.6-alpine 3a9e80fa4606 8 months ago 40.7MB
127.0.0.1:5000/hello-world latest feb5d9fea6a5 11 months ago 13.3kB
hello-world latest feb5d9fea6a5 11 months ago 13.3kB
docker/compose 1.29.2 32d8a4638cd8 15 months ago 76.2MB
点赞 收藏 关注
总有人间一两风 填我十万八千梦。