互联网科技Java Blog程序园

docker 指南系列之部署registry

2019-12-26  本文已影响0人  爪哇部落格

版本信息

# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7f2769b/1.13.1
 Built:           Sun Sep 15 14:06:47 2019
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-103.git7f2769b.el7.centos.x86_64
 Go version:      go1.10.3
 Git commit:      7f2769b/1.13.1
 Built:           Sun Sep 15 14:06:47 2019
 OS/Arch:         linux/amd64
 Experimental:    false

一. 找到镜像

搜索registry镜像

docker search registry

此处的OFFICIAL表示是官方镜像

下载registry镜像

docker pull docker.io/registry

基础方式运行registry镜像

docker run -d -p 5000:5000 --name registry --restart=always docker.io/registry

验证registry镜像是否启动成功

docker ps -a

访问registry

# curl http://127.0.0.1:5000/v2/_catalog
# {"repositories":""}

二. 配置用户

创建目录保存用户信息以及数据

mkdir /opt/registry/ /opt/registry/auth opt/registry/data

创建用户信息

docker run --entrypoint htpasswd docker.io/registry:latest -Bbn testuser testpwd > /opt/registry/auth/htpasswd

停止原有registry,并重新启动

# 停止registry,之前指定了--name
docker stop registry

# 移除容器
docker rm registry

# 以授权方式启动
docker run -d -p 5000:5000 --name registry --restart=always -v /opt/registry/data:/var/lib/registry -v /opt/registry/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm"  -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd docker.io/registry:latest

验证鉴权registry

## 原有方式
# curl http://127.0.0.1:5000/v2/_catalog
# {"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"registry","Class":"","Name":"catalog","Action":"*"}]}]}

## 鉴权方式
# curl http://127.0.0.1:5000/v2/_catalog -u testuser:testpwd
# {"repositories":""}

三.自签证书版

假如读者有现成证书可以跳过生成自建证书过程

自建证书

# mkdir /opt/registry/certs

# 生成证书
openssl req -newkey rsa:2048 -nodes -sha256 -keyout /opt/registry/certs/domain.key -x509 -days 365 -out /opt/registry/certs/domain.crt

使用证书

docker run -d -p 5000:5000 --name registry --restart=always -v /opt/registry/data:/var/lib/registry -v /opt/registry/auth:/auth -v /opt/registry/certs:/certs
-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
docker.io/registry:latest

四. 可能问题

1.客户端执行docker login http://XXXX 时登录异常

# docker login http://XXXX
Username: testuser
Password: testpwd
Error response from daemon : Get https://XXX/v2 : http: server gave HTTP response to HTTPS client  

http://XXXX 添加到客户端 daemon.json 的insecure-registries 中。

# cd /etc/docker
# vim daemon.json
{
  "insecure-registries" : [
    "http://xxxx"
  ]
}
  1. push 镜像时,返回如下内容
error parsing HTTP 413 response body: invalid character '<' looking for beginning of value: "<html>\r\n<head><title>413 Request Entity Too Large</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>413 Request Entity Too Large</h1></center>\r\n<hr><center>openresty/1.13.6.2</center>\r\n</body>\r\n</html>\r\n"
# 在http,server,location均可以设置,不同的设置,影响范围不一样。
client_max_body_size 0;
  1. 主机时间与容器内时间不一致,相差8小时
# date
Fri Dec 27 10:16:48 CST 2019

## 进入容器内执行结果
# docker exec -it registry /bin/sh
sh-4.2# date
Fri Dec 27 02:16:48 UTC 2019

原因:我国时区处于东八区

解决办法:

## 方法一:启动时挂载宿主机时间
docker run -d -p 5000:5000 --name registry -v /etc/localtime:/etc/localtime -v /opt/registry/:/var/lib/registry docker.io/registry:latest

## 方法二:复制本机时间至容器内
docker cp /etc/localtime registry:/etc/localtime

如返回:Error response from daemon: Error processing tar file(exit status 1): invalid symlink "/etc/localtime" -> "/usr/share/zoneinfo/Asia/Shanghai",执行以下命令

## 方法二:备份
docker cp /usr/share/zoneinfo/Asia/Shanghai registry:/etc/localtime

上一篇下一篇

猜你喜欢

热点阅读