环境:IntelliJ IDEA 2018.1.6 x64、CentOS 7
[root@localhost ~]# vim /usr/lib/systemd/system/docker.service
ExecStart
属性value值改为/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 2375 -j ACCEPT
[root@localhost ~]# iptables-save
# Generated by iptables-save v1.4.21 on Wed Oct 17 09:33:07 2018
*nat
:PREROUTING ACCEPT [31:5206]
:INPUT ACCEPT [31:5206]
:OUTPUT ACCEPT [4:304]
:POSTROUTING ACCEPT [4:304]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
COMMIT
# Completed on Wed Oct 17 09:33:07 2018
# Generated by iptables-save v1.4.21 on Wed Oct 17 09:33:07 2018
*filter
:INPUT ACCEPT [24:4973]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [3:340]
:DOCKER - [0:0]
:DOCKER-ISOLATION-STAGE-1 - [0:0]
:DOCKER-ISOLATION-STAGE-2 - [0:0]
:DOCKER-USER - [0:0]
-A INPUT -p tcp -m tcp --dport 2375 -j ACCEPT
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
COMMIT
# Completed on Wed Oct 17 09:33:07 2018
我这里找了个相对比较小的jdk,是在官方镜像库找到的
也可以在国内的镜像库里找,如:FROM hub.c.163.com/library/java:8-jre
FROM 99taxis/mini-java8
ADD target/*.jar idea-docker-deploy.jar
EXPOSE 8765
ENTRYPOINT ["java", "-jar", "idea-docker-deploy.jar"]
项目右键新建一个没有后缀的File
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
idea-docker-deploy latest 0b9861752b28 11 minutes ago 210MB
registry.cn-hangzhou.aliyuncs.com/zhaoyoung/mycentos 1.1 de8fc9f45769 26 hours ago 455MB
mycentos 1.1 de8fc9f45769 26 hours ago 455MB
zhaoyoungtomcat9 latest 124517434916 45 hours ago 751MB
myip_son latest 6c9507aea358 3 days ago 398MB
myip_father latest 2c22e721607a 3 days ago 299MB
myip2 latest dcbb4656e640 3 days ago 299MB
myip latest 9e3c14f76b1d 3 days ago 299MB
mycentos 1.0 c2d4f6acb9af 4 days ago 455MB
zhaoyoung/nodocstomcat 1.0 84498728984a 6 days ago 463MB
centos latest 75835a67d134 7 days ago 200MB
redis 3.2 a17eb18b1c62 2 weeks ago 76MB
tomcat latest 41a54fe1f79d 4 weeks ago 463MB
hello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB
mysql 5.6 1f47fade220d 6 weeks ago 256MB
99taxis/mini-java8 latest 45f8a8f0a77a 16 months ago 194MB
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f9cc9e975bb 0b9861752b28 "java -jar idea-dock…" 11 minutes ago Up 11 minutes 0.0.0.0:8765->8765/tcp idea-docker-deploy
前面提到的配置是允许所有人都可以访问的,因为docker默认是root权限的,你把2375端口暴露在外面,意味着别人随时都可以提取到你服务器的root权限,是很容易被黑客黑的,因此,docker官方推荐使用加密的tcp连接,以Https的方式与客户端建立连接
[root@localhost ~]# mkdir -p /usr/local/ca
[root@localhost ~]# cd /usr/local/ca/
需要连续输入两次相同的密码
[root@localhost ca]# openssl genrsa -aes256 -out ca-key.pem 4096
Generating RSA private key, 4096 bit long modulus
...................++
..........................................................................................................................................................................................................................................................................................................................................................++
e is 65537 (0x10001)
Enter pass phrase for ca-key.pem:
Verifying - Enter pass phrase for ca-key.pem:
[root@localhost ca]# openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
Enter pass phrase for ca-key.pem:
Enter pass phrase for ca-key.pem:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:zj
Locality Name (eg, city) [Default City]:hz
Organization Name (eg, company) [Default Company Ltd]:qdsg
Organizational Unit Name (eg, section) []:qdsg
Common Name (eg, your name or your server's hostname) []:qdsg
Email Address []:[email protected]
[root@localhost ca]# openssl genrsa -out server-key.pem 4096
Generating RSA private key, 4096 bit long modulus
...........................++
................++
e is 65537 (0x10001)
[root@localhost ca]# openssl genrsa -out server-key.pem 4096
Generating RSA private key, 4096 bit long modulus
.................................................................................................................................................++
.................................................++
e is 65537 (0x10001)
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr
比如
openssl req -subj "/CN=192.168.1.106" -sha256 -new -key server-key.pem -out server.csr
或
openssl req -subj "/CN=www.baidu.com" -sha256 -new -key server-key.pem -out server.csr
我这里使用局域网进行测试
[root@localhost ca]# openssl req -subj "/CN=192.168.1.106" -sha256 -new -key server-key.pem -out server.csr
也就是你接下来要允许那些ip可以连接到服务器的docker,因为已经是ssl连接,所以我推荐配置0.0.0.0,也就是所有ip都可以连接(但只有拥有证书的才可以连接成功),这样配置好之后公司其他人也可以使用。如果你不想这样,那你可以配置ip,用逗号分隔开。下面的$Host依旧是你服务器外网的IP或者域名,请自行替换。
echo subjectAltName = IP:$HOST,IP:0.0.0.0 >> extfile.cnf
echo subjectAltName = DNS:$HOST,IP:0.0.0.0 >> extfile.cnf
我这里使用局域网进行测试
[root@localhost ca]# echo subjectAltName = IP:192.168.1.106,IP:0.0.0.0 >> extfile.cnf
[root@localhost ca]# echo extendedKeyUsage = serverAuth >> extfile.cnf
[root@localhost ca]# openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem \-CAcreateserial -out server-cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=192.168.1.106
Getting CA Private Key
Enter pass phrase for ca-key.pem:
[root@localhost ca]# openssl genrsa -out key.pem 4096
Generating RSA private key, 4096 bit long modulus
...........................................................................................................................................................................................................................................................................................................................................................................++
......................................................................................................................................++
e is 65537 (0x10001)
[root@localhost ca]# openssl req -subj '/CN=client' -new -key key.pem -out client.csr
[root@localhost ca]# echo extendedKeyUsage = clientAuth >> extfile.cnf
[root@localhost ca]# openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem \-CAcreateserial -out cert.pem -extfile extfile.cnf
Signature ok
subject=/CN=client
Getting CA Private Key
Enter pass phrase for ca-key.pem:
[root@localhost ca]# rm -v client.csr server.csr
rm:是否删除普通文件 "client.csr"?y
已删除"client.csr"
[root@localhost ca]# chmod -v 0400 ca-key.pem key.pem server-key.pem
mode of "ca-key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
mode of "key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
mode of "server-key.pem" changed from 0644 (rw-r--r--) to 0400 (r--------)
[root@localhost ca]# chmod -v 0444 ca.pem server-cert.pem cert.pem
mode of "ca.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
mode of "server-cert.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
mode of "cert.pem" changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
[root@localhost ca]# cp server-*.pem /etc/docker/
[root@localhost ca]# cp ca.pem /etc/docker/
[root@localhost ca]# vim /lib/systemd/system/docker.service
将
ExecStart=/usr/bin/dockerd
替换为:
ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server-cert.pem --tlskey=/etc/docker/server-key.pem -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ca]# /sbin/iptables -I INPUT -p tcp --dport 2376 -j ACCEPT
[root@localhost ca]# iptables-save
# Generated by iptables-save v1.4.21 on Wed Oct 17 14:47:38 2018
*nat
:PREROUTING ACCEPT [225:14836]
:INPUT ACCEPT [225:14836]
:OUTPUT ACCEPT [1:76]
:POSTROUTING ACCEPT [1:76]
:DOCKER - [0:0]
-A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
-A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
-A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
-A DOCKER -i docker0 -j RETURN
COMMIT
# Completed on Wed Oct 17 14:47:38 2018
# Generated by iptables-save v1.4.21 on Wed Oct 17 14:47:38 2018
*filter
:INPUT ACCEPT [8:2858]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [39:30400]
:DOCKER - [0:0]
:DOCKER-ISOLATION-STAGE-1 - [0:0]
:DOCKER-ISOLATION-STAGE-2 - [0:0]
:DOCKER-USER - [0:0]
-A INPUT -p tcp -m tcp --dport 2376 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2375 -j ACCEPT
-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-ISOLATION-STAGE-1
-A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -o docker0 -j DOCKER
-A FORWARD -i docker0 ! -o docker0 -j ACCEPT
-A FORWARD -i docker0 -o docker0 -j ACCEPT
-A DOCKER-ISOLATION-STAGE-1 -i docker0 ! -o docker0 -j DOCKER-ISOLATION-STAGE-2
-A DOCKER-ISOLATION-STAGE-1 -j RETURN
-A DOCKER-ISOLATION-STAGE-2 -o docker0 -j DROP
-A DOCKER-ISOLATION-STAGE-2 -j RETURN
-A DOCKER-USER -j RETURN
COMMIT
# Completed on Wed Oct 17 14:47:38 2018
[root@localhost ca]# service docker restart