#docker TLS远程访问 ##生成证书
#创建ca-key.pem,需要输入密码,后面会用到
openssl genrsa -aes256 -out ca-key.pem 2048
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
openssl genrsa -out server-key.pem 2048
#修改`$HOST`,建议设置为本机IP地址。
openssl req -subj "/CN=$HOST" -new -key server-key.pem -out server.csr
#修改IP地址,
echo subjectAltName = IP:10.10.10.20,IP:127.0.0.1 > extfile.cnf
openssl x509 -req -days 365 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
openssl genrsa -out key.pem 2048
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth > extfile.cnf
openssl x509 -req -days 365 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
#删除下面两个文件
rm -v client.csr server.csr
#修改权限
chmod -v 0400 ca-key.pem key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem cert.pem
##docker远程访问 ###修改/etc/default/docker
文件,添加:
DOCKER_OPTS="--tlsverify -H=unix:///var/run/docker.sock -H=0.0.0.0:4243 --tlscacert=/root/key/ca.pem --tlscert=/root/key/server-cert.pem --tlskey=/root/key/server-key.pem"
###重启docker
service docker restart
###远程访问:
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H=$HOST:4243 docker命令
###curl访问:
curl https://127.0.0.1:4243/images/json --cert /root/key/cert.pem --key /root/key/key.pem --cacert /root/key/ca.pem
###python模块docker-py访问:
#!/usr/bin/python
# -*- coding:utf-8 -*-
__author__ = 'Zhenhua Zhang'
import docker
import json
tls_config = docker.tls.TLSConfig(
client_cert=('cert.pem','key.pem'),
verify='ca.pem'
)
c = docker.Client(base_url='https://127.0.0.1:4243',version='1.19',timeout=10,tls=tls_config)
s = c.images()
print s[0]['Id']