Let's see how we can get containers to talk to each other over a Docker network.
Create a Network
We can create a network for containers to be added to:
# List networks
docker network ls
# Create a network
docker network create appnet
Then we can add containers to that network as we run them:
docker run --rm -d \
--name=app \
--network=appnet\
shippingdocker/app:latest
docker run --rm -d \
--name=mysql \
--network=appnet \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=homestead \
-e MYSQL_USER=homestead \
-e MYSQL_USER_PASSWORD=secret \
mysql:5.7
This creates two containers (see the videos for details on the MySQL container), and adds them into the new network appnet
.
The containers then can reference eachother - the hostname for each container is taken from the --name
given to the container. This lets the app
container talk to mysql over hostname mysql
.
Within a container, you can run getent hosts mysql
to see the hostname resolution of hostname mysql
.