docker container can't connect to other contai

2019-08-17 06:41发布

I have 2 different containers running on my server(app and web_server). They are in the same network, because I put them in the same network and that network's driver is bridge.

networks:
  app-network:
    driver: bridge

Now, I have 3rd container, which needs to connect one of the 2 containers I mentioned. I try to connect from 3rd container to 1st container like this: app:9000 , but it says host not found. I guess this is because they are in different networks. What should I do to make it work?

1条回答
甜甜的少女心
2楼-- · 2019-08-17 07:38

You need both containers join the same network. either by creating a new network and make then join it or one of them join the other container's network.

the docker-compose.yml where you have container1 and container2:

  • define an external network which belongs to container3
  • add one of these two containers two the external network as shown in the first snippet

the docker-compose.yml where you have container3 will be a normal docker-compose with a bridge network.

you need to make sure to start the third container first in order to allow the third network to be created first or you can create the third network manually using docker cli and change the definition to external

#docker-compose.yml

container1:
  networks:
    - first_network
    - second_network

networks:
  first_network:
    driver: bridge
  second_network:
    external: true
#docker-compose.yml
container3:
  networks:
    - second_network

networks:
  second_network:
    driver: bridge
查看更多
登录 后发表回答