Middle
How to connect two Docker containers into one network on the same host?
sobes.tech AI
Answer from AI
To connect two Docker containers into a single network on the same host, you need to create a custom Docker network and run the containers within this network. This allows the containers to communicate with each other by container names.
Example:
- Create a network:
docker network create my_network
- Run the containers, connecting them to the network:
docker run -d --name container1 --network my_network image1
docker run -d --name container2 --network my_network image2
Now, the containers can refer to each other by the names container1 and container2 within the my_network network.
This is convenient for microservices when internal communication needs to be ensured without exposing ports externally.