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

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

    Another alternative is to use the --net container:$CONTAINER_ID option.

    Step 1: Create "network" containers

    docker run --name db_net ubuntu:14.04 sleep infinity
    docker run --name app1_net --link db_net:db ubuntu:14.04 sleep infinity
    docker run --name app2_net --link db_net:db ubuntu:14.04 sleep infinity
    docker run -p 80 -p 443 --name nginx_net --link app1_net:app1 --link app2_net:app2 ubuntu:14.04 sleep infinity
    

    Step 2: Inject services into "network" containers

    docker run --name db --net container:db_net pgsql
    docker run --name app1 --net container:app1_net app1
    docker run --name app2 --net container:app1_net app2
    docker run --name nginx --net container:app1_net nginx
    

    As long as you do not touch the "network" containers, the IP addresses of your links should not change.

提交回复
热议问题