如何通过其主机名ping Docker网络中的其他容器? [英] How can I ping other containers in a docker network through their hostname?

查看:642
本文介绍了如何通过其主机名ping Docker网络中的其他容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple docker-compose set up as follows.

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操作,则会得到以下输出。

If I run and try to ping the other container, I get the following output.

$ 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网络检查容器的IP地址桥,我可以按如下所示对其进行ping操作。

If I search up the ip address of the containers through docker network inspect bridge, I can use that to ping as follows.

$ 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版本如下。

However, I want to do the same thing through the hostname of the containers. My docker, docker-compose, and OS versions are as follows.

$ 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


推荐答案

信用给tgogos:我只需要使用非默认桥。

Credits to tgogos: I just had to use the non-default bridge.

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

For completeness, here is my working config.

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

这篇关于如何通过其主机名ping Docker网络中的其他容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆