码迷,mamicode.com
首页 > 其他好文 > 详细

Docker 私有仓库详解

时间:2017-08-13 23:19:25      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:duyuheng   docker 私有仓库   

仓库(Repository)是集中存放镜像的地方


一个册服Registry实际上注的具器, 每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认 为是一个具体的项目或目录。例如对于仓库地址 docker.sina.com.cn/centos:centos63 来说, docker.sina.com.cn  是注册服务器地址,centos  是仓库名,centos63  是仓库的 tag

Docker Hub  官方仓库

目前 Docker 官方维护了一个公共仓库 Docker Hub,其中已经包括了超过 15,000 的镜像。 大部分需求,都可以通过在 Docker Hub  中直接下载镜像来实现。

注册&登录

可以通过命令行执行 docker login 命令来输入用户名、密码和邮箱来完成注册和登录。注册

成功后,本地用户目录的 .docker/config.json 中将保存用户的认证信息。

技术分享

基本操作

用户无需登录即可通过 docker search  命令来查找官方仓库中的镜像,并利用 docker pull 令来将它下载到本地。

例如以 centos  为关键词进行搜索:

技术分享

可以看到返回了很多包含关键字的镜像,其中包括镜像名字、描述、星级(表示该镜像的受 欢迎方创、是自动建。镜像明是方项建和护的, automated 资源允许用户验证镜像的来源和内容。 根据是否是官方提供,可将镜像资源分为两类。一种是类似 centos 这样的基础镜像,被称 为基础或根镜像。这些基础镜像是由 Docker 公司创建、验证、支持、提供。这样的镜像往 往使用单个单词作为名字。还有一种类型,比如 tianon/centos 镜像,它是由 Docker 的用 户创建并维护的,往往带有用户名称前缀。可以通过前缀 user_name/ 来指定使用某个用户 提供的镜像,比如 tianon 用户。另外,在查找的时候通过 -s N 参数可以指定仅显示评价为 N  星以上的镜像。

创建自己的仓库----镜像仓库

拓扑:

技术分享

说明:

docker.benet.com  这是 docker  registry 服务器的主机名称,ip  192.168.1.107;因为 https

SSL 证书要用到主机名,所以要设置主机名。

dockerregistry 服务器作为处理 docker 镜像的最终上传和下载,用的是官方的镜像 registry nginx 1.6.x  是一个用 nginx 作为反向代理服务器

注:关闭 selinux 1)私有仓库 https 支持: A)安装依赖软件包:

[root@localhost ~]# yum -y install pcre-devel zlib-devel openssl openssl-devel


 Nginx 编译需要 PCRE,因为 Nginx  Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表 达式。需要安装 pcre  pcre-devel  yum 就能安装。

Zlib 库提供了开发人员的压缩算法,在 nginx 的模块中需要使用 gzip 压缩。 需要安装 zlib  zlib-devel  yum 就可以安装

 Nginx 中如果需要为服务器提供安全则需要用到 OpenSSL 库。 需要安装的是 openssl  openssl-devel。用 yum 就可以安装。

 

B)配置 SSL

(1) 编辑/etc/hosts, docker.nmgkj.top ip 地址添加进来,例如: 主机名、ip 地址:

添加主机名

[root@localhost ~]# vim  /etc/hostname

docker.nmgkj.top

~                   

增加/etc/hosts 文件内容

[root@localhost ~]# vim  /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.107  docker.ngmkj.top

 (2) 生成根密钥

如果有就将下列文件删除以免冲突

/etc/pki/CA/cacert.pem

/etc/pki/CA/index.txt

/etc/pki/CA/index.txt.attr

/etc/pki/CA/index.txt.old

/etc/pki/CA/serial

/etc/pki/CA/serial.old

 

 

生成

[root@localhost ~]# cd /etc/pki/CA
[root@localhost CA]# openssl   genrsa -out private/cakey.pem 2048

Generating RSA private key, 2048 bit long modulus

............................................+++

......................................................+++

e is 65537 (0x10001)

[root@localhost CA]# ls

certs  crl  newcerts  private

[root@localhost CA]# ls private/

cakey.pem

(3) 生成根证书 执行

[root@localhost CA]#  openssl req -new -x509 -key private/cakey.pem -out cacert.pem

You are about to be asked to enter information that will be incorporated

into 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 blank

For some fields there will be a default value,

If you enter ‘.‘, the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:beijing

Locality Name (eg, city) [Default City]:beijing

Organization Name (eg, company) [Default Company Ltd]:hyzy

Organizational Unit Name (eg, section) []:

Common Name (eg, your name or your server‘s hostname) []:docker.nmgkj.com

Email Address []:

 

会提示输入一些内容,因为是私有的,所以可以随便输入,最好记住能与后面保持一致, 别是"Common Name。必 hostname 的一致。

上面的自签证书 cacert.pem 应该生成在/etc/pki/CA 下。

[root@localhost CA]# ls

cacert.pem  certs  crl  newcerts  private

(4)   nginx web 服务器生成 ssl 密钥

[root@localhost CA]# mkdir /etc/pki/CA/ssl
[root@localhost CA]# cd /etc/pki/CA/ssl
[root@localhost ssl]# openssl   genrsa -out  nginx.key 2048

Generating RSA private key, 2048 bit long modulus

...................................+++

.........+++

e is 65537 (0x10001)

 CA 申请 nginx 执行 nginx 服务器 生成 ssl 密钥了,否则应该是在另一台需要用到证书的服务器上生成。

查看 nginx 服务器的密钥

[root@localhost ssl]# ls

nginx.key

(5) nginx 生成证书签署请求 执行

[root@localhost ssl]# openssl req -new -key nginx.key -out nginx.csr

You are about to be asked to enter information that will be incorporated

into 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 blank

For some fields there will be a default value,

If you enter ‘.‘, the field will be left blank.

-----

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:beijing

Locality Name (eg, city) [Default City]:beijing

Organization Name (eg, company) [Default Company Ltd]:hyzy

Organizational Unit Name (eg, section) []:

Common Name (eg, your name or your server‘s hostname) []:docker.nmgkj.com

Email Address []:

 

Please enter the following ‘extra‘ attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:

同样Commone Name 要是你要名或名,

challenge password 不填。

(6)  私有 CA 根据请求来签发

[root@localhost ssl]# touch   /etc/pki/CA/index.txt
[root@localhost ssl]# touch  /etc/pki/CA/serial
[root@localhost ssl]# echo 00 > /etc/pki/CA/serial 
[root@localhost ssl]# cat /etc/pki/CA/serial

00

执行

[root@localhost ssl]# openssl ca -in nginx.csr -out nginx.crt

Using configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

        Serial Number: 0 (0x0)

        Validity

            Not Before: Aug 10 01:45:33 2017 GMT

            Not After : Aug 10 01:45:33 2018 GMT

        Subject:

            countryName               = CN

            stateOrProvinceName       = beijing

            organizationName          = hyzy

            commonName                = docker.nmgkj.com

        X509v3 extensions:

            X509v3 Basic Constraints:

                CA:FALSE

            Netscape Comment:

                OpenSSL Generated Certificate

            X509v3 Subject Key Identifier:

                21:97:A2:D4:46:42:6C:4D:BC:D2:B1:BE:29:92:29:E7:FF:20:CE:76

            X509v3 Authority Key Identifier:

                keyid:AB:65:D6:7B:61:0C:7D:46:D7:46:F1:7A:83:36:A5:62:B7:82:8A:2D

 

Certificate is to be certified until Aug 10 01:45:33 2018 GMT (365 days)

Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

 

同样会提示输入一些内容,选择 y 就可以了!

查看 nginx 的证书

[root@localhost ssl]# ls

nginx.crt  nginx.csr  nginx.key

 

C)安装,配置,运行 nginx

(1)添加用户和组 指定ID号 也可以不指定

[root@localhost ssl]# groupadd  www -g 58
[root@localhost ssl]# useradd  -u 58 -g www www

2) 编译nginx源码包

在线下包

技术分享

[root@localhost ssl]# cd /usr/src/
[root@localhost src]# tar  zxf nginx-1.11.2.tar.gz 
[root@localhost src]# cd  nginx-1.11.2/
[root@localhost nginx-1.11.2]# ./configure --user=www --group=www --prefix=/opt/nginx  --with-pcre --with-http_stub_status_module  --with-http_ssl_module  --with-http_addition_module  --with-http_realip_module  --with-http_flv_module

上述选项的解释

--user=USER  设定程序运行的用户环境(www)

--group=GROUP 设定程序运行的组环境(www)

--prefix=PATH  设定安装目录

--with-pcre 启用 pcre 库,Nginx  Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表达式

--with-http_stub_status_module 是为了启用 nginx  NginxStatus 功能,用来监控 Nginx 的当 前状态

--with-http_ssl_module开启 SSL 模块,支持使用 HTTPS 协议的网页

 

--with-http_realip_module开启 Real IP 的支持,该模块用于从客户请求的头数据中读  Real Ip 地址

--with-http_addition_module开启 Addtion 模块,该模块允许你追加或前置数据到相应 的主体部分

--with-http_flv_module 模块 ngx_http_flv_module  Flash Video(FLV)文件提供服务端伪流 媒体支持

3安装

[root@localhost nginx-1.11.2]# make && make  install

(4)  编辑/opt/nginx/conf/nginx.conf 文件

[root@localhost conf]# vim  nginx.conf


user www;

worker_processes 4;

 

events {

worker_connections      4096;

}

 

http {

include mime.types;

default_type    application/octet-stream;

sendfile        on;

keepalive_timeout       65;

upstream registry {

                server  192.168.1.107:5000;

        }

server {

        listen  443 ssl;

        server_name     docker.nmgkj.com;

        ssl_certificate /etc/pki/CA/ssl/nginx.crt;

        ssl_certificate_key     /etc/pki/CA/ssl/nginx.key;

        ssl_session_cache       shared:SSL:1m;

        ssl_session_timeout     5m;

        ssl_ciphers     HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers       on;

        location        /       {

                        proxy_pass      http://registry;

                        client_max_body_size    3000m;

                        proxy_set_header        Host    $host;

                        proxy_set_header        X-Forward-For   $remote_addr;

                }

        }

}

 

相关选项含义:

ssl_session_cache   会话缓存用于保存 SSL  会话,这些缓存在工作进程间共享,可以使用

ssl_session_cache 指令进行配置。1M 缓存可以存放大约 4000 个会话。

ssl_session_timeout 缓存超时,默认的缓存超时是 5 分钟。

ssl_ciphers    HIGH:!aNULL:!MD5 使用高强度的加密算法

ssl_prefer_server_ciphers on 依赖 SSLv3 TLSv1 协议的服务器密码将优先于客户端密码。 即:在 SSLv3 或这是 TLSv1 握手时选择一个密码,通常是使用客户端的偏好。如果这个指令 是启用的,那么服务器反而是使用服务器的偏好。

client_max_body_size 即允许上传文件大小的最大值

proxy_set_header Host $host proxy_set_header X-Forward-For $remote_addr 的作用描述: nginx  了实现 反向代理 的需求 而增加 了一个 ngx_http_proxy_module  块。 其中 proxy_set_header 就是该模的含 http 请求同中的含义完全相同,除了 Host 外还有 X-Forward-For

Host 的含义是表明请求的主机名,因为 nginx 作为反向代理使用,而如果后端真实 的服务器设置有类似防盗链或者根据 http 请求头中的 host 字段来进行路由或判断功能的话, 如果反向代理层的 nginx 不重写请求头中的 host 字段,将会导致请求失败【默认反向代理服 务器会向后端真实服务器发送请求,并且请求头中的 host 字段应为 proxy_pass 指令设置的 服务

同理,X_Forward_For 字段表示该条 http 请求是有谁发起的?如果反向代理服务器不重写该 请求头的话,那么后端真实服务器在处理时会认为所有的请求都来自反向代理服务器,如果 后端有防攻击策略的话,那么机器就被封掉了。因此,在配置用作反向代理的 nginx 中一般 会增加两条配置,修改 http 的请求头:

proxy_set_header Host $host; proxy_set_header X-Forward-For $remote_addr;

这里的$host $remote_addr 都是 nginx 的导出变量,可以再配置文件中直接使用。

(5)  验证配置

[root@localhost conf]# /opt/nginx/sbin/nginx  -t

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

(6)  启动 nginx:

[root@localhost conf]# /opt/nginx/sbin/nginx

7)验证是否启动

[root@localhost conf]# ps  -ef |  grep  -i "nginx"

root      6700     1  0 10:02 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx

www       6701  6700  0 10:02 ?        00:00:00 nginx: worker process

www       6702  6700  0 10:02 ?        00:00:00 nginx: worker process

www       6703  6700  0 10:02 ?        00:00:00 nginx: worker process

www       6704  6700  0 10:02 ?        00:00:00 nginx: worker process

root      6706  2750  0 10:02 pts/0    00:00:00 grep --color=auto -i nginx

 

[root@localhost conf]# netstat -anpt  | grep   nginx

tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      9706/nginx: master  

2) 配置,运行 Docker

1)先停止docker

[root@localhost ~]# systemctl   stop  docker

(2)编辑/etc/sysconfig/docker 文件,加上如下一行

[root@localhost ~]# vim /etc/sysconfig/docker

DOCKER_OPTS="--insecure-registry docker.nmgkj.com --tlsverify --tlscacert /etc/pki/CA/cacert.pem"

把根证书复制到/etc/docker/certs.d/docker.xxx.com/目录下

[root@localhost ~]# mkdir -p /etc/docker/certs.d/docker.nmgkj.com
[root@localhost ~]# cp /etc/pki/CA/cacert.pem /etc/docker/certs.d/docker.nmgkj.com/ca-certificates.crt

启动docker

[root@localhost ~]# systemctl  start docker

3)运行私有容器 通过获取官方registry镜像来运行

[root@localhost ~]# docker search  registry 
[root@localhost ~]# docker pull  registry
[root@localhost src]# docker images

REPOSITORY           TAG                 IMAGE ID            CREATED             SIZE

docker.io/registry   latest              bca04f698ba8        18 months ago       422.8 MB

 

使用官方的registry镜像来启动本地的私有仓库。用户可以通过指定参数来配置私有仓库

例如将目录/opt/data/registry作为私有仓库的位置

 
[root@localhost ~]# mkdir -pv /opt/data/registry

mkdir: created directory /opt/data

mkdir: created directory /opt/data/registry

 

运行私有仓库容器

[root@localhost ~]# docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry docker.io/registry

2a8584c5dd643d0ab9e1a81f93cbe002f34e665fab13a207a283680ec85f4113

[root@localhost ~]# docker  ps


CONTAINER ID        IMAGE                COMMAND             CREATED             STATUS              PORTS                    NAMES

2a8584c5dd64        docker.io/registry   "docker-registry"   9 seconds ago       Up 4 seconds        0.0.0.0:5000->5000/tcp   adoring_lumiere

可以通过 -v   参数来将镜像文件存放在本地的指定路径。例如上面的例子将上传的镜像放到

/opt/data/registry 目录。

-p(小写的)用于将容器的 5000 端口映射宿主机的 5000 端口。

 

4)验证 registry:

用浏览器输入: https://docker.nmgkj.com


技术分享

技术分享

技术分享

或者:curl -i -k curl -i -k https://docker.nmgkj.com

 

[root@localhost ~]# curl -i -k https://docker.nmgkj.com

HTTP/1.1 200 OK

Server: nginx/1.11.2

Date: Sun, 13 Aug 2017 03:24:08 GMT

Content-Type: application/json

Content-Length: 28

Connection: keep-alive

Expires: -1

Pragma: no-cache

Cache-Control: no-cache

curl 是通过 url 语法在命令下上传或下载文件的工具软件它支持 http,https,ftp,ftps,telnet 等多种协 议,常被用来抓取网页和监控 Web 服务器状态

 

服务端的配置就到此完成! 注意:注意防火墙 

5Docker 客户端配置

(1)编辑/etc/hosts, docker.benet.com  ip 地址添加进来,例如:

[root@localhost ~]# vim  /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.107   docker.nmgkj.com

~                            

(2) docker registry 服务器端的根证书追加到 ca-certificates.crt 文件里

先从 docker registry 服务器端把文件/etc/pki/CA/cacert.pem 拷贝到本机

[root@localhost ~]# scp root@192.168.1.107:/etc/pki/CA/cacert.pem ./

The authenticity of host ‘192.168.1.107 (192.168.1.107)‘ can‘t be established.

ECDSA key fingerprint is 52:70:64:73:95:ba:b5:6e:63:bb:35:da:7e:1c:5c:d7.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added ‘192.168.1.107‘ (ECDSA) to the list of known hosts.

root@192.168.1.107‘s password:

cacert.pem                                                                           100% 1285     1.3KB/s   00:00    

然后执行命令:

[root@localhost ~]# cat ./cacert.pem >> /etc/pki/tls/certs/ca-certificates.crt

 (3) 验证 docker.yy.com 下的 registry:

用浏览器输入: https://docker.nmgkj.com

技术分享

或者:curl -i -k curl -i -k https://docker.nmgkj.com

技术分享

(4) 使用私有 registry 步骤:

 登录: docker login -u duyuheng  -p pwd123 -e "duyuheng@nmgkj.com" https://docker.nmgkj.com

[root@localhost ~]# docker login  https://docker.nmgkj.com
Username: duyuheng
Password:

Login Succeeded

本地上传一个镜像镜像测试也可从 Docker HUB 上拉取一个镜像,为基础镜像打个标签: docker tag centos:centos6 docker.nmgkj.com/centos:centos6

 

[root@localhost src]# docker  load  <  centos6.tar

2714f4a6cdee: Loading layer [==================================================>] 202.3 MB/202.3 MB

Loaded image: docker.io/centos:centos6                                          ] 557.1 kB/202.3 MB

[root@localhost src]# docker images


REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

docker.io/centos    centos6             cf2c3ece5e41        13 months ago       194.6 MB

 打标签

[root@localhost src]# docker tag centos:centos6 docker.nmgkj.com/centos:centos6
[root@localhost src]# docker  images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

docker.io/centos          centos6             cf2c3ece5e41        13 months ago       194.6 MB

docker.nmgkj.com/centos   centos6             cf2c3ece5e41        13 months ago       194.6 MB

 

发布:上传镜像到本地私有仓库

[root@localhost ~]# docker push docker.nmgkj.com/centos:centos6

The push refers to a repository [docker.nmgkj.com/centos]

2714f4a6cdee: Image successfully pushed

Pushing tag for rev [cf2c3ece5e41] on {https://docker.nmgkj.com/v1/repositories/centos/tags/centos6}

查看私有仓库是否有镜像

[root@localhost ~]# curl 192.168.1.107:5000/v1/search

{"num_results": 1, "query": "", "results": [{"description": "", "name": "library/centos"}]}

查看镜像的存储目录和文件(在镜像服务器)

[root@localhost ~]# yum  -y install tree
[root@localhost ~]# tree /opt/data/registry/repositories

/opt/data/registry/repositories

└── library

    └── centos

        ├── _index_images

        ├── tag_centos6

        └── tagcentos6_json

 

2 directories, 3 files

客户端从私有仓库 pull 下来 image,查看 image

先删除镜像(客户端)

[root@localhost ~]# docker rmi  docker.io/centos:centos6

Untagged: docker.io/centos:centos6

Deleted: sha256:cf2c3ece5e418fd063bfad5e7e8d083182195152f90aac3a5ca4dbfbf6a1fc2a

Deleted: sha256:2714f4a6cdee9d4c987fef019608a4f61f1cda7ccf423aeb8d7d89f745c58b18

[root@localhost ~]# docker  images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

拉取

[root@localhost ~]# docker pull docker.nmgkj.com/centos:centos6

Trying to pull repository docker.nmgkj.com/centos ...

Pulling repository docker.nmgkj.com/centos

cf2c3ece5e41: Pull complete

Status: Downloaded newer image for docker.nmgkj.com/centos:centos6

docker.nmgkj.com/centos: this image was pulled from a legacy registry.  Important: This registry version will not be supported in future versions of docker.

[root@localhost ~]# docker  images

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE

docker.nmgkj.com/centos   centos6             2d14596f4606        13 months ago       194.6 MB

查看私有仓库是否有对应的镜像

[root@localhost ~]# curl -k https://docker.nmgkj.com/v1/search

{"num_results": 1, "query": "", "results": [{"description": "", "name": "library/centos"}]}

浏览器访问仓库

技术分享

(1)  弊端:

server 端可以 login 到官方的 Docker Hub,可以 pull,push 官方和私有仓库! client 端只能操作搭设好的私有仓库!

私有仓库不能 search!

(2)  优点:

所有的 build,pull,push 操作只能在私有仓库的 server 端操作,降低企业风险!

(3)  client  docker login 到官方的 https://index.docker.io/v1/网站,出现 x509: certificate signed by unknown authority 错误时

         mv/etc/pki/tls/certs/ca-certificates.crt

/etc/pki/tls/certs/ca-certificates.crt.bak

重启 docker 服务! servicedocker restart!



本文出自 “duyuheng” 博客,谢绝转载!

Docker 私有仓库详解

标签:duyuheng   docker 私有仓库   

原文地址:http://duyuheng.blog.51cto.com/12879147/1955935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!