Docker – Compose

By | 08/12/2021

In this post, we will see how to use docker compose in order to run a container with MongoDB and another one with mongo-express (a web-application used to manage a MongoDB instance).
But first of all, what is docker compose?
From Docker web site:
“Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.”

We will start to run the two containers without docker compose and then, we will see how to do the same thing with docker compose.

First of all, we pull the mongodb images and the mongo-express image using the command:
docker pull ‘name of image’

Now, we will create a new docker network that we will use in order to establish a communication between mongodb and mongo-express:
docker network create ‘network name’

Then, we will run the MongoDb container using these parameters:
–name dockermongo => container name
-e MONGO_INITDB_ROOT_USERNAME=admindb => admin username
-e MONGO_INITDB_ROOT_PASSWORD=pass123 => admin password
-v dbmongo:/data/db => volume definition
–net netMongoDBServer => network definition

docker run -d --name dockermongo -e MONGO_INITDB_ROOT_USERNAME=admindb -e MONGO_INITDB_ROOT_PASSWORD=pass123 -v dbmongo:/data/db --net  netMongoDBServer  mongo


Finally, we will run the mongo-express container using these parameters:
-p 8081:8081 => definition of host port and container port
–name dockermongoexpress => container name
-e ME_CONFIG_MONGODB_ADMINUSERNAME=admindb => admin username of MongoDB
-e ME_CONFIG_MONGODB_ADMINPASSWORD=pass123 => admin password of MongoDB
-e ME_CONFIG_MONGODB_SERVER=dockermongo => MongoDB container name
–net netMongoDBServer => network definition

docker run -d -p 8081:8081 --name dockermongoexpress -e ME_CONFIG_MONGODB_ADMINUSERNAME=admindb -e ME_CONFIG_MONGODB_ADMINPASSWORD=pass123 -e ME_CONFIG_MONGODB_SERVER=dockermongo --net netMongoDBServer mongo-express


With the command docker ps -a, we can check that everything works fine:


Now, if we open a browser and go to http://localhost:8081, this will be the result:

We can see that everything works fine but we had to run many docker commands and, in situations like that, docker compose can help us.

First of all, we will stop and delete the two containers:
docker stop ‘id_container’
docker rm ‘id_container’

and then, we will delete the volume as well:
docker volume rm ‘container name’

Now, we open Visual Studio Code, we create a file called docker-compose.yml and we write this code:

[DOCKER-COMPOSE.YML]

version: '3'
services:
  # MongoDB container definition
  dockermongo:
    # name docker image 
    image: mongo
    # username and password Admin definition
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admindb 
      - MONGO_INITDB_ROOT_PASSWORD=pass123 
    # volume definition
    volumes:
      - dbmongo:/data/db
    # network definition
    networks:
      - netMongoDBServer
  # mongo-express container definition 
  dockermongoexpress:
    # name docker image
    image: mongo-express
    # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
    restart: always 
    # definition host and container port
    ports:
      - 8081:8081
    # definition of username and password MongoDB's Admin
    # name MongoDB container 
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admindb
      - ME_CONFIG_MONGODB_ADMINPASSWORD=pass123
      - ME_CONFIG_MONGODB_SERVER=dockermongo
    # network definition
    networks:
      - netMongoDBServer 
volumes:
  dbmongo:
    driver: local
networks:
    netMongoDBServer:
        driver: bridge


We have done and now, with the command docker-compose up -d (we don’t have to specify the file name, because we have used the name ‘docker-compose’), we will run the docker compose file:

If we open a browser and we go to http://localhost:8081, this will be the result:

Finally, using the command docker-compose down, we will stop and delete the containers defined in the docker compose file:



Leave a Reply

Your email address will not be published. Required fields are marked *