DEV Community

loading...
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.

Preparation

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'
services:
  mongodb_container:
    image: mongo:latest
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - 27017:27017
    volumes:
      - mongodb_data_container:/data/db

volumes:
  mongodb_data_container:
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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         0.0.0.0:27017->27017/tcp   mongodb_mongodb_container_1
Enter fullscreen mode Exit fullscreen mode

Looks good and our MongoDB container is running well.

To check our data container, check with command below.

docker volume ls
Enter fullscreen mode Exit fullscreen mode

It will show like below.

DRIVER              VOLUME NAME
local               mongodb_mongodb_data_container
Enter fullscreen mode Exit fullscreen mode

That's looks good.

How to connect to MongoDB server

Via mongo Shell

Type this.

mongo admin -u root -p rootpassword
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create new non-existant database:

use mydatabase
Enter fullscreen mode Exit fullscreen mode

Show collections:

show collections
Enter fullscreen mode Exit fullscreen mode

Show contents/documents of a collection:

db.your_collection_name.find()
Enter fullscreen mode Exit fullscreen mode

Save a data to a collection:

db.your_collection_name.save({"name":"Sony AK"})
Enter fullscreen mode Exit fullscreen mode

Show database version:

db.version()
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

To shutdown database and delete all containers.

docker-compose down
Enter fullscreen mode Exit fullscreen mode

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.

Reference

Credits

Discussion (4)

pic
Editor guide
Collapse
bradtaniguchi profile image
Brad

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!

Collapse
sonyarianto profile image
Sony AK Author

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?

Collapse
artemgrachov profile image
Artem Grachov

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

Collapse
sonyarianto profile image
Sony AK Author

thanks and I am happy to hear that