Mongo DB in Docker

22 September 2020

Updated: 03 September 2023

Considerations

To run MongDB in a Docker Container there are a few things to take note of:

  • You may need to configure auth
  • Volume storage to be set up

From Terminal

To run a Docker container using a single command in the terminal you can run the following command:

Terminal window
1
docker run -d -p 27017:27017

You can also specify additional information like the volumes you would like to use using flags when running:

Terminal window
1
docker run -d -p 27017:27017

From Compose

More info can be found here

You can also run a MongoDB Container with Compose which may be a bit easier:

docker-compose.yml

1
version: '3.3'
2
3
services:
4
# mongonode0:27017
5
mongo0:
6
image: mongo
7
hostname: mongo0
8
container_name: mongo0
9
restart: always
10
ports:
11
- '37000:27017'
12
volumes:
13
- ./mongo0-data:/data/db
14
environment:
15
MONGO_INITDB_ROOT_USERNAME: root
16
MONGO_INITDB_ROOT_PASSWORD: password