docker 仓库

时间:2022-07-22
本文章向大家介绍docker 仓库,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

仓库介绍

仓库是集中存放镜像的地方。

Docker Hub 是docker官方维护了一个公共仓库,大部分需求都可以在Docker Hub上直接下载镜像

拉取镜像

可以通过docker searchdocker pull命令来拉取镜像

1 2 3 4 5 6 7 8 9 10 11

[root@xs_test01 docker]# docker search redis NAME(名字) DESCRIPTION(描述) STARS(受关注度) OFFICIAL(是否官方创建) AUTOMATED(是否自动创建) redis Redis is an open source key-value store that… 4879 [OK] bitnami/redis Bitnami Redis Docker Image 70 [OK] sameersbn/redis 64 [OK] tenstartups/redis-commander 32 [OK] hypriot/rpi-redis Raspberry Pi compatible redis image 30 joshula/redis-sentinel A container for Redis Sentinel 21 kubeguide/redis-master redis-master with "Hello World!" 19 kubeguide/guestbook-redis-slave Guestbook redis slave 16 webhippie/redis Docker images for redis 8 [OK]

1 2 3 4 5

[root@xs_test01 docker]# docker pull redis Using default tag: latest latest: Pulling from library/redis Digest: sha256:e55dff3a21a0e7ba25e91925ed0d926d959dac09f9099fd1bcc919263305f1e4 Status: Image is up to date for redis:latest

推送镜像

用户可以通过docker push命令来将自己的镜像推送到Docker Hub

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

登陆 #docker login 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: devilf Password: Login Succeeded 使用Docker Hub账户的用户名打一个标签 #docker tag ubuntu:17.04 devilf/ubuntu:17.04 查看# docker images 推送 #docker push devilf/ubuntu:17.04 The push refers to repository [docker.io/devilf/ubuntu] 3ff70ce53dac: Pushed b8e5935ae7cc: Pushed ba76b502dc9b: Pushed 803030df23c1: Pushed db8686e0ca43: Pushed 17.04: digest: sha256:213e05583a7cb8756a3f998e6dd65204ddb6b4c128e2175dcdf174cdf1877459 size: 1357 搜索 #docker search devilf NAME DESCRIPTION STARS OFFICIAL AUTOMATED devilflake/get-started 0

私有仓库

安装docker-registry

1 2 3 4 5 6 7 8 9 10 11 12 13

第一次启动时报错: [root@xs_test01 docker]# docker run -d -p 5000:5000 --restart=always --name registry registry 36ebd0311366dc9ba3faa8afa42c9e796a1f4c72f5bd870c40404207ef2d01c5 docker: Error response from daemon: driver failed programming external connectivity on endpoint registry (f4e440a237fa6fc0b6e727d9a4dacf5140f02abc330a83e1884df81f5e1c4f93): (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 5000 -j DNAT --to-destination 172.17.0.3:5000 ! -i docker0: iptables: No chain/target/match by that name. (exit status 1)). 原因是因为centos7上的firewalld防火墙给关闭了,使用的是iptables,手动打开firewall即可正常启动。 第二次启动正常: [root@xs_test01 docker]# docker run -d -p 5000:5000 --restart=always --name registry registry 01bec932a1107403ad929633941bbf654bc89d74f73df68ec2f7a49a9f031465 [root@xs_test01 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 01bec932a110 registry "/entrypoint.sh /etc…" 4 seconds ago Up 2 seconds 0.0.0.0:5000->5000/tcp registry 1cd1fadb254b ubuntu:17.04 "/bin/sh -c 'while t…" 3 hours ago Up 3 hours kind_wilson

默认使用官方的registry镜像来启动私有仓库,会被放到/var/lib/registry目录下,我们可以通过-v参数来将镜像文件存放在本地的指定路径。

1 2 3 4 5 6

[root@xs_test01 docker]# docker run -d -p 5000:5000 -v /opt/data/registry:/var/lib/registry registry eab05e17db69f8fc728dec4dd579d7e7e523b5ac64eee83302ba2c8965d25dfc [root@xs_test01 docker]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES eab05e17db69 registry "/entrypoint.sh /etc…" 4 seconds ago Up 4 seconds 0.0.0.0:5000->5000/tcp hopeful_bose 1cd1fadb254b ubuntu:17.04 "/bin/sh -c 'while t…" 3 hours ago Up 3 hours kind_wilson

本地是没有/opt/data/registry这个目录的,使用-v参数会自动创建目录

在私有仓库上传、搜索、下载镜像

创建好私有仓库后,我们使用docker tag来标记一个镜像,然后推送到仓库

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

打标签 [root@xs_test01 registry]# docker tag ubuntu:14.04 127.0.0.1:5000/ubuntu:14.04 查看打完标签后的镜像 [root@xs_test01 registry]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu.tar latest a56d3f7377d4 3 hours ago 80MB openvz/ubuntu 14.04 4fb9933d51b0 5 hours ago 215MB ubuntu 14.04 a35e70164dfb 7 days ago 222MB 127.0.0.1:5000/ubuntu 14.04 a35e70164dfb 7 days ago 222MB devilf/ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB hello-world latest f2a91732366c 3 months ago 1.85kB 将做好标签的镜像推送到私有仓库中 [root@xs_test01 registry]# docker push 127.0.0.1:5000/ubuntu:14.04 The push refers to repository [127.0.0.1:5000/ubuntu] 65262d4f5516: Pushed dd420adea0d3: Pushed 3abb69fb15dc: Pushed e95051d9cb9b: Pushed 92fb50b4d953: Pushed 14.04: digest: sha256:0661e2dbc6072eadfd48c0c972681ffc11ca8ea5d0a9f5f4227df7d653f649fd size: 1359 使用curl命令查看仓库中的镜像 [root@xs_test01 registry]# curl 127.0.0.1:5000/v2/_catalog {"repositories":["ubuntu"]} 删除已有的镜像 [root@xs_test01 registry]# docker image rm 127.0.0.1:5000/ubuntu:14.04 Untagged: 127.0.0.1:5000/ubuntu:14.04 Untagged: 127.0.0.1:5000/ubuntu@sha256:0661e2dbc6072eadfd48c0c972681ffc11ca8ea5d0a9f5f4227df7d653f649fd [root@xs_test01 registry]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu.tar latest a56d3f7377d4 3 hours ago 80MB openvz/ubuntu 14.04 4fb9933d51b0 5 hours ago 215MB ubuntu 14.04 a35e70164dfb 7 days ago 222MB entrypoint latest 10c7252261f8 8 days ago 4.82MB myip latest 7f77fc2f30b7 8 days ago 307MB nginx v3 7386045ee352 9 days ago 109MB nginx v2 ada0bc75fda7 9 days ago 109MB nginx latest e548f1a579cf 3 weeks ago 109MB redis latest 33c26d72bd74 3 weeks ago 107MB registry latest d1fd7d86a825 2 months ago 33.3MB alpine 3.4 c7fc7faf8c28 2 months ago 4.82MB centos latest ff426288ea90 2 months ago 207MB devilf/ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB hello-world latest f2a91732366c 3 months ago 1.85kB 可以看出之前打好标签的镜像确实已经删除 从私有仓库中拉取镜像 [root@xs_test01 registry]# docker pull 127.0.0.1:5000/ubuntu:14.04 14.04: Pulling from ubuntu Digest: sha256:0661e2dbc6072eadfd48c0c972681ffc11ca8ea5d0a9f5f4227df7d653f649fd Status: Downloaded newer image for 127.0.0.1:5000/ubuntu:14.04 [root@xs_test01 registry]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu.tar latest a56d3f7377d4 3 hours ago 80MB openvz/ubuntu 14.04 4fb9933d51b0 5 hours ago 215MB 127.0.0.1:5000/ubuntu 14.04 a35e70164dfb 7 days ago 222MB ubuntu 14.04 a35e70164dfb 7 days ago 222MB entrypoint latest 10c7252261f8 8 days ago 4.82MB myip latest 7f77fc2f30b7 8 days ago 307MB nginx v3 7386045ee352 9 days ago 109MB nginx v2 ada0bc75fda7 9 days ago 109MB nginx latest e548f1a579cf 3 weeks ago 109MB redis latest 33c26d72bd74 3 weeks ago 107MB registry latest d1fd7d86a825 2 months ago 33.3MB alpine 3.4 c7fc7faf8c28 2 months ago 4.82MB centos latest ff426288ea90 2 months ago 207MB devilf/ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB ubuntu 17.04 fe1cc5b91830 2 months ago 95.6MB hello-world latest f2a91732366c 3 months ago 1.85kB