# How to run MongoDB via a Docker container

MongoDB Docker containers are easy to replicate and scale. If the load on the database increases, you can start additional MongoDB containers. This keeps the **database performance stable**.

## Does MongoDB run in a Docker container?

MongoDB can run effectively in a [Docker container](https://www.ionos.com/digitalguide/server/know-how/docker-container/). There are two types of MongoDB images on Docker Hub: the **Community Edition** and the **Enterprise Edition**. The choice between these two versions depends on your specific needs. The Community Edition is usually ideal for non-commercial use or smaller setups. The Enterprise Edition, however, includes extra features and support for larger applications or businesses with advanced needs like encryption, auditing, and LDAP integration.

If the pre-built MongoDB images on Docker Hub don’t fully meet your needs, [Docker](https://www.ionos.com/digitalguide/server/know-how/what-is-docker/) allows you to create a **custom Docker image** using a [Dockerfile](https://www.ionos.com/digitalguide/server/know-how/dockerfile/). With a Dockerfile, you can select the MongoDB version, configure specific options (like authentication methods), and install any additional tools or drivers needed for your environment.

## How to use MongoDB as a Docker container step by step

Docker is known for its **lightweight virtualization**, enabling portability and consistency across various development and production environments. Below, we’ll guide you step by step on how to set up MongoDB containers in Docker. For beginners, we suggest checking out our [Docker tutorial: Installation and first steps](https://www.ionos.com/digitalguide/server/configuration/docker-tutorial-installation-and-first-steps/), which provides a detailed introduction to the container platform.

### Step 1: Download MongoDB Docker image

To create a MongoDB Docker container, we first obtain the appropriate image for running **Docker Hub**. Open your terminal or command line and run the following command:

```bash
docker pull mongo:latest
```

If the image already exists locally, Docker won’t download a new version unless you specifically indicate the version you want to use (for example `mongo:4.4`).

### Step 2: Launch MongoDB Docker container

Once the Docker image for MongoDB has been successfully downloaded, you can start a container based on this image:

- `docker run`: Launches a new Docker container
- `--name mongodb-container`: Names the container “mongodb-container”
- `-d`: Use this parameter to launch the container in the background (detached mode). This means that the terminal can still be used while the container is running
- `-p 27017:27017`: Opens the MongoDB standard port 27017 of the container on your host system
- `mongo:latest`: Provides the instruction to obtain the latest available image

### Step 3: Check that the container is running

To ensure that the container has been launched successfully, enter the following command:

```bash
docker ps
```

This information provides a quick overview of the active MongoDB Docker containers on your system, including how long they have been running and which ports they are using. For more details, such as stopped containers or specific filtering options, it’s recommended to use the `docker ps -a` command.

### Step 4: Establish a connection to MongoDB in the Docker container

Now you can establish a connection to the MongoDB instance in your Docker container. To do this, launch the **MongoDB Shell** directly in the container:

- `docker exec`: Executes a command in the running container
- `-it`: Allows interaction with the terminal in the container
- `mongodb-container`: The name of the set-up MongoDB container
- `mongo`: Launches the MongoDB shell

Once you’ve executed this command, you should see the MongoDB shell ready to receive commands. You can now enter the usual MongoDB commands to manage or query your database.

### Step 5: Perform operations in the MongoDB shell

You can now display all existing databases on the MongoDB server as follows:

```bash
show databases
```

The output contains the names of the databases:

```bash
admin   0.000GB
local      0.000GB
test       0.000GB
```

To work with a specific database in the MongoDB shell, use the following command:

```bash
use mydatabase
```

Use the following command to switch to the database named “mydatabase”. If the database doesn’t exist, it will be created automatically when a document is inserted.

You can also retrieve documents from a specific collection:

```bash
db.users.find()
```

The MongoDB shell returns all documents that are stored in the “users” collection.

```bson
{ "_id": ObjectId("609823e9f9a5f7f364fc3f90"), "username": "alice", "age": 28 }
{ "_id": ObjectId("609823f2f9a5f7f364fc3f91"), "username": "bob", "age": 32 }
{ "_id": ObjectId("609823f9f9a5f7f364fc3f92"), "username": "charlie", "age": 25 }
```

Each document contains a unique `_id` field (MongoDB-specific identifier) and other fields such as `username` and `age`.

Find more information on [Docker tools](https://www.ionos.com/digitalguide/server/know-how/docker-tools-the-container-platform-eco-system/) and a [MongoDB presentation including a comparison with MySQL](https://www.ionos.com/digitalguide/websites/web-development/mongodb-an-introduction-and-comparison-to-mysql/) in our guide.


This is a markdown version of: [https://www.ionos.com/digitalguide/server/configuration/mongodb-docker-container/](https://www.ionos.com/digitalguide/server/configuration/mongodb-docker-container/) for AI/LLM consumption.