For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Create a Local Atlas Deployment with Docker

This tutorial shows you how to create a local Atlas deployment with Docker. This tutorial describes how to deploy a single-node replica set with Docker. For a complete and detailed Docker Compose example, see Docker Compose Example.

Required:

  • Docker is installed and running on your local computer.

  • Your computer has at least 2 GB of free RAM and 2 CPU cores.

  • The port you plan to use (default: 27017) is available on localhost.

Recommended:

No virtual private cloud (VPC), firewall, or proxy configuration is required. The deployment runs on local networking.

1

To learn more, see the Docker documentation.

2

Example:

docker pull mongodb/mongodb-atlas-local:latest
.. step:: Run the Docker image.
**Example:**
.. tabs::
.. tab:: Manually Connect no Auth
:tabid: no-auth
.. code-block:: sh
docker run -p 27017:27017 mongodb/mongodb-atlas-local
.. tab:: Manually Connect with Auth
:tabid: with-auth
.. code-block:: sh
docker run -e MONGODB_INITDB_ROOT_USERNAME=user -e MONGODB_INITDB_ROOT_PASSWORD=pass -p 27017:27017 mongodb/mongodb-atlas-local
.. tab:: Automate Connection
:tabid: automate-connection
To automate a containerized deployment of |service|, you'll need to wait
for the container to be in a healthy state before you can connect to |service|.
The following example demonstrates deploying an |service| image in Docker
and polling Docker to check the state of the container. Once the
container is in a healthy state, the script automates a connection
to your |service| instance with `Mongosh <https://www.mongodb.com/docs/mongodb-shell/install/>`__.
a. Create a file called ``mongodb-atlas-local.sh``, and paste the
following script into your new file.
.. code-block:: sh
# Start mongodb-atlas-local container
echo "Starting the container"
CONTAINER_ID=$(docker run --rm -d -P mongodb/mongodb-atlas-local:latest)
echo "waiting for container to become healthy..."
function wait() {
CONTAINER_ID=$1
echo "waiting for container to become healthy..."
for _ in $(seq 120); do
STATE=$(docker inspect -f '{{ .State.Health.Status }}' "$CONTAINER_ID")
case $STATE in
healthy)
echo "container is healthy"
return 0
;;
unhealthy)
echo "container is unhealthy"
docker logs "$CONTAINER_ID"
stop
exit 1
;;
*)
sleep 1
esac
done
echo "container did not get healthy within 120 seconds, quitting"
docker logs mongodb_atlas_local
stop
exit 2
}
wait "$CONTAINER_ID"
EXPOSED_PORT=$(docker inspect --format='{{ (index (index .NetworkSettings.Ports "27017/tcp") 0).HostPort }}' "$CONTAINER_ID")
# Build the connectionstring
CONNECTION_STRING="mongodb://127.0.0.1:$EXPOSED_PORT/test?directConnection=true"
# Example usage of the connection string to connect to mongosh
mongosh "$CONNECTION_STRING"
b. Run the following command to make the file executable.
.. code-block:: sh
chmod +x mongodb-atlas-local.sh
c. Run the executable.
.. code-block:: sh
./mongodb-atlas-local.sh
.. step:: Connect to the local |service| deployment.
To connect to the local |service| deployment from the host (not
container), copy and paste the following command into a new
terminal, and replace the ``{connection_string}`` variable with
your connection string.
.. note::
The following example uses :binary:`~bin.mongosh`, but you can use the
connection method that you prefer.
.. code-block:: sh
mongosh {connection_string}
**Examples:**
.. tabs::
.. tab:: No Authentication
:tabid: no-auth
.. code-block:: sh
mongosh "mongodb://localhost:27017/?directConnection=true"
.. tab:: With Authentication
:tabid: with-auth
.. code-block:: sh
mongosh "mongodb://user:pass@localhost:27017/?directConnection=true"

Create a local Atlas deployment with Docker Compose. For a complete and detailed example, see Docker Compose Example.

Note

If you have an existing Docker-Compose based Atlas implementation that you've built with the official MongoDB Docker Image and that you'd like to convert to use the Atlas Local Dev image, see Convert to a Local Dev Implementation below.

1

To learn more, see the Docker documentation.

2

Example:

brew install docker-compose
To learn more, see the `Docker Compose install documentation
<https://docs.docker.com/compose/install/>`__.
.. step:: Create a ``docker-compose.yaml`` file.
Create the ``docker-compose.yaml`` file in the same directory
that you run Docker Compose from. For a complete and detailed
example, see :ref:`atlas-cli-docker-compose`.
**Example:**
.. code-block:: sh
:linenos:
services:
mongodb:
image: mongodb/mongodb-atlas-local
environment:
- MONGODB_INITDB_ROOT_USERNAME=user
- MONGODB_INITDB_ROOT_PASSWORD=pass
ports:
- 27018:27017
.. step:: Run Docker Compose.
The following command creates a local |service| deployment with
|fts| capabilities enabled.
**Example:**
.. code-block:: sh
docker-compose up
.. step:: Connect to the local |service| deployment.
To connect to the local |service| deployment from the host (not
container), copy and paste the following command into a new
terminal, and replace the ``{connection_string}`` variable with
your connection string.
.. note::
The following example uses :binary:`~bin.mongosh`, but you can use the
connection method that you prefer.
.. code-block:: sh
mongosh {connection_string}
**Example:**
.. code-block:: sh
mongosh "mongodb://user:pass@localhost:27018/?directConnection=true"
.. step:: (Optional) Stop the container.
.. code-block:: sh
docker compose down -v

You can persist data across multiple runs with Docker Compose. Persisting data helps to ensure that data isn't lost between runs. Data remains available across runs of Docker Compose.

1

To learn more, see the Docker documentation.

2

Example:

brew install docker-compose
To learn more, see the `Docker Compose install documentation
<https://docs.docker.com/compose/install/>`__.
.. step:: Create a ``docker-compose.yaml`` file.
Update the ``docker-compose.yaml`` file to mount the necessary
data directories as volumes.
**Example:**
.. code-block:: sh
:linenos:
services:
mongodb:
hostname: mongodb
image: mongodb/mongodb-atlas-local
environment:
- MONGODB_INITDB_ROOT_USERNAME=user
- MONGODB_INITDB_ROOT_PASSWORD=pass
ports:
- 27019:27017
volumes:
- data:/data/db
- config:/data/configdb
volumes:
data:
config:
.. step:: Run Docker Compose.
The following command creates a local |service| deployment with
|fts| capabilities enabled.
**Example:**
.. code-block:: sh
docker-compose up
You can also run Docker Compose in `detached mode <https://docs.docker.com/reference/cli/docker/compose/up/#options>`__.
**Example:**
.. code-block:: sh
docker-compose up -d
.. step:: Connect to the local |service| deployment.
To connect to the local |service| deployment from the host (not
container), copy and paste the following command into a new
terminal, and replace the ``{connection_string}`` variable with
your connection string.
.. note::
The following example uses :binary:`~bin.mongosh`, but you can use the
connection method that you prefer.
.. code-block:: sh
mongosh {connection_string}
**Example:**
.. code-block:: sh
mongosh "mongodb://user:pass@localhost:27019/?directConnection=true"

You can generate a list of the dependencies for the mongodb/mongodb-atlas-local Docker image.

1

Example:

brew install syft
To learn more, see the :github:`syft README
</anchore/syft/blob/main/README.md>`.
.. step:: Run syft.
.. code-block:: sh
syft mongodb/mongodb-atlas-local

You can verify the signature of the mongodb/mongodb-atlas-local Docker image.

1

Example:

brew install cosign
To learn more, see the `cosign Installation
<https://docs.sigstore.dev/cosign/system_config/installation/>`__.
.. step:: Run the following commands.
**Example:**
.. code-block:: sh
curl -O https://cosign.mongodb.com/mongodb-atlas-local.pem
.. code-block:: sh
COSIGN_REPOSITORY="docker.io/mongodb/signatures" cosign verify --private-infrastructure --key=./mongodb-atlas-local.pem "mongodb/mongodb-atlas-local";

To run the mongodb/mongodb-atlas-local Docker image with GitHub actions, create a workflow file. To learn more, see the GitHub Actions Quickstart.

Example:

Create the following mongodb.yml file in the .github/workflows directory:

on:
push:
branches:
- main
pull_request:
jobs:
run:
runs-on: ubuntu-latest
services:
mongodb:
image: mongodb/mongodb-atlas-local
ports:
- 27017:27017
steps:
- name: install mongosh
run: |
curl --output mongosh.deb https://downloads.mongodb.com/compass/mongodb-mongosh_2.2.1_amd64.deb
sudo dpkg -i mongosh.deb
mongosh --version
- run: mongosh 'mongodb://localhost/?directConnection=true' --eval 'show dbs'

If you have an existing Atlas implementation running in Docker Compose that you have built with the official mongo Docker image, you can refer to the following checklist to simplify converting it to the mongodb-atlas-local image.

  1. Remove any existing command from your docker-compose.yaml file. Because the command in a Docker Compose definition overrides the ENTRYPOINT defined in the mongodb-atlas-local image, you must remove any existing command for the mongodb-atlas-local image to run as designed.

  2. There is no need to define a health check for the Atlas deployment, as this feature is built in to the mongodb-atlas-local image.

The following examples illustrate the likely required changes to your Docker Compose YAML file:

services:
self_built_atlas_implementation:
image: mongo:8.0
ports:
- 27017:27017
command: ["./entrypoint.sh"] # You may have defined a health check for the database in the entrypoint script.
services:
local_dev_atlas:
image: mongodb/mongodb-atlas-local:8.0
hostname: local_dev_atlas
ports:
- 27017:27017
environment:
- MONGODB_INITDB_ROOT_USERNAME=user
- MONGODB_INITDB_ROOT_PASSWORD=pass
volumes:
- data:/data/db
- config:/data/configdb
volumes:
- data:
- config:

After starting your container, confirm the deployment is running and that you can connect.

Checkpoint
Verification Step

Image pulled

Run docker images | grep mongodb-atlas-local. The image appears in the output.

Container running

Run docker ps. The container appears with status Up.

Container healthy

Run docker inspect <container-id> | grep Health. The status shows healthy.

Connection succeeds

Connect with mongosh using the connection string (for example, mongodb://localhost:27017/?directConnection=true).

Data volumes present (optional)

Run docker volume ls. The data volumes appear in the output.

After you create and verify your local Atlas deployment with Docker, you can: