最近公司准备用docker 管理应用,由于公司是内网环境,镜像只能通过U盘方式导入到内网环境。虽说docker有镜像的导入导出,这里就不再赘述。但是如果想让多个人使用镜像,不用每个人做导入导出,所以存在类似hub.docker这样的应用是非常必要的,现在手把手教你如何搭建Docker Registry私服。
关于本文需要的Docker镜像操作,有五个常用的命令:
a、拉取镜像,不跟意味着仓库从hub.docker.io下载。后跟镜像仓库名称,如果要指定某个版本,可以带上tag,否则默认下载最新版:latest
docker pull [:tag]
b、列出所有镜像,能得到镜像的相关基本信息。
docker@default:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ldapaccountmanager/lam latest ea2e1f34792f 11 days ago 473MB
osixia/openldap latest 4c780dfa5f5e 11 days ago 275MB
192.168.99.100:5000/tomcat 8 8973f493aa0a 3 weeks ago 508MB
nginx latest e445ab08b2be 2 months ago 126MB
registry latest f32a97de94e1 7 months ago 25.8MB
osixia/openldap 1.2.2 1162356984fb 13 months ago 222MB
jenkins latest cd14cecfdb3a 15 months ago 696MB
192.168.99.100:5000/docker-registry-web latest 0db5683824d8 3 years ago 599MB
hyper/docker-registry-web latest 0db5683824d8 3 years ago 599MB
c、删除镜像。可以根据镜像ID,或者镜像仓库名称进行删除。
docker rmi <IMAGE ID>/<repo>
d、镜像打标。可以将此操作与Git打标进行类比,相当于是release一个可用的镜像版本。
docker tag <repo> <new_repo>[:tag]
e、镜像推送。同样是可以借鉴Git领域的push操作,将打包好的镜像推送给远程仓库(即为Docker Registry)。
docker push <new_repo>[:tag]
搭建docker Registry
1.下载registry 镜像
docker@default:~$ docker pull registry:latest
latest: Pulling from library/registry
Digest: sha256:8004747f1e8cd820a148fb7499d71a76d45ff66bac6a29129bfdbfdc0154d146
Status: Image is up to date for registry:latest
docker.io/library/registry:latest
2.将5000端口映射出来,建议应用中涉及数据的部分单独拿出来,方便后续迁移。这是我们通过挂载方式
将/var/lib/registry 映射到本地
docker run -d \
-p 5000:5000 \
-v /mnt/sda1/data/registry:/var/lib/registry \
--restart=always \
--name registry \
registry:latest
3.可以执行
curl http://localhost:5000/v2/_catalog,查看已有的镜像源
curl http://localhost:5000/v2/_catalog
{"repositories":["docker-registry-web","tomcat"]}
因为我挂载过路径,“docker-registry-web”,"tomcat"是之前上传的,第一次装应该是:
{“repositories”:[]}
4.上传本地镜像
先在已下载的镜像打个标签:
docker@default:~$ docker tag osixia/openldap:latest localhost:5000/openldap
docker@default:~$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ldapaccountmanager/lam latest ea2e1f34792f 11 days ago 473MB
osixia/openldap latest 4c780dfa5f5e 11 days ago 275MB
localhost:5000/openldap latest 4c780dfa5f5e 11 days ago 275MB
nginx latest e445ab08b2be 2 months ago 126MB
registry latest f32a97de94e1 7 months ago 25.8MB
osixia/openldap 1.2.2 1162356984fb 13 months ago 222MB
jenkins latest cd14cecfdb3a 15 months ago 696MB
hyper/docker-registry-web latest 0db5683824d8 3 years ago 599MB
使用docker push 命令将镜像推上去
docker@default:~$ docker push localhost:5000/openldap:1.6
The push refers to repository [localhost:5000/openldap]
c1e054ea9d45: Pushed
568aa872bd8c: Pushed
0b197c944734: Pushed
73850ae3a7fa: Pushed
8b349d954bf0: Pushed
c53a68b5c449: Pushed
975729059892: Pushed
917bac618cd7: Pushed
1c95c77433e8: Pushed
1.6: digest: sha256:eaefde9d33b03fe04d081767fda7d3c3500ec861108bbdd19c1b5ceb76e3ee27 size: 2201
curl http://localhost:5000/v2/_catalog,查看已有的镜像源
docker@default:~$ curl http://localhost:5000/v2/_catalog
{"repositories":["docker-registry-web","openldap","tomcat"]}
同一个应用多个版本同样可以上传
docker@default:~$ docker tag osixia/openldap:1.2.2 localhost:5000/openldap:1.2.2
docker@default:~$ docker push localhost:5000/openldap:1.2.2
The push refers to repository [localhost:5000/openldap]
48cbdf93727e: Pushed
c6894eace047: Pushed
7c14592fd5a1: Pushed
b87f1a44d0b3: Pushed
49e27c1e677c: Pushed
8a72149f7317: Pushed
25751a7c5182: Pushed
cdb3f9544e4c: Pushed
1.2.2: digest: sha256:34661cfeffa84d925eb3d15a835d8836fbccf3daba3459e930f8b16e35cfeb1f size: 1994
docker@default:~$ curl http://localhost:5000/v2/_catalog
{"repositories":["docker-registry-web","openldap","tomcat"]}
但看不出同一个应用有哪些版本
通过查挂载盘信息看出来确实有多个版本已经上传
/mnt/sda1/data/registry/docker/registry/v2/repositories/openldap/_manifests/tags$ ls
1.2.2 1.6