How to start a MongoDB container along with a web-based admin interface using Docker Compose

How to start a MongoDB container along with a web-based admin interface using Docker Compose

The goal of this guide is to show how to get mongo and mongo-express into docker containers with a YAML file using docker compose tool.

Definitions

Docker

Docker allows you to package an application with its environment and all of its dependencies into a "box", called a container. Usually, a container consists of an application running in a stripped-to-basics version of a Linux operating system. An image is the blueprint for a container, a container is a running instance of an image.

Docker Compose

Docker Compose is a tool for defining and running multi-container applications. With Docker Compose, you use a YAML file to configure all your containers and with a single command, you create and start all the containers from your configuration.

MongoDB

MongoDB is a document-oriented NoSQL database. Instead of using tables and rows as in traditional relational databases, MongoDB makes use of documents and collections. A document consists of key-value pairs which are the basic unit of data in MongoDB. A collection is a set of documents which is the equivalent of a table in relational database.

Mongo-Express

Mongo-Express is a web-based admin interface for MongoDB. You can view and create documents and collections inside your MongoDB container with this simple to use interface.

Pre-requisite

  • Docker installed in your system.

Create a YAML File

A minimum YAML configuration should look like this :-

version: "3.8"
services:
  mongodb:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-data:/data/db
  mongo-express:
    image: mongo-express
    ports:
      - "8080:8081"
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongodb
      - ME_CONFIG_MONGODB_ADMINUSERNAME=admin
      - ME_CONFIG_MONGODB_ADMINPASSWORD=password
volumes:
  mongo-data:

Explanation

Line by line explanation of the YAML file :-

  1. version: "3.8" - latest version of docker compose as of writing this article.
  2. services - list of containers we want to start simultaneously.
  3. mongodb - arbitrary name of the container we want to give to our mongo instance.
  4. image: mongo - official name of the MongoDB image which we will pull from docker hub.
  5. ports: - list of ports we want to map.
  6. - "27017:27017" - this will map host port 27017 with the container port 27017 and expose our host port to the outside world.
  7. environment: - list of environment variables for our mongodb container.
  8. - MONGO_INITDB_ROOT_USERNAME=admin - required environment variable for our connection with mongo-express.
  9. - MONGO_INITDB_ROOT_PASSWORD=password - required environment variable for our connection with mongo-express.
  10. volumes: - list of volumes we would need to persist and regain our data incase our container restarts or stops.
  11. - mongo-data:/data/db - mongo-data is the name of the volume we will create later in this yaml file which will map to the location where data is stored inside the mongo container i.e. /data/db.
  12. mongo-express: - our second container we want to start along with mongodb
  13. image: mongo-express - official name of the image which we will pull from docker hub.
  14. ports: - list of ports we want to map for this second container.
  15. - "8080:8081" - host port 8080 is mapped to container port 8081. So we will open our mongo user interface at port 8080 in our browser.
  16. environment: - environment variables for our mongo-express container.
  17. - ME_CONFIG_MONGODB_SERVER=mongodb - pass the name of the container we gave to our mongo instance to this environment variable of mongo-express.
  18. - ME_CONFIG_MONGODB_ADMINUSERNAME=admin - pass the username we gave to our mongo container to this environment variable.
  19. - ME_CONFIG_MONGODB_ADMINPASSWORD=password - provide the password we gave to our mongo container to this environment variable.
  20. volumes: - list of volumes we want to create.
  21. mongo-data: - the name of the volume we want to persist data.

Run it

Steps on how to run our containers using this yaml file.

  1. Save the yaml file as mongo.yaml and open a terminal in the same folder.
  2. Run the following command in the terminal - docker-compose -f mongo.yaml up -d
  3. Check if the containers are running using this command - docker ps
  4. To check on the volume, run this command - docker volume ls
  5. Visit http://localhost:8080 in your browser to open your database admin interface.

Congratulations!

You have now successfully created a mongo and mongo-express containers simultaneously using docker compose tool.