Use TLS (HTTPS) to protect the Docker daemon socket
If you need Docker to be reachable through HTTP rather than SSH in a safe manner, you can enable TLS (HTTPS) by specifying the flag and pointing Docker’s flag to a trusted CA certificate.
tlsverify``tlscacert
In the daemon mode, it only allows connections from clients authenticated by a certificate signed by that CA. In the client mode, it only connects to servers with a certificate signed by that CA.
文章目录
1、Create a CA, server and client keys with OpenSSL
使用 OpenSSL创建 CA、服务器和客户端密钥,此处提供一个生成脚本(我的网卡为ens32,注意脚本第4行)
# !/bin/bash
# configuration information
SERVER=`ip addr show ens32 | grep inet -w | awk -F' ' '{print $2}' | awk -F/ '{print $1}'`
PASSWORD="admin123"
COUNTRY="CN"
STATE="AH"
CITY="HF"
ORGANIZATION="linkedti"
ORGANIZATIONAL_UNIT="linkedti"
EMAIL="2297547342@qq.com"
echo "Start generating files ..."
# Create directory that produces the key
mkdir -p /data/certs/docker/
# Switch to the directory that produces the key
cd /data/certs/docker/
# Switch to the directory that produces the key to generate the CA private key (encrypted using AES256)
openssl genrsa -aes256 -passout pass:$PASSWORD -out ca-key.pem 4096
# Generate a CA certificate and enter the configuration information
openssl req -new -x509 -passin "pass:$PASSWORD" -days 3650 -key ca-key.pem -sha256 -out ca.pem -subj "/C=$COUNTRY/ST=$STATE/L=$CITY/O=$ORGANIZATION/OU=$ORGANIZATIONAL_UNIT/CN=$SERVER/emailAddress=$EMAIL"
# Generate a CA certificate and fill in the configuration information to generate a server certificate private key file
openssl genrsa -out server-key.pem 4096
# Generate the server certificate request file
openssl req -subj "/CN=$SERVER" -new -key server-key.pem -out server.csr
# Generate the server certificate request file. The server Ip address is used to configure the whitelist. Please replace the preceding DNS with an Ip address
sh -c 'echo "subjectAltName = IP:'$SERVER',IP:0.0.0.0" >> extfile.cnf'
sh -c 'echo "extendedKeyUsage = serverAuth" >> extfile.cnf'
# Use the CA certificate and CA key as well as the server certificate request file above to issue the certificate and generate the server certificate
openssl x509 -req -days 3650 -in server.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out server-cert.pem -extfile extfile.cnf
# The RSA private key file of the client certificate is generated
openssl genrsa -out key.pem 4096
# Generating a client certificate RSA private key File Generating a client certificate request file
openssl req -subj "/CN=client" -new -key key.pem -out client.csr
sh -c 'echo extendedKeyUsage=clientAuth >> extfile-client.cnf'
# Generate the client certificate (generated according to the above client private key file, client certificate request file)
openssl x509 -req -days 3650 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -passin "pass:$PASSWORD" -CAcreateserial -out cert.pem -extfile extfile-client.cnf
# Changing key Permissions
chmod 0400 ca-key.pem key.pem server-key.pem
chmod 0444 ca.pem server-cert.pem cert.pem
# Deleting Unnecessary Files
rm client.csr server.csr
# Copy the required files to the docker directory
cp -v {ca,server-*}.pem /etc/docker/
mkdir -p ~/.docker && cp -v {ca,key,cert}.pem ~/.docker
echo "File generation completed ! "
$ vi tls-docker.sh
# 将上述脚本内容写入文件
:wq
# 保存退出
$ chmod +x tls-docker.sh
# 执行脚本生成C&S证书文件,分别存放于 ~/.docker和/etc/docker 路径下
$ ./tls-docker.sh
2、防火墙放行端口
# 添加放行的端口
$ firewall-cmd --zone=public --add-port=2376/tcp --permanent
# 使配置生效
$ firewall-cmd --reload
# 查看放行的所有端口
$ firewall-cmd --list-port
3、配置Docker启用TLS
$ vi /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock m -H tcp://0.0.0.0:2376 -H unix://var/run/docker.sock
# 上面这行行尾追加ca验证配置
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --tlsverify --tlscacert=/data/certs/docker/ca.pem --tlscert=/data/certs/docker/server-cert.pem --tlskey=/data/certs/docker/server-key.pem -H tcp://0.0.0.0:2376 -H unix://var/run/docker.sock
4、重启docker
$ systemctl daemon-reload && systemctl restart docker
5、保存客户端证书
# 客户端证书存放在 ~/.docker
# 将客户端证书保存到windows文件系统
scp root@192.168.3.150:/root/.docker/* E:\VIRTUAL MACHINE\docker_certs
6、IDEA连接Docker
File -> setting -> search docker
certificates folder处选择客户端证书所在位置
左下角显示Connection successful即为成功