nginx反向代理通路其他容器
一、 Docker容器互訪三種方式
參考:https://www.cnblogs.com/shenh/p/9714547.html
我們都知道docker容器之間是互相隔離的,不能互相通路,但如果有些依賴關系的服務要怎麼辦呢。下面介紹三種方法解決容器互通路題。容器之間的互相通信也是為了項目能夠更好的拓展,同時也可對目前正在看的nginx的方向代理容器有所幫助。建議直接學習方式三
方式一、虛拟ip通路
安裝docker時,docker會預設建立一個内部的橋接網絡docker0,每建立一個容器配置設定一個虛拟網卡,容器之間可以根據ip互相通路。
[[email protected] /]# [[email protected] ~]# ifconfig
......
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:35ff:feac:66d8 prefixlen 64 scopeid 0x20<link>
ether 02:42:35:ac:66:d8 txqueuelen 0 (Ethernet)
RX packets 4018 bytes 266467 (260.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4226 bytes 33935667 (32.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
......
運作一個centos鏡像, 檢視ip位址得到:172.17.0.7
[[email protected] ~]# docker run -it --name centos-1 docker.io/centos:latest
[[email protected] /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.7 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:7 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:07 txqueuelen 0 (Ethernet)
RX packets 16 bytes 1296 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
以同樣的指令再起一個容器,檢視ip位址得到:172.17.0.8
[[email protected] ~]# docker run -it --name centos-2 docker.io/centos:latest
[[email protected] /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.8 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:8 prefixlen 64 scopeid 0x20<link>
ether 02:42:ac:11:00:08 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 648 (648.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
容器内部ping測試結果如下:
[[email protected] /]# ping 172.17.0.7
PING 172.17.0.7 (172.17.0.7) 56(84) bytes of data.
64 bytes from 172.17.0.7: icmp_seq=1 ttl=64 time=0.205 ms
64 bytes from 172.17.0.7: icmp_seq=2 ttl=64 time=0.119 ms
64 bytes from 172.17.0.7: icmp_seq=3 ttl=64 time=0.118 ms
64 bytes from 172.17.0.7: icmp_seq=4 ttl=64 time=0.101 ms
這種方式必須知道每個容器的ip,在實際使用中并不實用。
方式二、link
運作容器的時候加上參數link
運作第一個容器
docker run -it --name centos-1 docker.io/centos:latest
運作第二個容器
[[email protected] ~]# docker run -it --name centos-2 --link centos-1:centos-1 docker.io/centos:latest
–link:參數中第一個centos-1是容器名,第二個centos-1是定義的容器别名(使用别名通路容器),為了友善使用,一般别名預設容器名。
測試結果如下:
[[email protected] /]# ping centos-1
PING centos-1 (172.17.0.7) 56(84) bytes of data.
64 bytes from centos-1 (172.17.0.7): icmp_seq=1 ttl=64 time=0.210 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=2 ttl=64 time=0.116 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1 (172.17.0.7): icmp_seq=4 ttl=64 time=0.114 ms
此方法對容器建立的順序有要求,如果叢集内部多個容器要互訪,使用就不太友善。
方式三、建立bridge網絡
1.安裝好docker後,運作如下指令建立bridge網絡:docker network create testnet
查詢到新建立的bridge testnet。

檢視網橋下的容器ip資訊: docker network inspect dyl_testnet
2.運作容器連接配接到testnet網絡。
使用方法:docker run -it --name <容器名> —network --network-alias <網絡别名> <鏡像名>
[[email protected] ~]# docker run -it --name centos-1 --network testnet --network-alias centos-1 docker.io/centos:latest
[[email protected] ~]# docker run -it --name centos-2 --network testnet --network-alias centos-2 docker.io/centos:latest
3.從一個容器ping另外一個容器,測試結果如下:
[[email protected] /]# ping centos-1
PING centos-1 (172.20.0.2) 56(84) bytes of data.
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=1 ttl=64 time=0.158 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=2 ttl=64 time=0.108 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=3 ttl=64 time=0.112 ms
64 bytes from centos-1.testnet (172.20.0.2): icmp_seq=4 ttl=64 time=0.113 ms
4.若通路容器中服務,可以使用這用方式通路 <網絡别名>:<服務端口号>
推薦使用這種方法,自定義網絡,因為使用的是網絡别名,可以不用顧慮ip是否變動,隻要連接配接到docker内部bright網絡即可互訪。bridge也可以建立多個,隔離在不同的網段。brig
二、實作(bridge方式)
(1)、運作node docker
server.js
const express = require('express');
// Constants
const PORT = 8081;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/api', (req, res) => {
res.send('Hello World');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
docker run -it --network testnet --network-alias node_web_app diyunlong/node_web_app
(2)、運作nginx docker
為了儲存資料和友善修改配置檔案,需要對nginx進行目錄挂載,前面有具體介紹
default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
location / {
root /usr/share/nginx/html; // 這個不用改
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ^~ /api {
proxy_pass http://node_web_app:8081/api;
}
}
docker run -it --name=vue_nginx --privileged=true --network dyl_testnet --network-alias nginx -p 8091:8091 -v /home/dyl/nginxdocker/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/dyl/nginxdocker/html:/usr/share/nginx/html -v /home/dyl/nginxdocker/logs:/var/log/nginx -v /home/dyl/nginxdocker/conf.d/default.conf:/etc/nginx/conf.d/default.conf nginx
(3)、測試:
// 在宿主主機上
[[email protected] ~]# curl http://127.18.0.2:8081
Hello World
// 進入nginx,試着cur另一個node docker容器ip
[[email protected] ~]# docker exec -it 11b10c5142b2 /bin/bash
[email protected]:/# curl http://127.18.0.2:8081
curl: (7) Failed to connect to 127.18.0.2 port 8081: Connection refused
// 換一種方式curl
[email protected]:/# curl node_web_app:8081
Hello World
流程如下:
http://47****2:8080/api > nginx docker > ^~ /api > http://node_web_app:8081/api > nodejs