You can run MongoDB Community Edition as a Docker container using the official MongoDB Community image. This page describes how to install, run, verify, and connect to MongoDB Community Edition in Docker.
Before You Begin
Before you install MongoDB Community Edition on Docker, complete the following prerequisites:
Install Docker on your host.
Install mongosh.
Confirm that your host platform is supported. To learn which platforms MongoDB runs on, see Platform Support.
MongoDB 5.0+ Docker images require AVX support on your system. If your system does not support AVX, you can use a docker image of MongoDB prior to version 5.0.
Warning
Versions of MongoDB prior to 7.0 are EOL'd and no longer supported by MongoDB. These versions should be used for testing purposes only.
Install
To pull the MongoDB 9.0 Community Server image, run the following command:
docker pull mongodb/mongodb-community-server:9.0-ubi9-slim
Start and Verify
Run the Image as a Container
Run the following command to start a container named mongodb from the image:
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:9.0-ubi9-slim
The -d flag runs the container in the background. The -p 27017:27017 flag publishes the container port to the host port.
You can append mongod command-line options to the docker run command. For example, to start a single-node replica set instead, run the following command:
docker run --name mongodb -p 27017:27017 -d mongodb/mongodb-community-server:9.0-ubi9-slim --replSet myReplicaSet
Verify That the Container Is Running
To list running containers, run docker ps. The output includes the container ID, image, status, and published ports:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c29db5687290 mongodb/mongodb-community-server:9.0-ubi9-slim "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp mongodb
Connect
Report the MongoDB Server Status
To report the status of your MongoDB instance, run the serverStatus command:
db.runCommand( { serverStatus: 1 } )
The command returns a document that describes your mongod deployment.