Install MongoDB and Mongo Express using Docker Compose

I have guided you on how to install the MongoDB server on Windows and macOS in previous tutorials. Using Docker Compose to install the MongoDB server will make everything much easier. In this tutorial, I will guide you on how to install the MongoDB server and Mongo Express, a web-based MongoDB client tool used to query data in the MongoDB server, using Docker Compose!

First, you can find the Docker Image of the MongoDB server here: https://hub.docker.com/_/mongo. As for the Docker Image of Mongo Express, you can also find it here: https://hub.docker.com/_/mongo-express/.

The Docker Compose file to install the MongoDB server and Mongo Express, you can define as follows:

With MongoDB server, I define the container name as mongodb, mount the /data folder inside the container outside the host machine with the folder “./mongodb_data” to avoid losing data when I create a new container, expose port 27017 of MongoDB server to the outside.

With Mongo Express, in addition to defining the container name, exposing port 8081 to the outside, I also declare that running the mongo-express container will depend on the mongodb container. You also need to define additional environment variables:

  • ME_CONFIG_MONGODB_SERVER points to the MongoDB server
  • ME_CONFIG_MONGODB_PORT is the port that the MongoDB server is running
  • ME_CONFIG_BASICAUTH_USERNAME and ME_CONFIG_BASICAUTH_PASSWORD are used to define the login credentials to the Mongo Express web interface.

Now, if you run the command “docker compose up” in the directory containing the Docker Compose file with the above content, you will see the following result:

Go to http://localhost:8081, you will see the following result:

Use the username and password you registered in the Docker Compose file above to log in, you will see the following result:

So, we have successfully installed the MongoDB server and Mongo Express using Docker Compose!

Add Comment