[英]Exposing ports to all containers, not only the host
我可以從主機ping在docker容器中運行的InfluxDB
,並在主機中暴露端口:
» curl -k -L -I https://localhost:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 2bb1059b-360e-11e7-8001-000000000000
X-Influxdb-Version: 1.2.0
Date: Thu, 11 May 2017 05:53:34 GMT
我運行一個Ubuntu 16.04
docker容器(已安裝curl),該容器與InfluxDB
容器連接到同一網絡,但無法ping localhost:8086
。 最終,我發現需要使用InfluxDB
容器的IP地址ping:
root@4a5457a5e297:/# curl -k -sL -I https://172.18.0.1:8086/ping
HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: d8ab4282-360e-11e7-8002-000000000000
X-Influxdb-Version: 1.2.0
Date: Thu, 11 May 2017 05:58:25 GMT
這意味着首先我需要找出InfluxDB
容器的IP地址。 我只是猜測,因為我無法在InfluxDB
容器中執行ifconfig
,而且我也不知道如何列出所有正在運行的容器的IP: docker ps
不會顯示它。
因此, InfluxDB
的端口在主機中作為localhost:8086
公開,但未對其他容器公開。 一些問題:
localhost:8086
? 我運行一個Ubuntu 16.04 Docker容器(已安裝curl),並與InfluxDB容器連接到同一網絡,但無法ping localhost:8086。 最終,我發現需要使用InfluxDB容器的IP地址ping:
當與同一網絡上的其他容器“說話”時,請使用容器名稱。 您可以使用--name influxdb
強制執行此--name influxdb
然后,您就可以從同一網絡使用curl http://influxdb:8086
正如Rawkode所說,這是可能的。 但是由於某些原因,我無法使用默認網橋網絡執行此操作。
$ docker run -itd --rm --name test1 alpine /bin/sh
726cd933446df40e78e760d86256e11b1e786d83057a9d075c05c4d38240656c
$ docker run -itd --rm --name test2 alpine /bin/sh
c6837529c37f486edbc3a7743a6b127b9bdaae8a619564368697137fd8ae5622
$ docker container exec test1 ping test2
ping: bad address 'test2'
$ docker container exec test2 ping test1
ping: bad address 'test1'
$ docker container stop test1
test1
$ docker container stop test2
test2
創建測試網絡將起作用:
$ docker network create testnet
ca1db96c3a533033c68d8885fac2f354919edc810e0f376f06f86e45d3050b35
$ docker run -itd --rm --name test1 --network testnet alpine /bin/sh
187f2c8534504e6a8db96c0a731c735976b19b0a710e162f3537b2f5f16d7b05
$ docker run -itd --rm --name test2 --network testnet alpine /bin/sh
7efd386c13962a56dc074903373848174fb5cdb649038a6e67fbb2f0f8bde74b
$ docker container exec test1 ping test2
PING test2 (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.076 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.165 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.159 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.077 ms
^C
$ docker container exec test2 ping test1
PING test1 (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: seq=0 ttl=64 time=0.063 ms
64 bytes from 172.18.0.2: seq=1 ttl=64 time=0.235 ms
64 bytes from 172.18.0.2: seq=2 ttl=64 time=0.218 ms
64 bytes from 172.18.0.2: seq=3 ttl=64 time=0.099 ms
^C
$
容器可以直接通信,您不必使用localhost。 看一下如何暴露端口
基本上,您有三個選擇:
- 既不指定EXPOSE也不指定-p。
- 僅指定EXPOSE。
- 指定EXPOSE和-p。
如果未指定任何內容,則只能從容器本身內部訪問任何位置的容器中的服務。
如果暴露端口,則無法從Docker外部訪問容器中的服務,而可以從其他Docker容器內部訪問容器中的服務。 因此,這對於容器間的通信很有用。
如果使用EXPOSE和-pa port,則可以從任何地方訪問容器中的服務,甚至可以從Docker外部訪問。
在這里找到: https : //stackoverflow.com/a/22150099/1561148
$ docker network inspect testnet | grep IPv4
"IPv4Address": "172.18.0.2/16",
"IPv4Address": "172.18.0.3/16",
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.