国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Web服務器 > 正文

詳解docker容器間通信的一種方法

2024-09-01 13:50:52
字體:
來源:轉載
供稿:網友

以我的ghost博客為例進行說明,我在VPS上用docker啟動了兩個ghost博客,還有一個Nginx做反向代理,將兩個域名分別指向兩個博客。

docker啟動命令

ghost:

docker run -e NODE_ENV=production --name ghost1 -v /path/to/data/ghost/ghost1/:/var/lib/ghost -d ghostdocker run -e NODE_ENV=production --name ghost2 -v /path/to/data/ghost/ghost2/:/var/lib/ghost -d ghost

nginx:

 

復制代碼 代碼如下:

docker run -p 80:80 --name nginx --link ghost1 --link ghost2 -v /path/to/data/nginx/nginx.conf:/etc/nginx/nginx.conf -d nginx

 

先啟動兩個ghost,然后啟動nginx。使用--link參數將容器“鏈接”到一起,此參數會在容器中加入環境變量并在/etc/hosts中插入一條容器名與IP的映射

root@fabfd4bacfda:/# cat /etc/hosts172.17.0.3   ghost1 d19c0134011a172.17.0.5   ghost2 0e2e66ba70e0172.17.0.4   fabfd4bacfda

設置nginx反向代理

修改nginx.conf,在http段內添加如下內容

http {  server {    listen 80;    server_name www.domain1.tk domain1.tk;    location / {      proxy_pass http://ghost1:2368;      proxy_redirect   off;      proxy_set_header  Host       $host;      proxy_set_header  X-Real-IP    $remote_addr;      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    }  }  server {    listen 80;    server_name www.domain2.tk domain2.tk;    location / {      proxy_pass http://ghost2:2368;      proxy_redirect   off;      proxy_set_header  Host       $host;      proxy_set_header  X-Real-IP    $remote_addr;      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    }  }}

注意proxy_pass的值proxy_pass http://ghost2:2368;。 ghost2是nginx容器/etc/hosts中的一條,是由--link參數添加進來的。

設置完這些后,nginx就會將兩個域名的請求分別代理到兩個博客中。

補充

容器重啟后IP可能變化,所以直接在nginx.conf中指定IP并不是一個好方法。使用--link時hosts文件會隨著容器IP的變化更新,所以使用域名才是更容易維護的方法。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪洞县| 双牌县| 彭阳县| 云安县| 青川县| 南召县| 五常市| 达拉特旗| 防城港市| 鄂托克前旗| 舒兰市| 观塘区| 吴忠市| 扎赉特旗| 怀宁县| 利川市| 剑河县| 深州市| 阳高县| 公主岭市| 通州市| 星子县| 乡宁县| 扶沟县| 社会| 宜都市| 苏州市| 绥化市| 固始县| 安岳县| 滦南县| 罗平县| 洛扎县| 乌兰浩特市| 沛县| 中江县| 栾川县| 大同县| 广水市| 清镇市| 杭锦后旗|