Docker配置TLS加密,IDEA远程连接Docker

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即为成功

在这里插入图片描述
参考:保护 Docker 守护程序套接字|Docker 文档

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LemonCoder呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值