Published 2023/01/07 - Updated 2023/01/07 - Written by Nathaniel Walser

How to deploy MongoDB with Docker Compose

MongoDB is a nosql document database. It provides features like atomic transactions, sharding, replication and storage tiering, just to name a few.

This setup is tailored for development purposes only. For production it needs to be extended quite a bit.

Install MongoDB

There are two common setup options available:

  • MongoDB with MongoDB Express. (Express is a Webbased admin ui)
  • MongoDB with MongoDB Compass. (Compass is a Desktop admin ui)

Personally I prefer the later one. MongoDB Compass has many features more and its easier to work with in development.

Create a docker-compose.yml file in an empty directory. Copy the following content into the newly created file.

version: '3.1'

services:
  mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - mongodb-data:/data/db
      - mongodb-config:/data/configdb

volumes:
  mongodb-data:
  mongodb-config:

Run docker-compose up in your terminal inside this folder to download and start the MongoDB container.

# CLI
docker-compose up

Get started with the MongoDB Driver

Installation instructions for your MongoDB Driver of choice can be found here. After successfull installation you can connect to MongoDB with mongodb://host.docker.internal:27017 as connection string.

Get started with MongoDB Compass

If you’ve choosen the MongoDB Compass option, you can download and install MongoDB Compass here.

After installation you will be able to connect to MongoDB with mongodb://host.docker.internal:27017/ as connection string. You can leave the other options as preconfigured.

Get started with MongoDB Express

If you’ve choosen the MongoDB Express option, MongoDB Express is available under port 8081. You can access it without any credentials under http://host.docker.internal:8081/.

More Templates