Cover image for How to spin MongoDB server with Docker and Docker Compose

How to spin MongoDB server with Docker and Docker Compose

sonyarianto profile image Sony AK 惻3 min read

Today I will prepare and create a MongoDB server for testing purpose by using Docker and Docker Compose. It's simple.

Here is my scenario. I have Linux Ubuntu 19.10 (eoan) and I want to have MongoDB server instance. I don't want to install MongoDB in traditional way but instead just use Docker.

The boring things

I think everyone already knows MongoDB right? It's one of the best NoSQL database in the market.

According to Wikipedia.

MongoDB is a cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schema. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License.

You can get more information from https://www.mongodb.com.

Let's start.


I assume that you already have Docker and Docker Compose installed on your machine. If not, please refer to https://docs.docker.com/install/ for Docker installation and refer to https://docs.docker.com/compose/install/ for Docker Compose installation.

In this case I am on Linux Ubuntu, but the result I think should be similar for other OS.

Compose file

File docker-compose.yml

version: '3.7'
    image: mongo:latest
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
      - 27017:27017
      - mongodb_data_container:/data/db


What is the Docker Compose file above means?

It will have MongoDB image and it will take the latest version. As of this writing, the latest version of MongoDB is 4.2.2. MongoDB will have user root and password rootpassword. It will expose port 27017 to the host machine.

It will also using data container to save the data, called mongodb_data_container. It's useful for data persistent, so the data will not be deleted even later you call command docker-compose down.

Run it

docker-compose up -d

Above command will start the services on detach mode (similar like running in the background).

If everything OK then our MongoDB server will start.

How to check MongoDB container is running or not?

Type this command.

docker ps

The result it will look like this.

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
4056a520c8ea        mongo:latest        "docker-entrypoint.sā€¦"   19 hours ago        Up 19 hours>27017/tcp   mongodb_mongodb_container_1

Looks good and our MongoDB container is running well.

To check our data container, check with command below.

docker volume ls

It will show like below.

local               mongodb_mongodb_data_container

That's looks good.

How to connect to MongoDB server

Via mongo Shell

Type this.

mongo admin -u root -p rootpassword

It will connect to localhost port 27017.

Note that mongo command should be installed on the computer. On Linux this should be install mongodb-org-shell only. Refer to this for more detail https://docs.mongodb.com/manual/installation/

Bonus! Some quick tips after logged-in via mongo shell

Some useful commands.

Show databases:

show dbs

Create new non-existant database:

use mydatabase

Show collections:

show collections

Show contents/documents of a collection:


Save a data to a collection:

db.your_collection_name.save({"name":"Sony AK"})

Show database version:


Now we can enjoy our local MongoDB database server for any purpose we want. For me this setup is fine for testing purpose.

How to stop the MongoDB server

To shutdown database without delete all containers.

docker-compose stop

To shutdown database and delete all containers.

docker-compose down

That's it and we are done. Relax, if you already create some data it will not gone.

The code above also available on my GitHub repository at https://github.com/sonyarianto/docker-compose-mongodb

Thank you and I hope you enjoy it.



Posted on by:


Editor guide

Hi, I've been messing around with a setup very similar to this and was wondering if there was a way to get the created mongo volume to not be created by root. Its rather annoying to not be able to just "wipe" the local database data without using sudo (or similar).

Just wondering if there was an easy way around this, thanks!


Hi Brad, thanks for the comment. Unfortunately I still don't know for this answer. But I am quick look at the internet. Is this related with this?


I am newbie in Docker, and your tutorial clarifyed many things for me. Thank you very much! :-)


thanks and I am happy to hear that