Docker MongoDB Container

MongoDb Docker

Make sure Docker Desktop is running.

Create a folder named mongodb-docker.

Add a file to the folder named docker-compose.yml, and put this into it:

services:
  mongodb:
    image: mongo:7
    container_name: mongodb
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:

Open a terminal and cd into the mongodb-docker folder, then run this command:

docker-compose up -d

It will take a few minutes to build the image and run the container.

To connect to the database, you can install Compass at: https://www.mongodb.com/try/download/compass

In Compass, click Add New Connection and paste this in for the URI:

mongodb://root:example@localhost:27017

Then click Save and Connect.

When are done working, you can stop the container with this command:

docker compose stop

When you want to get back to work on it, just start the container with docker compose up -d