Docker基础内容之仓库

前言

Docker提供了开放的中央仓库dockerhub,同时也容许咱们使用registry搭建本地私有仓库。搭建私有仓库有以下的优势:html

  • 节省网络带宽,提高Docker部署速度,不用每一个镜像从DockerHub上去下载,只需从私有仓库下载就可;
  • 私有镜像,包含公司敏感信息,不方便公开对外,只在公司内部使用。

 

私有仓库基本部署

部署步骤

下载镜像仓库nginx

  要想部署docker私有仓库必须使用官方给定的镜像来进行构造。git

docker pull registry

 

建立容器github

docker run -d -p 5000:5000 --restart=always --name=registry-srv -v /mydata/dockerRegistry:/var/lib/registry registry

-d:后台运行
-p:将容器的5000端口映射到宿主机的5000端口--name:容器的名称
-v:将容器内的/var/lib/registry映射到宿主机的/mydata/dockerRegistry目录,默认状况下,会将仓库存放于容器的/tmp/registry目录下;

 

搭建容器相关的web服务web

docker pull hyper/docker-registry-web
docker run -it -p 8080:8080 --restart=always --name registry-web --link registry-srv -e REGISTRY_URL=http://registry-srv:5000/v2 -e REGISTRY_NAME=localhost:5000 hyper/docker-registry-web

-it: 以交互模式运行
--link:连接其它容器(registry-srv),在此容器中,使用registry-srv等同于registry-srv容器的局域网地址
-e:设置环境变量

 

上传测试docker

docker push 192.168.1.87:5000/cbbing/hcharts

 

下载测试json

docker pull 192.168.1.87:5000/hcharts

 

注意centos

若是你不想使用 127.0.0.1:5000 做为仓库地址,好比想让本网段的其余主机也能把镜像推送到私有仓库。你就得把例如 192.168.199.100:5000 这样的内网地址做为私有仓库地址,这时你会发现没法成功推送镜像。网络

这是由于 Docker 默认不容许非 HTTPS 方式推送镜像。咱们能够经过 Docker 的配置选项来取消这个限制,或者查看下一节配置可以经过 HTTPS 访问的私有仓库。dom

对于使用 systemd 的系统,请在 /etc/docker/daemon.json 中写入以下内容(若是文件不存在请新建该文件);修改完配置以后记得重启docker。

{
  "registry-mirror": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "192.168.199.100:5000"
  ]
}

 

web端查看私有仓库镜像

  访问:http://192.168.1.87:8080/,网页上呈现:

 

私有仓库高级部署

环境准备

  新建一个文件夹,如下步骤均在该文件夹中进行

[root@host-3 ~]# mkdir /opt/ssl
[root@host-3 ssl]# cd /opt/ssl/

 

准备站点证书

  这里假设咱们将要搭建的私有仓库地址为 docker.domain.com,下面咱们介绍使用 openssl 自行签发 docker.domain.com 的站点 SSL 证书。

 

建立 CA 私钥

[root@host-3 ssl]# openssl genrsa -out "root-ca.key" 4096
Generating RSA private key, 4096 bit long modulus
...........................++
................++
e is 65537 (0x10001)

 

建立 CA 根证书请求文件 

[root@host-3 ssl]# openssl req -new -key "root-ca.key" -out "root-ca.csr" -sha256 -subj '/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com'
# 以上命令中 -subj 参数里的 /C 表示国家,如 CN;/ST 表示省;/L 表示城市或者地区;/O 表示组织名;/CN 通用名称。

 

配置 CA 根证书

[root@host-3 ssl]# touch root-ca.cnf
[root@host-3 ssl]# vi root-ca.cnf
[root_ca]
basicConstraints = critical,CA:TRUE,pathlen:1
keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
subjectKeyIdentifier=hash

 

签发根证书

[root@host-3 ssl]# 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
Signature ok
subject=/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com
Getting Private key

 

生成站点 SSL 私钥

[root@host-3 ssl]# openssl genrsa -out "docker.domain.com.key" 4096
Generating RSA private key, 4096 bit long modulus
................................................................................................++
.......................................................................................................................................++
e is 65537 (0x10001)

 

生成证书请求文件

[root@host-3 ssl]# openssl req -new -key "docker.domain.com.key" -out "site.csr" -sha256 -subj '/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com'

 

配置证书

[root@host-3 ssl]# touch site.cnf
[root@host-3 ssl]# vi site.cnf

[server]
authorityKeyIdentifier=keyid,issuer
basicConstraints = critical,CA:FALSE
extendedKeyUsage=serverAuth
keyUsage = critical, digitalSignature, keyEncipherment
subjectAltName = DNS:docker.domain.com, IP:127.0.0.1
subjectKeyIdentifier=hash

 

签署站点 SSL 证书

root@host-3 ssl]# openssl x509 -req -days 750 -in "site.csr" -sha256 -CA "root-ca.crt" -CAkey "root-ca.key"  -CAcreateserial -out "docker.domain.com.crt" -extfile "site.cnf" -extensions server
Signature ok
subject=/C=CN/ST=beijing/L=chaoyang/O=emaxdata/CN=docker.domain.com
Getting CA Private Key

# 这样已经拥有了 docker.domain.com 的网站 SSL 私钥 docker.domain.com.key 和 SSL 证书 docker.domain.com.crt 及 CA 根证书 root-ca.crt。保留docker.domain.com.key docker.domain.com.crt root-ca.crt这三个文件,删除其余文件

 

将证书相关内容迁移到docker配置中

[root@host-3 ssl]# mkdir -p /etc/docker/registry
[root@host-3 ssl]# cd /etc/docker/registry/
[root@host-3 registry]# mv /opt/ssl /etc/docker/registry/

 

配置私有仓库信息

[root@host-3 registry]# vi /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://docker.domain.com
  headers:
    X-Content-Type-Options: [nosniff]
  http2:
    disabled: false
  tls:
    certificate: /etc/docker/registry/ssl/docker.domain.com.crt
    key: /etc/docker/registry/ssl/docker.domain.com.key
health:
  storagedriver:
    enabled: true
    interval: 10s
threshold: 3

 

生成http认证文件

[root@host-3 registry]# mkdir -p /etc/docker/registry/auth/
[root@host-3 registry]# docker run --rm --entrypoint htpasswd registry -Bbn admin admin > /etc/docker/registry/auth/nginx.htpasswd

 

生成compose文件

[root@host-3 registry]# vi  /etc/docker/registry/docker-compose.yml
version: '3'

services:
  registry:
    image: registry
    ports:
      - "443:443"
    volumes:
      - ./:/etc/docker/registry
      - registry-data:/var/lib/registry     # 这里的存储路劲就是容器存储中映射到宿主机的挂载点,挂载点在/var/lib/docker下;详情请参考volume的使用

volumes:
  registry-data:

 

修改hosts

vi /etc/hosts
127.0.0.1 docker.domain.com

 

启动容器

[root@host-3 registry]# cd /etc/docker/registry/
[root@host-3 registry]# curl -L https://github.com/docker/compose/releases/download/1.24.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
[root@host-3 registry]# chmod a+x /usr/local/bin/docker-compose
[root@host-3 registry]# docker-compose up -d

这样咱们就搭建好了一个具备权限认证、TLS 的私有仓库,接下来咱们测试其功能是否正常。

 

测试功能

  因为自行签发的 CA 根证书不被系统信任,因此咱们须要将 CA 根证书 ssl/root-ca.crt 移入 /etc/docker/certs.d/docker.domain.com 文件夹中。

[root@host-3 registry]# mkdir -p /etc/docker/certs.d/docker.domain.com
[root@host-3 registry]# cp /etc/docker/registry/ssl/root-ca.crt /etc/docker/certs.d/docker.domain.com/ca.crt

 

登陆私有仓库

docker login docker.domain.com          # 输入上面建立的用户与密码

 

尝试推送、拉取镜像

[root@host-3 centos]# docker tag centos:latest docker.domain.com/admin/centos:latest
[root@host-3 docker]# docker push docker.domain.com/admin/centos
[root@host-3 centos]# docker rmi -f 9f38484d220f
[root@host-3 centos]# docker pull docker.domain.com/admin/centos

 

文章来自转载:https://yeasy.gitbooks.io/docker_practice/content/repository/registry_auth.html

相关文章
相关标签/搜索