一、扯淡
撸 Dokcer 这么长时间以来,由于国内众所周知的网络原因,Docker pull 镜像会非常慢,慢到你怀疑这个世界,甚至怀疑你来到这个世界的正确性与合理性,为了为了让自己不怀疑世界,记录一下如何撸一个 docker mirror registry
二、动手撸一个
2.1、基本环境
以下操作基本环境如下
- Docker 1.12.1
- registry 2.5.1
- nginx 1.10.1
2.2、导出 registry 配置
Docker 官方提供了一个 registry,Github 地址 点这里,而大部分能找到的资料都是如何撸一个 private registry,就是启动一下这个官方 registry container 即可,然后就可以 docker push 什么的;而 mirror registry 其实也很简单,就是增加一个配置即可
首先把官方 registry 中的配置文件导出
1 2
| docker run -it --rm --entrypoint cat registry:2.5.1 \ /etc/docker/registry/config.yml > config.yml
|
registry 的配置文件内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| version: 0.1 log: fields: service: registry storage: cache: blobdescriptor: inmemory filesystem: rootdirectory: /var/lib/registry http: addr: :5000 headers: X-Content-Type-Options: [nosniff] health: storagedriver: enabled: true interval: 10s threshold: 3
|
2.3、修改 registry 配置
上一步已经将配置导出了,接下来如果想使用 mirror 功能只需在下面增加 proxy 选项即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| version: 0.1 log: fields: service: registry storage: cache: blobdescriptor: inmemory filesystem: rootdirectory: /var/lib/registry http: addr: :5000 headers: X-Content-Type-Options: [nosniff] health: storagedriver: enabled: true interval: 10s threshold: 3 proxy: remoteurl: https://registry-1.docker.io username: [username] password: [password]
|
username 与 password 是可选项,当填写 username 与 password 以后就可以从 hub pull 私有镜像
2.4、启动 mirror registry
最后只需要在启动 registry 时候将配置塞回去即可
1 2 3 4
| docker run -dt --name v2-mirror \ -v /data/registry:/var/lib/registry \ -v /etc/registry/config.yml:/etc/docker/registry/config.yml \ -p 5000:5000 registry:2.5.1
|
以上命令将启动一个 mirror registry,并且数据持久化到 /data/registry
2.5、nginx 配置 ssl
当然此时直接在 docker 启动参数总增加 --registry-mirror=http://IP:5000
,然后重启 docker 再进行 pull 即可生效,但是 5000 端口外加 http 总有点那么不装逼,所以最好增加一个 nginx 做反向代理,同时可以使用 ssl 加密,以下是一个 nginx 配置仅供参考,ssl 证书可采用 StartSSL 免费一年的 DV 证书
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| user nginx nginx;
worker_processes auto;
worker_rlimit_nofile 51200;
error_log /var/log/nginx/error.log info;
pid /var/run/nginx/nginx.pid;
events { worker_connections 51200;
use epoll;
multi_accept on;
}
http { include mime.types; default_type application/octet-stream;
log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" - $request_body ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$ssl_protocol $ssl_cipher $request_time ';
server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on;
gzip on; gzip_buffers 16 8k; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_proxied any; gzip_vary on; gzip_types text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml text/javascript application/javascript application/x-javascript text/x-json application/json application/x-web-app-manifest+json text/css text/plain text/x-component font/opentype application/x-font-ttf application/vnd.ms-fontobject image/x-icon; gzip_disable "MSIE [1-6]\.(?!.*SV1)";
open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf; }
|
mirror.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| server { listen 80; server_name your-domain; rewrite ^(.*) https://$server_name$1 permanent; }
server { listen 443; server_name your-domain; access_log /var/log/nginx/your-domain.log main;
ssl on; ssl_certificate /etc/nginx/ssl/your-domain.crt; ssl_certificate_key /etc/nginx/ssl/your-domain.key;
location / {
log_not_found on;
proxy_pass http://mirror:5000; proxy_read_timeout 300; proxy_connect_timeout 300; proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; } }
|