How to communicate between Docker containers via “hostname”

后端 未结 5 1218
离开以前
离开以前 2020-11-27 10:02

I plan to split my monolthic server up into many small docker containers but haven\'t found a good solution for \"inter-container communication\" yet. This is my target scen

5条回答
  •  心在旅途
    2020-11-27 10:16

    The new networking feature allows you to connect to containers by their name, so if you create a new network, any container connected to that network can reach other containers by their name. Example:

    1) Create new network

    $ docker network create        
    

    2) Connect containers to network

    $ docker run --net= ...
    

    or

    $ docker network connect  
    

    3) Ping container by name

    docker exec -ti  ping  
    
    64 bytes from c1 (172.18.0.4): icmp_seq=1 ttl=64 time=0.137 ms
    64 bytes from c1 (172.18.0.4): icmp_seq=2 ttl=64 time=0.073 ms
    64 bytes from c1 (172.18.0.4): icmp_seq=3 ttl=64 time=0.074 ms
    64 bytes from c1 (172.18.0.4): icmp_seq=4 ttl=64 time=0.074 ms
    

    See this section of the documentation;

    Note: Unlike legacy links the new networking will not create environment variables, nor share environment variables with other containers.

    This feature currently doesn't support aliases

提交回复
热议问题