Docker Network & Docker-compose


Docker can create isolated networks if we deploy 2 or more Docker images that can communicate without any IP or port number, can communicate with their container name. They belong to the same network. Here I am trying to explain the docker networking.

To see the existing networks use: docker network ls

Will try to connect mongo and mongo-express in the same docker network

Some helpful command for the network creations

Will create a new network

docker network create mongo-nw

visit for more assistance about the configurations.

try to pull the mongo containers

docker pull mongo

docker pull mongo-express

docker run -p27017:27017 -d -e MONGO_INITDB_ROOT_USERNAME=mongoadmin -e MONGO_INITDB_ROOT_PASSWORD=secret --name mongodb --net mongo-nw mongo

Where '-e' is an environmental variable, '--name' used to give a name to the container '--net' is the network name that we gave in the previous step.

Now will set up MongoDB-Express

visit for mongo-express configurations

docker run -d -p8081:8081 -e ME_CONFIG_BASICAUTH_USERNAME=mongoadmin -e ME_CONFIG_BASICAUTH_PASSWORD=secret --net=mongo-nw --name=mongo-exp -e ME_CONFIG_MONGODB_SERVER=mongodb mongo-express

*Note:ME_CONFIG_MONGODB_SERVER=mongodb where mongodb is the container name that we rovided while running mongodb and give the same username and password while configuring the mongodb

to connect mongodb and mongo-express we are using same newtwork name "mongo-nw"

enter the command docker ps and check the containers up and running

Enter http://localhost:8081/  in your browser may ask for the username and password, provide the same while configuring mongodb.

The dashboard is ready for use.

Here we saw that to deploy the mongo and mongo express we need to write the scripts to make the instance live and created a custom network for the communication. We can avoid all this by writing a single docker-compose file.

In short, docker-compose helps to run multiple containers in a single host. 😍 gives more info about the docker-compose current version and its usage structure

docker-compose -v : to check the version of the docker-compose version

Before writing docker-compose check the docker version using the command: 

docker version

Now check the compatible docker-compose version with docker 19.03 from the below URL:

Sample docker file with name "docker-compose.yaml"

*Note: No need to specify the network name, docker-compose will create one automatically.

check this yaml in the correct format or not by entering the command

docker-compose -f docker-compose.yaml config

run the docker-compose by executing the below command

docker-compose -f docker-compose.yaml up -d

execute: docker network ls

Can see a newly created network name like the below image.

execute the URL in the browser localhost:8081

check the container by executing: docker ps

To stop the docker-compose use: docker-compose -f docker-compose.yaml down command.


Popular posts from this blog

Linux Basics

JAVA 8 Predefined Functions