1 准备工作
- 创建一个文件夹
registry
,后续专门在该文件下操作; - 拥有一个私有仓库地址,比如
demo.testdocker.com
。
2 准备站点证书
注意:以下是使用openssl
自行签发demo.testdocker.com
的站点SSL
证书。
2.1 创建CA私钥
$ sudo openssl genrsa -out "root-ca.key" 4096
2.2 创建CA根证书请求文件
sudo openssl req -new -key "root-ca.key" -out "root-ca.csr" -sha256 -subj '/C=CN/ST=Shanxi/L=Xian/O=Company/CN=Company Docker Registry CA'
2.3 配置CA根证书
- 新建
root-ca.cnf
:
- 并写入如下内容:
[root_ca]
basicConstraints = critical,CA:TRUE,pathlen:1
keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
subjectKeyIdentifier=hash
2.4 签发根证书
sudo openssl x509 -req -days 3650 -in "root-ca.csr" -signkey "root-ca.key" -sha256 -out "root-ca.crt" -extfile "root-ca.cnf" -extensions root_ca
2.5 生成站点SSL私钥
sudo openssl genrsa -out "demo.testdocker.com.key" 4096
2.6 私钥生成证书请求文件
sudo openssl req -new -key "demo.testdocker.com.key" -out "site.csr" -sha256 -subj '/C=CN/ST=Shanxi/L=Xian/O=Company/CN=demo.testdocker.com'
2.7 配置证书
- 新建
site.cnf
文件:
- 写入如下内容:
[server]
authorityKeyIdentifier=keyid,issuer
basicConstraints = critical,CA:FALSE
extendedKeyUsage=serverAuth
keyUsage = critical, digitalSignature, keyEncipherment
subjectAltName = DNS:demo.testdocker.com, IP:127.0.0.1
subjectKeyIdentifier=hash
2.8 签署站点SSL证书
sudo openssl x509 -req -days 750 -in "site.csr" -sha256 -CA "root-ca.crt" -CAkey "root-ca.key" -CAcreateserial -out "demo.testdocker.com.crt" -extfile "site.cnf" -extensions server
此时拥有了 demo.testdocker.com 的网站 SSL 私钥 demo.testdocker.com.key 和 SSL 证书
demo.testdocker.crt 及 CA 根证书 root-ca.crtssl 文件夹并将 docker.domain.com.key docker.domain.com.crt root-ca.crt 这
三个文件移入,删除其他文件
3 配置私有仓库
- 私有仓库默认的配置文件位于
/etc/docker/registry/config.yml
:
version: 0.1
log:
accesslog:
disabled: true
level: debug
formatter: text
fields:
service: registry
environment: staging
storage:
delete:
enabled: true
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: /var/lib/registry
auth:
htpasswd:
realm: basic-realm
path: /etc/docker/registry/auth/nginx.htpasswd
http:
addr: :443
host: https://demo.testdocker.com
headers:
X-Content-Type-Options: [nosniff]
http2:
disabled: false
tls:
certificate: /etc/docker/registry/ssl/demo.testdocker.com.crt
key: /etc/docker/registry/ssl/demo.testdocker.com.key
health:
storagedriver:
enabled: true
interval: 10s
threshold: 3
4 生成 http 认证文件
- 创建
auth
文件夹:
- 执行如下命令:
sudo docker run --rm --entrypoint htpasswd httpd:alpine -Bbn noamanelson 123456 > auth/nginx.htpasswd
noamanelson@noamanelson-Virtual-Machine:/etc/docker/registry$ sudo docker run --rm --entrypoint htpasswd httpd:alpine -Bbn noamanelson 123456 > auth/nginx.htpasswd
Unable to find image 'httpd:alpine' locally
alpine: Pulling from library/httpd
8a49fdb3b6a5: Already exists
beb66a906b34: Pulling fs layer
01924573e8db: Pulling fs layer
255f194b76b3: Pulling fs layer
0f48f8ea2bc9: Pulling fs layer
390cbd08c8ae: Pulling fs layer
0f48f8ea2bc9: Waiting
390cbd08c8ae: Waiting
beb66a906b34: Verifying Checksum
beb66a906b34: Download complete
beb66a906b34: Pull complete
01924573e8db: Verifying Checksum
01924573e8db: Download complete
01924573e8db: Pull complete
255f194b76b3: Verifying Checksum
255f194b76b3: Download complete
390cbd08c8ae: Verifying Checksum
390cbd08c8ae: Download complete
255f194b76b3: Pull complete
0f48f8ea2bc9: Verifying Checksum
0f48f8ea2bc9: Download complete
0f48f8ea2bc9: Pull complete
390cbd08c8ae: Pull complete
Digest: sha256:3e25d419bf180122f5f9af5cf2180709d8234320ff25514a61f5d1bc3cf46394
Status: Downloaded newer image for httpd:alpine
noamanelson@noamanelson-Virtual-Machine:/etc/docker/registry$
- 编辑编辑
docker-compose.yml
:
version: '3'
ervices:
registry:
image: registry
ports:
- "443:443"
volumes:
- ./:/etc/docker/registry
- registry-data:/var/lib/registry
volumes:
registry-data:
5 修改hosts文件
sudo vim /etc/hosts
6 启动
- 使用命令提示找不到,需要安装:
noamanelson@noamanelson-Virtual-Machine:/etc/docker/registry$ sudo docker-compose up -d
sudo: docker-compose:找不到命令
- 安装
docker-compose
:
sudo apt-get install docker-compose
- 再次启动,又报错了:
- 应为
docker-compose
的版本不对,重新卸载docker-compose
:
sudo apt-get remove docker-compose
- 再次安装:
sudo apt-get update
sudo apt-get install docker-compose-plugin
- 启动:
sudo docker compose up -d
7 测试私有仓库
7.1 移动证书
- 由于自行签发的 CA 根证书不被系统信任,所以我们需要将 CA 根证书
ssl/root-ca.crt
移入/etc/docker/certs.d/docker.test.com
文件夹中:
sudo mkdir -p /etc/docker/certs.d/demo.testdocker.com
sudo cp ssl/root-ca.crt /etc/docker/certs.d/demo.testdocker.com/ca.crt
7.2 登陆仓库
sudo docker login demo.testdocker.com
:
7.3 拉取推送镜像
# noamanelson是我的用户名,改为自己的即可
sudo docker pull ubuntu:18.04
sudo docker tag ubuntu:18.04 docker.domain.com/noamanelson/ubuntu:18.04
sudo docker push docker.domain.com/noamanelson/ubuntu:18.04
sudo docker image rm docker.domain.com/noamanelson/ubuntu:18.04
sudo docker pull docker.domain.com/noamanelson/ubuntu:18.04
7.4 退出登陆,并推送镜像
docker logout docker.domain.com
docker push docker.domain.com/noamanelson/ubuntu:18.04