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

前端 未结 11 2228
隐瞒了意图╮
隐瞒了意图╮ 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 06:10

    If anyone is still curious, you have to use the host entries in /etc/hosts file of each docker container and should not depend on ENV variables as they are not updated automatically.

    There will be a host file entry for each of the linked container in the format LINKEDCONTAINERNAME_PORT_PORTNUMBER_TCP etc..

    The following is from docker docs

    Important notes on Docker environment variables

    Unlike host entries in the /etc/hosts file, IP addresses stored in the environment variables are not automatically updated if the source container is restarted. We recommend using the host entries in /etc/hosts to resolve the IP address of linked containers.

    These environment variables are only set for the first process in the container. Some daemons, such as sshd, will scrub them when spawning shells for connection.

提交回复
热议问题