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 :-
version: "3.8"
- latest version of docker compose as of writing this article.services
- list of containers we want to start simultaneously.mongodb
- arbitrary name of the container we want to give to our mongo instance.image: mongo
- official name of the MongoDB image which we will pull from docker hub.ports:
- list of ports we want to map.- "27017:27017"
- this will map host port27017
with the container port27017
and expose our host port to the outside world.environment:
- list of environment variables for our mongodb container.- MONGO_INITDB_ROOT_USERNAME=admin
- required environment variable for our connection with mongo-express.- MONGO_INITDB_ROOT_PASSWORD=password
- required environment variable for our connection with mongo-express.volumes:
- list of volumes we would need to persist and regain our data incase our container restarts or stops.- 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
.mongo-express:
- our second container we want to start along with mongodbimage: mongo-express
- official name of the image which we will pull from docker hub.ports:
- list of ports we want to map for this second container.- "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.environment:
- environment variables for our mongo-express container.- ME_CONFIG_MONGODB_SERVER=mongodb
- pass the name of the container we gave to our mongo instance to this environment variable of mongo-express.- ME_CONFIG_MONGODB_ADMINUSERNAME=admin
- pass the username we gave to our mongo container to this environment variable.- ME_CONFIG_MONGODB_ADMINPASSWORD=password
- provide the password we gave to our mongo container to this environment variable.volumes:
- list of volumes we want to create.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.
- Save the yaml file as mongo.yaml and open a terminal in the same folder.
- Run the following command in the terminal -
docker-compose -f mongo.yaml up -d
- Check if the containers are running using this command -
docker ps
- To check on the volume, run this command -
docker volume ls
- 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.