拉取私有仓库
逻辑
1. 本次私有仓库是创建在云服务器上,云服务器有两个地址,公网和私有IP2. push时需要命名为daemon中认证的IP地址和端口号3. 从Windows端口push时,由于时外网访问,所以上传IP地址和内网地址不一致,所以并不能上云
docker pull registry
122
1212
修改配置
# 编辑配置文件vi /etc/docker/daemon.json# 内容{"registry-mirrors":["http://hub-mirror.c.163.com","https://docker.mirrors.ustc.edu.cn"],"insecure-registries":["192.168.1.9:3389"]}
重新加载配置信息以及重启Dokcer服务
#重新加载某个服务的配置文件sudo systemctl daemon-reload#重启dockersudo systemctl restart docker
创建私有仓库容器
# 删除/mydata/docker_registry下文件再创建docker run -di --name registry -p 3389:5000 -v /mydata/docker_registry:/var/lib/registry registry
12
推送镜像至私有仓库
# 添加标签 格式为原标签:新标签【网址+端口/镜像名】docker tag caesartylor/test-helloworld:1.0.0 192.168.1.9:3389/hello-world# 本地推送docker push 192.168.1.9:3389/hello-world
实现效果
# 从本地仓库创建容器docker run -it --name hello-world 192.168.1.9:3389/hello-world# 查看卷中的文件root@vvkt7whznuckhiz2-0723575:/mydata/docker_registry/docker/registry/v2/repositories# lshello-world
配置私有仓库认证
私有仓库已经搭建好了,要确保私有仓库的安全性,还需要一个安全认证证书, 防止发生意想不到的事情。所以需要在搭建私有仓库的Docker主机上先生成自签名证书。
创建证书存储目录。
mkdir -p /usr/local/registry/certs
生成自签名证书命令
openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/registry/certs/domain.key -x509 -days 365 -out /usr/local/registry/certs/domain.crt
5.1 生成自签名证书
● openssl req :创建证书签名请求等功能;● -newkey :创建CSR证书签名文件和RSA私钥文件;● rsa:2048 :指定创建的RSA私钥长度为2048;● -nodes :对私钥不进行加密;● -sha256 :使用SHA256算法;● -keyout :创建的私钥文件名称及位置;● -x509 :自签发证书格式;● -days :证书有效期;● -out :指定CSR输出文件名称及位置;
创建过程
root@vvkt7whznuckhiz2-0723575:~# openssl req -newkey rsa:2048 -nodes -sha256 -keyout /usr/local/registry/certs/domain.key -x509 -days 365 -out /usr/local/registry/certs/domain.crtCan't load /root/.rnd into RNG140495885132224:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rndGenerating a RSA private key..............+++++.............................+++++writing new private key to '/usr/local/registry/certs/domain.key'-----You are about to be asked to enter information that will be incorporatedinto your certificate request.What you are about to enter is what is called a Distinguished Name or a DN.There are quite a few fields but you can leave some blankFor some fields there will be a default value,If you enter '.', the field will be left blank.-----Country Name (2 letter code) [AU]:SHState or Province Name (full name) [Some-State]:SHLocality Name (eg, city) []:SHOrganization Name (eg, company) [Internet Widgits Pty Ltd]:CTOrganizational Unit Name (eg, section) []:CTCommon Name (e.g. server FQDN or YOUR name) []:192.168.1.9Email Address []:419997284@qq.com
5.2 生成鉴权密码文件
#创建存储鉴权密码文件目录mkdir -p /usr/local/registry/auth#如果没有htpasswd 功能需要安装httpdyum install -y httpd# Ubuntu下安装sudo apt install apache2-utils#创建用户和密码htpasswd -Bbn root 1234 > /usr/local/registry/auth/htpasswd
htpasswd是apache http的基本认证文件,使用htpasswd命令可以生成用户及密码文件。
5.3 创建私有仓库容器
docker run -di --name registry -p 3389:5000 -v /mydata/docker_registry:/var/lib/registry -v /usr/local/registry/certs:/certs -v /usr/local/registry/auth:/auth -e "REGISTRY AUTH=htpasswd" -e "REGISTRY_AUTH.HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH HTPASSWD_PATH=/auth/htpasswd -e REGISTRY HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=certs/domain.key registrydocker run -di --name registry -p 3389:5000 -v /mydata/docker_registry:/var/lib/registry -v /usr/local/registry/certs:/certs -v /usr/local/registry/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=auth/htpasswd -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=/certs/domain.key registry
docker run -di --name registry -p 3389:5000-v /mydata/docker_registry:/var/lib/registry-v /usr/local/registry/certs:/certs-v /usr/local/registry/auth:/auth-e "REGISTRY_AUTH=htpasswd"-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm"-e REGISTRY_AUTH_HTPASSWD_PATH=auth/htpasswd-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/domain.crt-e REGISTRY_HTTP_TLS_KEY=/certs/domain.keyregistry
容器无法运行的报错及解决措施
1. 报错,程序使用密钥和证书文件创建,最后竟无法运行time="2021-08-13T02:03:48.750612384Z" level=warning msg="No HTTP secret provided - generated random secret. This may cause problems with uploads if multiple registries are behind a load-balancer. To provide a shared secret, fill in http.secret in the configuration file or set the REGISTRY_HTTP_SECRET environment variable." go.version=go1.11.2 instance.id=b21b502d-e852-4ca8-95cb-70ac7d76a27e service=registry version=v2.7.1time="2021-08-13T02:03:48.750853985Z" level=info msg="redis not configured" go.version=go1.11.2 instance.id=b21b502d-e852-4ca8-95cb-70ac7d76a27e service=registry version=v2.7.1time="2021-08-13T02:03:48.760778738Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.11.2 instance.id=b21b502d-e852-4ca8-95cb-70ac7d76a27e service=registry version=v2.7.1time="2021-08-13T02:03:48.761126562Z" level=fatal msg="open /certs/domain.cert: no such file or directory"time="2021-08-13T02:04:07.141904369Z" level=warning msg="No HTTP secret provided - generated random secret. This may cause problems with uploads if multiple registries are behind a load-balancer. To provide a shared secret, fill in http.secret in the configuration file or set the REGISTRY_HTTP_SECRET environment variable." go.version=go1.11.2 instance.id=9866b621-ac2a-4333-959b-d44260e3885b service=registry version=v2.7.1time="2021-08-13T02:04:07.142162337Z" level=info msg="redis not configured" go.version=go1.11.2 instance.id=9866b621-ac2a-4333-959b-d44260e3885b service=registry version=v2.7.1time="2021-08-13T02:04:07.152537921Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.11.2 instance.id=9866b621-ac2a-4333-959b-d44260e3885b service=registry version=v2.7.1time="2021-08-13T02:04:07.152917456Z" level=fatal msg="open /certs/domain.cert: no such file or directory"root@vvkt7whznuckhiz2-0723575:~# cd /usr/local/registry/certs2. 原因,使用命令:docker logs --details registry,查看日志,发现配置文件domain.crt错写为cert3. 解决办法:删除容器,修改配置文件重新创建4. 收获:命令整体为绑定目录,指定环境变量,auth和cert都是单独指定目录
5.4 推送至私有仓库失败
root@vvkt7whznuckhiz2-0723575:~# docker push 192.168.1.9:3389/hello-worldUsing default tag: latestThe push refers to repository [192.168.1.9:3389/hello-world]f22b99068db9: Preparingno basic auth credentialsroot@vvkt7whznuckhiz2-0723575:~#
5.5 登录账号
root@vvkt7whznuckhiz2-0723575:~# docker login 192.168.1.9:3389Username: rootPassword:WARNING! Your password will be stored unencrypted in /root/.docker/config.json.Configure a credential helper to remove this warning. Seehttps://docs.docker.com/engine/reference/commandline/login/#credentials-storeLogin Succeeded
5.6 推送镜像至私有仓库成功
root@vvkt7whznuckhiz2-0723575:~# docker push 192.168.1.9:3389/hello-worldUsing default tag: latestThe push refers to repository [192.168.1.9:3389/hello-world]f22b99068db9: Pushedlatest: digest: sha256:1b26826f602946860c279fce658f31050cff2c596583af237d971f4629b57792 size: 525
报错记录
time="2021-08-13T03:03:00.998142307Z" level=warning msg="No HTTP secret provided - generated random secret. This may cause problems with uploads if multiple registries are behind a load-balancer. To provide a shared secret, fill in http.secret in the configuration file or set the REGISTRY_HTTP_SECRET environment variable." go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1time="2021-08-13T03:03:00.998516037Z" level=info msg="redis not configured" go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1time="2021-08-13T03:03:01.008786591Z" level=info msg="using inmemory blob descriptor cache" go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1time="2021-08-13T03:03:01.02673276Z" level=info msg="Starting upload purge in 17m0s" go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1time="2021-08-13T03:03:01.027143705Z" level=info msg="listening on [::]:5000, tls" go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1time="2021-08-13T03:18:28.465212015Z" level=warning msg="error authorizing context: basic authentication challenge for realm "Registry Realm": invalid authorization credential" go.version=go1.11.2 http.request.host="192.168.1.9:3389" http.request.id=82e3313d-5a76-43e9-a306-209524a20ac8 http.request.method=GET http.request.remoteaddr="192.168.1.9:35974" http.request.uri="/v2/" http.request.useragent="docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \(linux\))"192.168.1.9 - - [13/Aug/2021:03:18:28 +0000] "GET /v2/ HTTP/1.1" 401 87 "" "docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \\(linux\\))"time="2021-08-13T03:20:01.027011339Z" level=info msg="PurgeUploads starting: olderThan=2021-08-06 03:20:01.026981626 +0000 UTC m=-603779.950852935, actuallyDelete=true"time="2021-08-13T03:20:01.027107501Z" level=info msg="Purge uploads finished. Num deleted=0, num errors=1"time="2021-08-13T03:20:01.027116354Z" level=info msg="Starting upload purge in 24h0m0s" go.version=go1.11.2 instance.id=a9ba00fa-9bd2-4c0a-a5b7-609c082bbbbd service=registry version=v2.7.1192.168.1.9 - - [13/Aug/2021:03:20:31 +0000] "GET /v2/ HTTP/1.1" 401 87 "" "docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \\(linux\\))"time="2021-08-13T03:20:31.656182602Z" level=warning msg="error authorizing context: basic authentication challenge for realm "Registry Realm": invalid authorization credential" go.version=go1.11.2 http.request.host="192.168.1.9:3389" http.request.id=d2365ba3-4c76-44e2-a987-5f3c9859d516 http.request.method=GET http.request.remoteaddr="192.168.1.9:35978" http.request.uri="/v2/" http.request.useragent="docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \(linux\))"time="2021-08-13T03:20:31.665389768Z" level=info msg="authorized request" go.version=go1.11.2 http.request.host="192.168.1.9:3389" http.request.id=2219268c-ef1c-4b3e-bf80-a5aa263e4f4e http.request.method=GET http.request.remoteaddr="192.168.1.9:35980" http.request.uri="/v2/" http.request.useragent="docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \(linux\))"time="2021-08-13T03:20:31.665647483Z" level=info msg="response completed" go.version=go1.11.2 http.request.host="192.168.1.9:3389" http.request.id=2219268c-ef1c-4b3e-bf80-a5aa263e4f4e http.request.method=GET http.request.remoteaddr="192.168.1.9:35980" http.request.uri="/v2/" http.request.useragent="docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \(linux\))" http.response.contenttype="application/json; charset=utf-8" http.response.duration=4.445561ms http.response.status=200 http.response.written=2192.168.1.9 - - [13/Aug/2021:03:20:31 +0000] "GET /v2/ HTTP/1.1" 200 2 "" "docker/20.10.8 go/go1.16.6 git-commit/75249d8 kernel/4.15.0-154-generic os/linux arch/amd64 UpstreamClient(Docker-Client/20.10.8 \\(linux\\))"2021/08/13 03:21:27 http: TLS handshake error from 183.223.83.69:11714: tls: first record does not look like a TLS handshake2021/08/13 03:21:28 http: TLS handshake error from 183.223.83.69:11713: tls: first record does not look like a TLS handshake2021/08/13 03:21:30 http: TLS handshake error from 183.223.83.69:11717: tls: first record does not look like a TLS handshake2021/08/13 03:21:34 http: TLS handshake error from 183.223.83.69:11718: tls: first record does not look like a TLS handshake2021/08/13 03:21:35 http: TLS handshake error from 183.223.83.69:11735: tls: first record does not look like a TLS handshake2021/08/13 03:21:38 http: TLS handshake error from 183.223.83.69:11738: tls: first record does not look like a TLS handshake2021/08/13 03:21:39 http: TLS handshake error from 183.223.83.69:11742: tls: first record does not look like a TLS handshake2021/08/13 03:21:40 http: TLS handshake error from 183.223.83.69:11746: tls: first record does not look like a TLS handshake2021/08/13 03:21:45 http: TLS handshake error from 183.223.83.69:11748: tls: first record does not look like a TLS handshake
5.7 退出账户
root@vvkt7whznuckhiz2-0723575:~# docker logout 192.168.1.9:3389Removing login credentials for 192.168.1.9:3389
