如何使用Letsencrypt SSL在Ubuntu 18.04/Ubuntu 16.04上设置Docker Private Registry

时间:2020-02-23 14:31:38  来源:igfitidea点击:

如果我们在基础架构中运行Docker微服务,则可能有兴趣构建内部私有Docker Registry来托管Docker镜像。这可能是出于安全原因或者具有更快的构建。本文将向我们展示在Ubuntu 18.04和Ubuntu 16.04上设置Docker Private Registry所需的步骤。

这里的假设是我们正在运行Docker Engine主机,Kubernetes集群或者DC/OS集群。

如何在Ubuntu 18.04/Ubuntu 16.04上设置Docker Private Registry

让我们开始为Docker镜像构建Private Registry。首先,在主机上安装Docker Engine以充当注册表。

更新apt包索引:

sudo apt-get update

安装软件包,以允许通过HTTPS使用存储库:

sudo apt-get install \
 apt-transport-https \
 ca-certificates \
 curl \
 software-properties-common

添加Dockers官方GPG密钥:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add 

添加官方Docker稳定存储库:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable"

安装docker-cepakage:

sudo apt-get update
sudo apt-get install docker-ce

如果要使用Docker作为非root用户,则现在应考虑将用户添加到dockergroup中:

sudo usermod -aG docker your-user

运行以下命令以查看已安装的Docker版本。

$docker --version
Docker version 18.06.0-ce, build 0ffa825

检查状态,它应该处于运行状态:

$ systemctl status docker

获取Lets Encrypt SSL证书

在此Docker Registry设置中,我们将使用"加密SSL证书",该证书每90天过期一次,我们需要续订。

安装certbot-auto

wget https://dl.eff.org/certbot-auto 
chmod +x certbot-auto 
sudo mv certbot-auto  /usr/local/bin/certbot-auto

要求SSL证书:

export DOMAIN="registry.domain.com"
export EMAIL="theitroad@localhost"
certbot-auto certonly --standalone -d $DOMAIN --preferred-challenges http --agree-tos -n -m $EMAIL --keep-until-expiring

证书将保存在/etc/letsencrypt/live /

/etc/letsencrypt/live/registry.domain.com/fullchain.pem
/etc/letsencrypt/live/registry.domain.com/privkey.pem

fullchain.pem组合文件cert.pemchain.pem
chain.pem中间证书
cert.pem SSL服务器证书(包括公钥)
privkey.pem私钥文件

配置并启动Docker Registry容器

我们可以使用SSL或者不使用SSL运行docker Registry。但首先,创建一个目录以保存Docker注册表镜像:

sudo mkdir /var/lib/docker/registry

在没有SSL的情况下运行本地Docker注册表

$docker run -d -p 5000:5000 \
--name docker-registry \
--restart=always \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2

使用SSL运行本地Docker注册表

创建目录并将证书放置在主机上:

mkdir /certs
cat /etc/letsencrypt/live/registry.domain.com/fullchain.pem > /certs/fullchain.pem
cat /etc/letsencrypt/live/registry.domain.com/privkey.pem > /certs/privkey.pem
cat /etc/letsencrypt/live/registry.domain.com/cert.pem > /certs/cert.pem

创建一个Docker Registry容器:

$docker run -d --name docker-registry --restart=always \
-p 5000:5000 \
-e REGISTRY_HTTP_ADDR=0.0.0.0:5000 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/fullchain.pem  \
-e REGISTRY_HTTP_TLS_KEY=/certs/privkey.pem \
-v /certs:/certs \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2

registry.domain.com替换为注册表子域名。

它将下载注册表:2 docker镜像(如果不存在)并创建一个容器

....
Unable to find image 'registry:2' locally
2: Pulling from library/registry
4064ffdc82fe: Pull complete 
c12c92d1c5a2: Pull complete 
4fbc9b6835cc: Pull complete 
765973b0f65f: Pull complete 
3968771a7c3a: Pull complete 
Digest: sha256:51bb55f23ef7e25ac9b8313b139a8dd45baa832943c8ad8f7da2ddad6355b3c8
Status: Downloaded newer image for registry:2
211c906fdbc3f0ccc2ce5cf7f6af5f7b7448eb006bca96ba275c093182f63888

检查容器状态:

# docker ps

要将镜像推送到注册表容器服务器,请进行如下设置:

$curl https://registry.theitroad.local:5000/v2/_catalog 
{"repositories":[]}

让我们下载两个docker镜像并将其推送到此本地存储库:

# docker pull alpine
# docker pull ubuntu

然后设置标签并将镜像推送到我们的注册表:

# docker tag ubuntu registry.theitroad.local:5000/ubuntu:v1
# docker push registry.theitroad.local:5000/ubuntu:v1
The push refers to repository [registry.theitroad.local:5000/ubuntu]
268a067217b5: Pushed 
c01d74f99de4: Pushed 
ccd4d61916aa: Pushed 
8f2b771487e9: Pushed 
f49017d4d5ce: Pushed 
v1: digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5 size: 1357

使用docker images命令验证:

# docker images

在Docker主机上,我们可以使用以下方式拉取镜像:

# docker pull registry.theitroad.local:5000/ubuntu:v1
v1: Pulling from ubuntu
c64513b74145: Pull complete 
01b8b12bad90: Pull complete 
c5d85cf7a05f: Pull complete 
b6b268720147: Pull complete 
e12192999ff1: Pull complete 
Digest: sha256:958eaeb7e33e6c4f68f7fef69b35ca178c7f5fb0dd40db7b44a8b9eb692b9bc5
Status: Downloaded newer image for registry.theitroad.local:5000/ubuntu:v1

该镜像现在应该可见:

# docker images

通过身份验证支持启动注册表

除了运行在安全本地网络上的注册表外,注册表应始终实施访问限制。实现访问限制的最简单方法是通过基本身份验证:

为用户创建一个密码文件,其中一个条目为" dockadmin",密码为" registrypassword":

$docker run \
--entrypoint htpasswd \
registry:2 -Bbn dockadmin registrypassword > ~/.htpasswd

$cat ~/.htpasswd
dockadmin:y$WN6moKCdUB1i3lFloCPR/Oaw5b4aVcphXSUlEHuRMD2knofj1FuBW

删除当前的Docker注册表

$docker rm -f docker-registry

使用基本身份验证启动注册表。

$docker run -d --name docker-registry --restart=always \
-p 5000:5000 \
-v ~/.htpasswd:/auth_htpasswd \
-e "REGISTRY_AUTH=htpasswd" \
-e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" \
-e REGISTRY_AUTH_HTPASSWD_PATH=/auth_htpasswd \
-e REGISTRY_HTTP_ADDR=0.0.0.0:5000 \
-e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/fullchain.pem \
-e REGISTRY_HTTP_TLS_KEY=/certs/privkey.pem \
-v /certs:/certs \
-v /var/lib/docker/registry:/var/lib/registry \
registry:2

尝试从注册表中提取镜像,或者将镜像推入注册表。这些命令失败。

$ docker pull registry.theitroad.local:5000/ubuntu:v1

我们需要登录到注册表。

# docker login registry.theitroad.local:5000

现在,我们应该能够下载镜像并将其推送到存储库:

$docker tag alpine registry.theitroad.local:5000/alpine_local
$docker push registry.theitroad.local:5000/alpine_local
The push refers to repository [registry.theitroad.local:5000/alpine_local]
73046094a9b8: Pushed 
latest: digest: sha256:0873c923e00e0fd2ba78041bfb64a105e1ecb7678916d1f7776311e45bf5634b size: 528

停止本地Docker注册表

要停止注册表,请使用与任何其他容器相同的docker container stop命令。

$docker container stop docker-registry

要删除容器,请使用docker container rm。

$docker container stop registry
$docker container rm -v registry
$docker container rm -f -v registry # Force remove running