如何通过主机名ping docker网络中的其他容器?

时间:2018-01-22 10:12:30

标签: linux docker docker-compose ubuntu-16.04

我有一个简单的docker-compose设置如下。

version: "3"
services:
  main:
    image: python:3.5.2
    entrypoint: /usr/bin/yes
    network_mode: bridge

  another:
    image: python:3.5.2
    entrypoint: /usr/bin/yes
    network_mode: bridge

如果我运行并尝试ping另一个容器,我会得到以下输出。

$ docker-compose up -d
Recreating dockerplayground_main_1 ... 
Recreating dockerplayground_another_1 ... done
$ docker-compose ps
           Name                Command      State   Ports
---------------------------------------------------------
dockerplayground_another_1   /usr/bin/yes   Up           
dockerplayground_main_1      /usr/bin/yes   Up           
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3b256d98bf2c        python:3.5.2        "/usr/bin/yes"      7 seconds ago       Up 6 seconds                            dockerplayground_another_1
dfc04a452547        python:3.5.2        "/usr/bin/yes"      7 seconds ago       Up 6 seconds                            dockerplayground_main_1
$ docker exec -it 3b256d98bf2c ping dfc04a452547
ping: unknown host
$ docker exec -it 3b256d98bf2c ping main
ping: unknown host
$ docker exec -it 3b256d98bf2c ping another
ping: unknown host
$ docker exec -it 3b256d98bf2c ping dockerplayground_another_1
ping: unknown host
$ docker exec -it 3b256d98bf2c ping dockerplayground_main_1
ping: unknown host

如果我通过docker network inspect bridge搜索容器的IP地址,我可以使用它来ping如下。

$ docker exec -it 3b256d98bf2c ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: icmp_seq=0 ttl=64 time=0.076 ms
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.046 ms

但是,我想通过容器的主机名做同样的事情。我的docker,docker-compose和OS版本如下。

$ docker --version
Docker version 17.12.0-ce, build c97c6d6

$ docker-compose --version
docker-compose version 1.18.0, build 8dd22a9
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial

3 个答案:

答案 0 :(得分:7)

对tgogos的信任:我只需要使用非默认桥。

为了完整性,这是我的工作配置。

version: "3"
services:
  main:
    networks:
      test:
    image: python:3.5.2
    entrypoint: /usr/bin/yes

  another:
    networks:
      test:
    image: python:3.5.2
    entrypoint: /usr/bin/yes


networks:
  test:
    driver: bridge

现在ping了。

$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
faa9f96d46a9        python:3.5.2        "/usr/bin/yes"      9 seconds ago       Up 9 seconds                            dockerplayground_main_1
5b2d56ac0cd7        python:3.5.2        "/usr/bin/yes"      9 seconds ago       Up 8 seconds                            dockerplayground_another_1
$ docker exec -it faa ping another
PING another (172.18.0.2): 56 data bytes
64 bytes from 172.18.0.2: icmp_seq=0 ttl=64 time=0.054 ms
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from 172.18.0.2: icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from 172.18.0.2: icmp_seq=3 ttl=64 time=0.066 ms

答案 1 :(得分:3)

从您的撰写文件中删除此C:\Program Files\Java\jdk1.8.0_161\jre,为我工作=)。

答案 2 :(得分:1)

我尝试和工作过的另一个解决方案是将要ping的包含项与主机名显式链接。例如,我有一个 $http({ method : "POST", url : "/searchFor", data: {item: sI, dID: searchID} }).then(function(response){ if(response.data.length > 0 || response.data != 'undefined'){ $("#sidebar").css({pointerEvents: "none"}); $("#default").css({pointerEvents: "none"}); $scope.displayResults = true; $scope.sItems = []; for(let i = 0; i < response.data.length; i++){ $scope.sItems[i] = response.data[i]; } window.setTimeout(function(){ $("#closeMenuS").click(function(){ $scope.displayResults = false; $("#sidebar").css({pointerEvents: "auto"}); $("#default").css({pointerEvents: "auto"}); }); }, 30); } }, function myError(response) { $scope.myWelcome = response.statusText; }); 容器,一个postgres想要连接到它。

使用以下命令运行server

server

然后在服务器容器环境中,您可以ping(假设docker run --name server --link postgres someserver:latest 在同一网桥/网络上并且正在运行)

postgres

ping postgres 起已被弃用,建议使用网桥。

--link

这两个容器可以按名称相互ping通。