Part 1 – Before Docker Compose
Step 1: Creating a Docker Network
In order to deploy the application, we first need to create a Docker Network which will allow containers to communicate with each other and with the external world. Enter the following command to create a Docker Network called “mongo-tutorial-network”:
docker network create mongo-tutorial-network
This will give you a similar result and the Network will be created.
We can now check the list of Docker Networks by entering the following command:
docker network ls
And we will find our mongo-tutorial-network in the list (list may vary for you since I have additional Docker Networks created):
Step 2: Running first container in Docker Network
Now that the Network is setup, we need to add the mongo image to the Network. mongo image requires to setup two environment variables:
“MONGO_INITDB_ROOT_USERNAME” which I am setting to “admin”
“MONGO_INITDB_ROOT_PASSWORD” which I am setting to “gfg-secret”.
With the following command we are going to define the port to 27017 which is MongoDB’s default Port. We are going to bind it to the same port on the host. We are specifying the Network it will run in and give it a name:
docker run -d \
-p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=gfg-secret \
--network mongo-tutorial-network \
--name mongodb \
mongo
It will pull the image and run it and you will get a similar output:
And now if we check the list of running containers:
docker ps
we will see our mongodb container:
Step 3: Running second container in Docker Network
Similar to the previous container, this command will pull and run the mongo-express container in our mongo-tutorial-network Network:
docker run -d \
-p 8081:8081 \
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admin \
-e ME_CONFIG_MONGODB_ADMINPASSWORD=gfg-secret \
-e ME_CONFIG_MONGODB_SERVER=mongodb \
--network mongo-tutorial-network \
--name mongo-express \
mongo-express
And you will get a similar result:
And now if we check the list of running containers:
docker ps
we will see our both our containers running:
Step 4: Running the application on browser
Now that both of the containers are up and running, let us test the application. Go to “http://0.0.0.0:8081” and you will get a sign in window. since our basicAuth credentials are “admin:pass”. You can check that using:
docker logs [MONGO_EXPRESS_CONTAINER_ID]
You will see this sign in window:
Enter the Username as “admin” and password as “pass”. and we will get our MongoDB application running in the browser:
Docker Compose – How To Execute Multiple Commands?
Docker Compose is an orchestration tool that comes with Docker and enables users to run and manage multi-container applications using a YAML format file. Docker Compose comes pre-installed with Docker and does not require any additional installation or activation. In this article, we will explore what Docker Compose is. and how we can execute multiple commands using Docker Compose.