Docker镜像操作及管理
docker镜像仓库
Docker镜像仓分为本地仓库和公网仓库,公网仓库包括阿里云和docker官方提供的镜像仓库,私有仓库又可分为单机私有仓库和harbor分布式仓库。公网仓库中的镜像
阿里云镜像仓库
地址:阿里云镜像仓库
1.创建命名仓库,可以是个人、机构或者公司名称
2.创建镜像仓库名称,也就是项目名称,如centos系统,nginx服务或者haproxy负载均衡代理
3.推送镜像到阿里云,点击任何的镜像后,阿里云均会提供对应的操作步骤,包括登录仓库,从仓库拉取镜像以及如何推送镜像到仓库中
3.1. 登录阿里云Docker Registry
$ sudo docker login --username=xxx registry.cn-zhangjiakou.aliyuncs.com
地址:注册阿里云镜像后将自动生成
用户:注册阿里云时的账号,可以使用--username
指定,或者不指定使用交互式输入
密码:注册时输入,使用交互式方式输入
登录成功后,将在/$home/.docker/config.json中生成加密的登录信息
root@ubuntu1804-server:~# cat /root/.docker/config.json
{
"auths": {
"registry.cn-zhangjiakou.aliyuncs.com": {
"auth": "6ZmI5rC46ZKmdGhvbWFzOnRob21hcy5jVu"
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/18.09.9 (linux)"
}
}
3.2. 从Registry中拉取镜像
$ sudo docker pull registry.cn-zhangjiakou.aliyuncs.com/thomaschen/haproxy:[镜像版本号]
镜像地址:创建后将自动生成
3.3. 将镜像推送到Registry
3.3.1.登录镜像仓库
$ sudo docker login --username=xxx registry.cn-zhangjiakou.aliyuncs.com
3.3.2.为将要上传的镜像打标,打标格式需与推荐的格式匹配
$ sudo docker tag [ImageId] registry.cn-zhangjiakou.aliyuncs.com/thomaschen/haproxy:[镜像版本号]
如果出现了以下的报错,可能是登录后的信息/root/.docker/config.json
出现问题,将其删除后重新使用docker login
登录即可
denied: requested access to the resource is denied
3.3.3.将上一步打的tag标签,使用docker push
命令推送即可
$ sudo docker push registry.cn-zhangjiakou.aliyuncs.com/thomaschen/haproxy:[镜像版本号]
创建Docker registry单机仓库
Docker Registry 作为 Docker 的核心组件之一负责镜像内容的存储与分发,客户
端的 docker pull 以及 push 命令都将直接与 registry 进行交互,最初版本的 registry
由Python实现,由于设计初期在安全性,性能以及API的设计上有着诸多的缺陷,
该版本在 0.9 之后停止了开发,由新的项目 distribution(新的 docker register 被
称为 Distribution)来重新设计并开发下一代 registry,新的项目由 go 语言开发,
所有的 API,底层存储方式,系统架构都进行了全面的重新设计已解决上一代
registry 中存在的问题,2016 年 4 月份 rgistry 2.0 正式发布,docker 1.6 版本开始
支持 registry 2.0,而八月份随着 docker 1.8 发布,docker hub 正式启用 2.1 版本
registry 全面替代之前版本 registry,新版 registry 对镜像存储格式进行了重新设
计并和旧版不兼容,docker 1.5 和之前的版本无法读取 2.0 的镜像,另外,Registry
2.4 版本之后支持了回收站机制,也就是可以删除镜像了,在 2.4 版本之前是无
法支持删除镜像的,所以如果你要使用最好是大于 Registry 2.4 版本的,目前最
新版本为 2.7.x。
官方文档地址:https://docs.docker.com/registry/
官方 github 地址:https://github.com/docker/distribution
1.拉取registry镜像
root@ubuntu1804-server:~# docker pull registry
Using default tag: latest
latest: Pulling from library/registry
486039affc0a: Pull complete
ba51a3b098e6: Pull complete
8bb4c43d6c8e: Pull complete
6f5f453e5f2d: Pull complete
42bc10b72f42: Pull complete
Digest: sha256:7d081088e4bfd632a88e3f3bcd9e007ef44a796fddfe3261407a3f9f04abe1e7
2.设置登入registry仓库的验证信息,将验证信息加密后放到/docker/auth/htpasswd
命令:docker run --entrypoint htpasswd registry -Bbn [username] [passwd] > auth/htpasswd
root@ubuntu1804-server:~# mkdir /docker/auth -p
root@ubuntu1804-server:~# cd /docker/
root@ubuntu1804-server:/docker# docker run --entrypoint htpasswd registry -Bbn thomas thomas > auth/htpasswd
root@ubuntu1804-server:/docker# docker^C
root@ubuntu1804-server:/docker# cat auth/htpasswd
thomas:$2y$05$qsVHkPuGD3wNyyva407J8OC//Htcpylx5gtrJkfS2WcZKwlTzwbzq
3.启动registry镜像,registry默认端口为5000,启动时使用-e参数设置环境变量指定授权信息路径
root@ubuntu1804-server:/docker# docker run -d -p 5000:5000 \
--restart=always --name registry1 \
-v /docker/auth:/auth -e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry
4.验证容器是否正常启动
5.上传镜像
5.1 登录仓库
正常登录将会报错
root@ubuntu1804-server:/docker# docker login 192.168.7.11:5000
Username: thomas
Password:
Error response from daemon: Get https://192.168.7.11:5000/v2/: http: server gave HTTP response to HTTPS client
因为所有镜像仓库使用的协议都必须是加密的,而要添加信任则需要使用containerd
的参数--insecure-registry registry_ip:registry_port
,所以修改docker.service即可
5.2 为推送的镜像打tag并推送
打标:docker tag imageID registry_ip:registry_port/project_name/app_name:tag
推送:docker push registry_ip:registry_port/project_name/app_name:tag
root/docker# docker tag 948a57f42cb9 192.168.7.11:5000/thomas/ubuntu:1804
root/docker# docker push 192.168.7.11:5000/thomas/ubuntu:1804
The push refers to repository [192.168.7.11:5000/thomas/ubuntu]
1907bffee58b: Pushed
f55aa0bd26b8: Pushed
1d0dfb259f6a: Pushed
21ec61b65b20: Pushed
43c67172d1d1: Pushed
1804: digest: sha256:c46331c0f3b7258b201f716171c93987682b0cee6667b3256d82b81b910b95bc size: 1364
5.3.拉取推送的镜像验证
pull用法:docker pull registry_ip:registry_port/project_name/app_name:tag
root@ubuntu1804-server:~# docker pull 192.168.7.11:5000/thomas/ubuntu:1804
1804: Pulling from thomas/ubuntu
5c939e3a4d10: Pull complete
c63719cdbe7a: Pull complete
19a861ea6baf: Pull complete
651c9d2d6c4f: Pull complete
960380405c88: Pull complete
Digest: sha256:c46331c0f3b7258b201f716171c93987682b0cee6667b3256d82b81b910b95bc
Status: Downloaded newer image for 192.168.7.11:5000/thomas/ubuntu:1804
root@ubuntu1804-server:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.7.11:5000/thomas/ubuntu 1804 948a57f42cb9 4 hours ago 176MB
分布式仓库Harbor
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,由vmware开源,其通过添加一些企业必需的功能特性,例如安全、标识和管理等,扩展了开源 Docker Distribution。作为一个企业级私有 Registry 服务器,Harbor 提供了更好的性能和安全。提升用户使用 Registry 构建和运行环境传输镜像的效率。Harbor
支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有Registry中,确保数据和知识产权在公司内部网络中管控,另外,Harbor 也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。
vmware 官方开源服务列表地址:https://vmware.github.io/harbor/cn/
harbor 官方 github 地址:https://github.com/vmware/harbor
harbor 官方网址:https://goharbor.io/
Harbor组件
nginx:harbor 的一个反向代理组件,代理 registry、ui、token 等服务。
这个代理会转发 harbor web和docker clien 的各种请求到后端服务上。
harbor-adminserver:harbor系统管理接口,可以修改系统配置以及获取系统信息。
harbor-db:存储项目的元数据、用户、规则、复制策略等信息。
harbor-jobservice:harbo 里面主要是为了镜像仓库之前同步使用的。
harbor-log:收集其他harbor的日志信息。
harbor-ui:一个用户界面模块,用来管理registry。
registry:存储 docker images 的服务,并且提供pull/push 服务。
redis:存储缓存信息
webhook:当 registry 中的 image 状态发生变化的时候去记录更新日志、复制等操作。
token service:在docker client进行pull/push的时候负责token的发放。
安装harbor 1.10.1版本
1.解压安装包
root@ubuntu1804-server:~# tar xzvf harbor-offline-installer-v1.10.1.tgz -C /usr/local/src
harbor/harbor.v1.10.1.tar.gz
harbor/prepare
harbor/LICENSE
harbor/install.sh
harbor/common.sh
harbor/harbor.yml
root@ubuntu1804-server:~# ln -sv /usr/local/src/harbor/ /usr/local/harbor
'/usr/local/harbor' -> '/usr/local/src/harbor/'
2.修改配置文件,暂时关闭https功能,修改登录密码
root@ubuntu1804-server:/usr/local/harbor# cat harbor.yml
hostname: harbor.thomas.com
# http related config
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 80
#https:
# https port for harbor, default is 443
# port: 443
# The path of cert and key files for nginx
# certificate: /your/certificate/path
# private_key: /your/private/key/path
harbor_admin_password: thomas
3.安装docker-compose(版本需大于1.18.0)
犹豫阿里云或者ubuntu官方提供的版本都不支持,所以此处使用python3的pip3安装
root@ubuntu1804-server:/usr/local/harbor# apt install python3-pip
root@ubuntu1804-server:/usr/local/harbor# pip3 install docker-compose
root@ubuntu1804-server:/usr/local/harbor# docker-compose -v
docker-compose version 1.25.4, build unknown
4.正式安装harbor
运行install.sh
即可
harbor登录名:admin
harbor登录密码:harbor.yml中harbor_admin_password
字段定义的密码
安装harbor 1.7.6版本
1.7以前和1.8的harbor版本的安装方式有点差异,主要体现在目录结构以及配置文件。
1.解压安装包
root@ubuntu1804-server:~# tar xzvf harbor-offline-installer-v1.7.6.tgz -C /usr/local/
root@ubuntu1804-server:~# cd /usr/local/harbor/
root@ubuntu1804-server:/usr/local/harbor# ll
total 587748
drwxr-xr-x 3 root root 4096 Feb 20 20:17 ./
drwxr-xr-x 11 root root 4096 Feb 20 17:23 ../
drwxr-xr-x 4 root root 4096 Feb 20 17:27 common/
-rw-r--r-- 1 root root 939 Sep 12 11:27 docker-compose.chartmuseum.yml
-rw-r--r-- 1 root root 975 Sep 12 11:27 docker-compose.clair.yml
-rw-r--r-- 1 root root 1434 Sep 12 11:27 docker-compose.notary.yml
-rw-r--r-- 1 root root 5608 Sep 12 11:27 docker-compose.yml
-rw-r--r-- 1 root root 8025 Feb 20 17:28 harbor.cfg
-rw-r--r-- 1 root root 600483180 Sep 12 11:28 harbor.v1.7.6.tar.gz
-rwxr-xr-x 1 root root 5739 Sep 12 11:27 install.sh*
-rw-r--r-- 1 root root 11347 Sep 12 11:27 LICENSE
-rw-r--r-- 1 root root 1263409 Sep 12 11:27 open_source_license
-rwxr-xr-x 1 root root 36337 Sep 12 11:27 prepare*
2.编辑harbor.cfg
修改hostname = 192.168.7.103
和harbor_admin_password = thomas
执行./prepare
更新修改的配置文件
root@ubuntu1804-server:/usr/local/harbor# ./prepare
Clearing the configuration file: ./common/config/core/app.conf
Clearing the configuration file: ./common/config/core/env
Clearing the configuration file: ./common/config/core/private_key.pem
Clearing the configuration file: ./common/config/log/logrotate.conf
Clearing the configuration file: ./common/config/nginx/nginx.conf
Clearing the configuration file: ./common/config/db/env
Clearing the configuration file: ./common/config/jobservice/config.yml
Clearing the configuration file: ./common/config/jobservice/env
Clearing the configuration file: ./common/config/adminserver/env
Clearing the configuration file: ./common/config/registryctl/config.yml
Clearing the configuration file: ./common/config/registryctl/env
Clearing the configuration file: ./common/config/registry/config.yml
Clearing the configuration file: ./common/config/registry/root.crt
loaded secret from file: /data/secretkey
Generated configuration file: ./common/config/nginx/nginx.conf
Generated configuration file: ./common/config/adminserver/env
Generated configuration file: ./common/config/core/env
Generated configuration file: ./common/config/registry/config.yml
Generated configuration file: ./common/config/db/env
Generated configuration file: ./common/config/jobservice/env
Generated configuration file: ./common/config/jobservice/config.yml
Generated configuration file: ./common/config/log/logrotate.conf
Generated configuration file: ./common/config/registryctl/env
Generated configuration file: ./common/config/core/app.conf
Generated certificate, key file: ./common/config/core/private_key.pem, cert file: ./common/config/registry/root.crt
The configuration files are ready, please use docker-compose to start the service.
安装python-pip
(必须为python2版本)
安装pip install docker-compose
(harbor需使用)
运行./install.sh
确保80
端口已经放开
root@ubuntu1804-server:/usr/local/harbor# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 127.0.0.1:1514 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:6010 [::]:*
LISTEN 0 128 *:443 *:*
LISTEN 0 128 *:4443 *:*
登录harbor地址验证
Harbor 高可用
Harbor 高可用的方式有两种,一种是使用共享存储方式,将harbor镜像文件夹共享存储,另一种是基于http方式,使用双向同步的方式,类似于Mysql的双主复制
实现双主同步
1.在另一台主机安装相同版本的harbor,此处不做演示。
2.新增项目将访问级别设置为公开,否则匿名用户将无法pull下载镜像,需要login。
3.设置镜像仓库,也就是要同步的对方主机域名或地址还有验证信息,必须测试连接成功后才能保存
4.设置复制管理,主要是设置要将哪个项目同步给对方
5.在另一台做类似的操作,但是两者的项目名称必须相同才能互相同步
104的设置
6.本地配置同意http不安全连接,根据配置文件的配置,此处不适用https加密协议
7.登录harbor(帐号密码与登录web一致),打tag,并push到harbor
root@ubuntu1804-server:/docker# docker login 192.168.7.104
Authenticating with existing credentials...
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
root@ubuntu1804-server:/docker# docker tag 06d988b898c7 192.168.7.104/thomas/tomcat-web:base
root@ubuntu1804-server:/docker# docker push 192.168.7.104/thomas/tomcat-web:base
The push refers to repository [192.168.7.104/thomas/tomcat-web]
d630e7a7f32b: Pushed
5948124f32b4: Pushed
a49dad040d48: Pushed
0255f0261dc0: Pushed
ea2d33b54842: Pushed
17dde2184f14: Pushed
a0743b2ef3cf: Pushed
ff8bf6c9707a: Pushed
e4b1c7e0d6f8: Pushed
da9c7e1605d0: Pushed
5eafc45c14d2: Pushed
c5f2514667fc: Pushed
40983fc3f6df: Pushed
17d101b04c9e: Pushed
034f282942cd: Pushed
base: digest: sha256:4b4f41245abd2c4ab7efbef14afb962f2860ffbfebd9d50ce8322b3051e366ac size: 3460
8.web上确认是否上传并同步成功
103确认已经上传成功
104确认已经同步成功