1. 程式人生 > >docker 實踐(五)公共倉庫及私有倉庫

docker 實踐(五)公共倉庫及私有倉庫

一、docker倉庫說明

Docker registries 可以上傳和下載image,有共有和私有兩種。共有就是Docker Hub,私有就是自建一個倉庫使用。


二、docker hub使用

2.1.註冊使用者

註冊地址:https://hub.docker.com


2.2.登陸上傳映象

2.2.1.檢視映象

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx-centos        latest              d6f680e9e2f5        3 hours ago         442MB
cent-ckl-http       latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng       latest              248da05fb49e        2 days ago          333MB
centos-vim          latest              82fd472e3387        2 days ago          327MB
cent-vim            latest              4659d1ec001d        2 days ago          225MB
centos              latest              1e1148e4cc2c        11 days ago         202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB

2.2.2.上傳之前自定義的映象

命令補全:yum install bash-completion


登陸docker hub:

# 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: ckl
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


為映象打tag,為了區分不同的使用者映象,docker hub使用 '使用者名稱/映象名' 的格式,否則上傳失敗:

# docker push nginx-centos       
The push refers to repository [docker.io/library/nginx-centos]
910b14919137: Preparing 
64f9c904f716: Preparing 
e7ae32451785: Preparing 
25a6d58a35a2: Preparing 
0d57e2a37c8a: Preparing 
071d8bd76517: Waiting 
denied: requested access to the resource is denied

打映象tag:

docker tag nginx-centos ckl893/nginx-centos


上傳映象:

[[email protected] ~]# docker push ckl893/nginx-centos
The push refers to repository [docker.io/ckl893/nginx-centos]
910b14919137: Pushed 
64f9c904f716: Pushed 
e7ae32451785: Pushed 
25a6d58a35a2: Pushed 
0d57e2a37c8a: Pushed 
071d8bd76517: Pushed 
latest: digest: sha256:52ae885b133c966a059bcb9cf8031dd264c75c0ad5f4d08a8599aad6dcb85952 size: 1577

2.2.3.刪除本地映象,下載上傳的映象

刪除本地映象:

docker rmi d6f680e9e2f5


# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
cent-ckl-http       latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng       latest              248da05fb49e        2 days ago          333MB
centos-vim          latest              82fd472e3387        2 days ago          327MB
cent-vim            latest              4659d1ec001d        3 days ago          225MB
centos              latest              1e1148e4cc2c        11 days ago         202MB
nginx               latest              568c4670fa80        2 weeks ago         109MB
ubuntu              latest              93fd78260bd1        3 weeks ago         86.2MB
busybox             latest              59788edf1f3e        2 months ago        1.15MB


下載已經上傳的映象:

 docker pull ckl893/nginx-centos
Using default tag: latest
latest: Pulling from ckl893/nginx-centos
a02a4930cb5d: Already exists 
b4294eb07bed: Pull complete 
b81c82ea9ee0: Pull complete 
d505886c9b92: Pull complete 
56a178b649f6: Pull complete 
7653590c7334: Pull complete 
Digest: sha256:52ae885b133c966a059bcb9cf8031dd264c75c0ad5f4d08a8599aad6dcb85952
Status: Downloaded newer image for ckl893/nginx-centos:latest
# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ckl893/nginx-centos   latest              d6f680e9e2f5        5 hours ago         442MB
cent-ckl-http         latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng         latest              248da05fb49e        2 days ago          333MB
centos-vim            latest              82fd472e3387        2 days ago          327MB
cent-vim              latest              4659d1ec001d        3 days ago          225MB
centos                latest              1e1148e4cc2c        11 days ago         202MB
nginx                 latest              568c4670fa80        2 weeks ago         109MB
ubuntu                latest              93fd78260bd1        3 weeks ago         86.2MB
busybox               latest              59788edf1f3e        2 months ago        1.15MB


三、私有倉庫(本地倉庫)

3.1.下載registry映象

# docker search registry
NAME                                    DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
registry                                The Docker Registry 2.0 implementation for s…   2363                [OK]                
konradkleine/docker-registry-frontend   Browse and modify your Docker registry in a …   211                                     [OK]
hyper/docker-registry-web               Web UI, authentication service and event rec…   156                                     [OK]
atcol/docker-registry-ui                A web UI for easy private/local Docker Regis…   111                                     [OK]
distribution/registry                   WARNING: NOT the registry official image!!! …   56                                      [OK]
....
# docker pull registry
Using default tag: latest
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

3.2.搭建本地倉庫

3.2.1.搭建倉庫

# docker run -d -v /home/ckl/registry:/var/lib/registry -p 5000:5000 --restart=always --privileged=true --name ckl-registry registry:latest 

-v /home/ckl/registry:/var/lib/registry 預設情況下,會將倉庫存放於容器內的/var/lib/registry目錄下,指定本地目錄掛載到容器。
-p 5000:5000 埠對映
--restart=always1 在容器退出時總是重啟容器,主要應用在生產環境
--privileged=true 在CentOS7中的安全模組selinux把許可權禁掉了,引數給容器加特權,不加上傳映象會報許可權錯誤OSError: [Errno 13] Permission denied: ‘/tmp/registry/repositories/liibrary’)或者(Received unexpected HTTP status: 500 Internal Server Error)錯誤
--name ckl-registry 指定容器的名稱

COMMAND_FAILED: '/sbin/iptables -t nat -A DOCKER -p tcp -d 0/0 --dport 8111 -j DNAT --to-destination 172.17.0.6:8111 ! -i docker0' failed: iptables: No chain/target/match by that name.

解決:

pkill docker
iptables -t nat -F
ifconfig docker0 down
brctl delbr docker0
service docker restart

再次執行成功:

# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
37803352e3fa        registry:latest     "/entrypoint.sh /etc…"   4 seconds ago       Up 2 seconds        0.0.0.0:5000->5000/tcp   ckl-registry

3.2.2.為想上傳的映象打tag:

# docker tag cent-ckl-http 192.168.2.120:5000/cent-ckl-http  #一定要帶埠,不然,不知道傳哪裡

3.2.3.上傳映象到本地倉庫:

# docker push 192.168.2.120:5000/cent-ckl-http
The push refers to repository [192.168.2.120:5000/cent-ckl-http]
Get https://192.168.2.120:5000/v2/: dial tcp 192.168.2.120::5000: connect: connection refused

解決:

# vim /usr/lib/systemd/system/docker.service 
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.2.120:5000  -H unix://

重啟docker:

# systemctl daemon-reload 
# systemctl restart docker.service


再次上傳成功:

# docker push 192.168.2.120:5000/cent-ckl-http
The push refers to repository [192.168.2.120:5000/cent-ckl-http]
99bfa91f28fa: Pushed 
dd54c57d2f82: Pushed 
56a34fc853e9: Pushed 
071d8bd76517: Pushed 
latest: digest: sha256:70cf9a97343265e191054268245fd77b18c5da7a8fe66168702c96711ba4ef0e size: 1163

3.2.4.刪除本地映象測試

# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
ckl893/nginx-centos                latest              d6f680e9e2f5        6 hours ago         442MB
192.168.2.120:5000/cent-ckl-http   latest              d3ec54e41d03        2 days ago          443MB
cent-ckl-http                      latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng                      latest              248da05fb49e        3 days ago          333MB
centos-vim                         latest              82fd472e3387        3 days ago          327MB
cent-vim                           latest              4659d1ec001d        3 days ago          225MB
centos                             latest              1e1148e4cc2c        11 days ago         202MB
nginx                              latest              568c4670fa80        2 weeks ago         109MB
ubuntu                             latest              93fd78260bd1        3 weeks ago         86.2MB
busybox                            latest              59788edf1f3e        2 months ago        1.15MB
registry                           latest              2e2f252f3c88        3 months ago        33.3MB

刪除本地映象:

# docker rmi 192.168.2.120:5000/cent-ckl-http
Untagged: 192.168.2.120:5000/cent-ckl-http:latest
Untagged: 192.168.2.120:5000/[email protected]:70cf9a97343265e191054268245fd77b18c5da7a8fe66168702c96711ba4ef0e
# docker images
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
ckl893/nginx-centos   latest              d6f680e9e2f5        6 hours ago         442MB
cent-ckl-http         latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng         latest              248da05fb49e        3 days ago          333MB
centos-vim            latest              82fd472e3387        3 days ago          327MB
cent-vim              latest              4659d1ec001d        3 days ago          225MB
centos                latest              1e1148e4cc2c        11 days ago         202MB
nginx                 latest              568c4670fa80        2 weeks ago         109MB
ubuntu                latest              93fd78260bd1        3 weeks ago         86.2MB
busybox               latest              59788edf1f3e        2 months ago        1.15MB
registry              latest              2e2f252f3c88        3 months ago        33.3MB


從本地倉庫拉取映象:

# docker pull 192.168.2.120:5000/cent-ckl-http
Using default tag: latest
latest: Pulling from cent-ckl-http
Digest: sha256:70cf9a97343265e191054268245fd77b18c5da7a8fe66168702c96711ba4ef0e
Status: Downloaded newer image for 192.168.2.120:5000/cent-ckl-http:latest
# docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
ckl893/nginx-centos                latest              d6f680e9e2f5        6 hours ago         442MB
192.168.2.120:5000/cent-ckl-http   latest              d3ec54e41d03        2 days ago          443MB
cent-ckl-http                      latest              d3ec54e41d03        2 days ago          443MB
centos-ckl-ng                      latest              248da05fb49e        3 days ago          333MB
centos-vim                         latest              82fd472e3387        3 days ago          327MB
cent-vim                           latest              4659d1ec001d        3 days ago          225MB
centos                             latest              1e1148e4cc2c        11 days ago         202MB
nginx                              latest              568c4670fa80        2 weeks ago         109MB
ubuntu                             latest              93fd78260bd1        3 weeks ago         86.2MB
busybox                            latest              59788edf1f3e        2 months ago        1.15MB
registry                           latest              2e2f252f3c88        3 months ago        33.3MB

3.2.4.非本機上傳映象到私有registry

為映象打tag:

# docker tag mysql:5.6 192.168.2.120:5000/mysql:5.6

上傳映象:

# docker push 192.168.2.120:5000/mysql:5.6
The push refers to a repository [192.168.2.120:5000/mysql]
Get https://192.168.2.120:5000/v1/_ping: http: server gave HTTP response to HTTPS client


解決:

# vim /etc/docker/key.json 
{ "insecure-registries":["192.168.2.120:5000"] }


重啟docker:

# systemctl restart docker.service

再次上傳成功:

# docker push 192.168.2.120:5000/mysql:5.6
The push refers to a repository [192.168.2.120:5000/mysql]
119c93d6ccc5: Pushed 
1dadbfda87dc: Pushed 
89bb4ce949c4: Pushed 
46ecad004d82: Pushed 
dae415ebeca7: Pushed 
3c392b9bb7a4: Pushed 
2566141f200b: Pushed 
0ad177796f33: Pushed 
0f1205f1cd43: Pushed 
a588c986cf97: Pushed 
ef68f6734aa4: Pushed 
5.6: digest: sha256:91651561a6bc037926b3ff5b1eed80d3c6c2a53064414b8e11a8d14317d5de56 size: 2621