sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
sudo systemctl start docker
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
sudo systemctl daemon-reload
sudo systemctl restart docker
mkdir -p /usr/local/ca
cd /usr/local/ca
vi ca.sh
按A按键切换为输入模式,然后把下面代码粘贴上去
#!/bin/bash
SERVER="服务器外网ip"
PASSWORD="yinfeng"
COUNTRY="CN"
STATE="shanghai"
CITY="shanghai"
ORGANIZATION="yinfeng"
ORGANIZATIONAL_UNIT="dev"
EMAIL="[email protected]"
echo "starting..."
cd /usr/local/ca
openssl genrsa -aes256 -passout pass:$PASSWORD -out ca-key.pem 4096
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"
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=$SERVER" -new -key server-key.pem -out server.csr
sh -c 'echo "subjectAltName = IP:'$SERVER',IP:0.0.0.0" >> extfile.cnf'
sh -c 'echo "extendedKeyUsage = serverAuth" >> extfile.cn'
sh -c 'echo "extendedKeyUsage = serverAuth" >> extfile.cnf'
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
openssl genrsa -out key.pem 4096
openssl req -subj "/CN=client" -new -key key.pem -out client.csr
sh -c 'echo extendedKeyUsage=clientAuth >> extfile-client.cnf'
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
rm client.csr server.csr
cp server-*.pem /etc/docker/
cp ca.pem /etc/docker/
echo "========end========"
sh ca.sh
执行完成后会生成以下文件,搜了半天,总结了目前全网唯一可用的脚本,嘿嘿
vim /lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/usr/local/ca/ca.pem --tlscert=/usr/local/ca/server-cert.pem --tlskey=/usr/local/ca/server-key.pem -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
systemctl daemon-reload && systemctl restart docker
我使用的是sz,如果没有的需要先安装
yum -y install lrzsz
安装成功后执行
sz ca.pem cert.pem key.pem
docker --tlsverify --tlscacert=ca.pem --tlscert=cert.pem --tlskey=key.pem -H=服务器外网ip:2375 version
最后总结一下,自己服务配置docker外网链接时,一定要弄好加密措施,否则很容易挂马,目前网上的步骤一般都只是tcp免密链接,很不安全,我昨天试了一下,不到半个小时就被扫描挂马了,所以才决定写份笔记记录一下自己的部署过程,希望大家引以为鉴。
这个是阿里云的告警信息
后面有时间还会再写一篇idea一键加密部署springboot到docker容器,肝文不易,麻烦老铁们点个赞,谢谢大家支持。