Simple example of Docker networking. Creating the environment manually and with Docker Compose.
This environment is composed of a Node.js application that depends on a Mongo database, each one in a different container connected through a network.
Command to create the network:
docker network create --attachable appnet
To build the Node application image run the following command:
docker build -t mongoapp .
To run the containers from the images:
docker run -d --name db mongo
docker run -d --name app -p 3000:3000 --env MONGO_URL=mongodb://db:27017/test mongoapp
And finally, to connect the containers to the network previously created, execute each of these commands:
docker network connect appnet db
docker network connect appnet app
To build the services run the following command:
docker-compose build
Command to create and start the environment:
docker-compose up -d
Command to stop and delete the environment:
docker-compose down
For more information about Docker Compose consult its documentation