基于Nginx身份验证代理
通过这个方法,你可以实现在你的registry前面增加一层基本的身份校验功能的方向代理。
这种模型赋予你使用任意的身份验证后端的能力,你需要通过一个二次验证的机制去实现它们,在你的代理内部。而且要求把TLS终端从registry端迁到它的内部。
此外,在你的通信链路中添加了额外的http层,增加了部署、维护、调试的复杂度,可能还会引入一些信息的问题(比较典型,Nginx是缓存客户请求到硬盘中,如果你正在处理巨大的图片且频繁请求时,会导致一个主机问题。)
### 要求
你必须通读整个部署的入门指南。
如果你还没有,请你先花点时间去看看。
此时,假定你具备了如下的条件:
- 你了解Docker安全的要求且知道如何去正确地设置你的docker引擎。
- 你已经安装了Docker Compose。
- 强烈的建议你去一个知名的CA获取一个证书去替代自签名的证书。
- 在当前的文件下,你有一个符合X509规范的
domain.crt
和domain.key
,为你的myregistrydomain.com
(或任何一个你想使用的域名) - 确信你已经停止并删除之前运行的registry(通常
docker stop registry && docker rm -v registry
)
设置
再次阅读要求。
准备好了吗?
运行下面的指令:
mkdir -p auth
mkdir -p data
# This is the main nginx configuration you will use
cat <<EOF > auth/registry.conf
upstream docker-registry {
server registry:5000;
}
server {
listen 443 ssl;
server_name myregistrydomain.com;
# SSL
ssl_certificate /etc/nginx/conf.d/domain.crt;
ssl_certificate_key /etc/nginx/conf.d/domain.key;
# disable any limits to avoid HTTP 413 for large image uploads
client_max_body_size 0;
# required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
chunked_transfer_encoding on;
location /v2/ {
# Do not allow connections from docker 1.5 and earlier
# docker pre-1.6.0 did not properly set the user agent on ping, catch "Go *" user agents
if (\$http_user_agent ~ "^(docker\/1\.(3|4|5(?!\.[0-9]-dev))|Go ).*\$" ) {
return 404;
}
# To add basic authentication to v2 use auth_basic setting plus add_header
auth_basic "Registry realm";
auth_basic_user_file /etc/nginx/conf.d/htpasswd;
add_header 'Docker-Distribution-Api-Version' 'registry/2.0' always;
proxy_pass http://docker-registry;
proxy_set_header Host \$http_host; # required for docker client's sake
proxy_set_header X-Real-IP \$remote_addr; # pass on real client's IP
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
proxy_read_timeout 900;
}
}
EOF
# Now, create a password file for "testuser" and "testpassword"
htpasswd -bn testuser testpassword > auth/htpasswd
# Copy over your certificate files
cp domain.crt auth
cp domain.key auth
# Now create your compose file
cat <<EOF > docker-compose.yml
nginx:
image: "nginx:1.9"
ports:
- 5043:443
links:
- registry:registry
volumes:
- `pwd`/auth/:/etc/nginx/conf.d
registry:
image: registry:2
ports:
- 127.0.0.1:5000:5000
environment:
REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY: /data
volumes:
- `pwd`/data:/data
EOF
启动和关闭
通过上述的配置,你现在可以:
docker-compose up -d
去启动你的registrydocker login myregistrydomain.com:5043
(使用testuser
和testpassword
)docker tag ubuntu myregistrydomain.com:5043/toto
docker push myregistrydomain.com:5043/toto
Docker对证书还有抱怨吗?
答案你当然的了,因为你使用了一个自己签名的证书,忽略了警告。
假如你坚持使用它们,你必须信任它,在操作系统级别上。
通常,在乌班图中:
cp auth/domain.crt /usr/local/share/ca-certificates/myregistrydomain.com.crt
update-ca-certificates
在红帽的系统中:
cp auth/domain.crt /etc/pki/ca-trust/source/anchors/myregistrydomain.com.crt
update-ca-trust
现在:
service docker stop && service docker start
(或使用其他的方法重启docker引擎)docker-compose up -d
启动registry