How do I set up linkage between Docker containers so that restarting won't break it?

前端 未结 11 2261
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 05:40

I have a few Docker containers running like:

  • Nginx
  • Web app 1
  • Web app 2
  • PostgreSQL

Since Nginx needs to connect to the

11条回答
  •  情书的邮戳
    2020-12-02 05:54

    Links are for a specific container, not based on the name of a container. So the moment you remove a container, the link is disconnected and the new container (even with the same name) will not automatically take its place.

    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

提交回复
热议问题