從 docker 容器連接到 docker 主機

從 docker 容器連接到 docker 主機

我有一個設置,可以在 Docker 容器中運行網站的所有部分。我的 nginx 監聽埠 80 和 443,在容器中運作。

363292a98545        scivm/nginx-django-scivmcom:latest   /usr/bin/supervisord   12 days ago         Ghost               0.0.0.0:40001->22/tcp, 88.198.57.112:443->443/tcp, 88.198.57.112:80->80/tcp     lonely_feynmann           

我想在另一個容器中設定服務的代理。此容器綁定到主機上的連接埠 3000:

b38c8ef72d0a        mazzolino/strider-dind:latest        wrapdocker /usr/bin/   41 minutes ago      Up 41 minutes       0.0.0.0:3000->3000/tcp, 22/tcp, 27017/tcp                                       distracted_einstein      

我的 docker 主機上的 iptables 看起來像這樣:

root@Ubuntu-1204-precise-64-minimal /var/run # iptables -L
Chain INPUT (policy ACCEPT) target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:https
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:8000
DROP       all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere             ctstate RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

由於 iptables 配置,我無法在容器內連接到主機上的連接埠 3000。

我不想向公共互聯網開放端口 3000。

有沒有辦法在連接埠 3000 上開啟容器和主機之間的直接橋接?

或者我應該修改我的 iptables 以接受 docker ip 範圍?

答案1

你所需要的只是 Docker 的連結能力[已棄用]

只需擺脫您嘗試做的所有複雜的事情並開始使用命名容器,然後將它們相互連結即可。

答案2

埃利亞斯的答案是正確的,但連結很長且令人困惑。這是一個簡單的總結:

首先,運行要連結到的容器,並將其命名:

sudo docker run -d --name db training/postgres

然後運行另一個容器,將其連結到第一個容器:

sudo docker run -d -P --name web --link db:db training/webapp python app.py

從第一個容器到第二個容器的連結被放入 中/etc/hosts。所以你可以像主機名稱一樣使用它。例如:

sudo docker run --name web --link db:db training/webapp ping db

相關內容