Docs Menu

Docs HomeLaunch & Manage MongoDBAtlas CLI

Create a Local Atlas Deployment with Docker

On this page

  • Create a Local Atlas Deployment with Docker
  • Create a Local Atlas Deployment with Docker Compose
  • Persist Data Across Runs with Docker Compose
  • Supported Actions

This tutorial shows you how to use the atlas deployments command to create a local Atlas deployment with Docker. In this tutorial, we will deploy a single-node replica set with Docker.

Important

Public Preview

The atlas deployments command and subsequent related commands are available as a Public Preview. The feature and corresponding documentation may change at any time in the Public Preview stage. To ask questions and provide feedback, see the Atlas CLI Local Development Community Forum.

Use the atlas deployments command to create a local Atlas deployment with Docker.

1

To learn more, see the Docker documentation.

2

Example:

docker pull mongodb/atlas:latest
3

To learn more, see Run Atlas CLI Commands with Docker.

Example:

docker run -p 27777:27017 --privileged -it mongodb/atlas bash
4

The following command creates a local Atlas deployment with Atlas Search capabilities enabled. It also returns a connection string.

Example:

atlas deployments setup --bindIpAll --username root --password root --type local --force

To learn more about the available options, see atlas deployments setup.

5

To connect to the local Atlas deployment from the host (not container), copy and paste the following command, and replace the {connection_string} variable with your connection string.

Note

The following example uses mongosh, but you can use the connection method that you prefer.

mongosh {connection_string}

Example:

mongosh mongodb://root:root@localhost:27017/?directConnection=true

Use the atlas deployments command to create a local Atlas deployment with 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.

3
  1. Create the docker-compose.yaml file in the same directory that you run Docker Compose from.

    Example:

    1services:
    2 mongo:
    3 image: mongodb/atlas
    4 privileged: true
    5 command: |
    6 /bin/bash -c "atlas deployments setup --type local --port 27778 --bindIpAll --username root --password {your-password} --force && tail -f /dev/null"
    7 volumes:
    8 - /var/run/docker.sock:/var/run/docker.sock
    9 ports:
    10 - 27778:27778
  2. Replace the {your-password} variable in the example with your password and save the file.

To learn more about the available options, see atlas deployments setup.

4

The following command creates a local Atlas deployment with Atlas Search capabilities enabled. It also returns a connection string.

Example:

docker-compose up
5

To connect to the local Atlas deployment from the host (not container), copy and paste the following command, and replace the {connection_string} variable with your connection string.

Note

The following example uses mongosh, but you can use the connection method that you prefer.

mongosh {connection_string}

Example:

mongosh mongodb://root:root@localhost:27778/?directConnection=true

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.

3
  1. Create the following entrypoint.sh script file in the same directory that you run Docker Compose from:

    #!/bin/bash
    stop_atlas() {
    echo "Stopping Atlas deployment..."
    atlas deployments stop
    }
    start_atlas() {
    echo "Starting Atlas deployment..."
    atlas deployments start
    }
    trap 'stop_atlas' SIGTERM SIGINT
    deployment_status=$(atlas deployments list | grep 'LOCAL')
    if [[ -z "$deployment_status" ]]; then
    echo "No local deployment found. Setting up..."
    atlas deployments setup dev --bindIpAll --username root --password $LOCALDEV_PASSWORD --type local --port 27017 --force
    else
    if [[ $deployment_status == *"STOPPED"* ]]; then
    start_atlas
    fi
    fi
    while true
    do
    tail -f /dev/null & wait ${!}
    done
  2. Run the following command to make the script executable only by you:

    chmod u+x entrypoint.sh
4
  1. Update the docker-compose.yaml file to mount the necessary data directories as volumes.

    Example:

    1services:
    2 mongo:
    3 image: mongodb/atlas
    4 privileged: true
    5 volumes:
    6 - data-cni:/etc/cni
    7 - data-containers:/var/lib/containers
    8 - ./entrypoint.sh:/entrypoint.sh
    9 entrypoint: ["/entrypoint.sh"]
    10 ports:
    11 - 27017:27017
    12 environment:
    13 - LOCALDEV_PASSWORD={your-password}
    14volumes:
    15 data-cni:
    16 data-containers:
  2. Replace the {your-password} variable in the example with your password and save the file.

To learn more about the available options, see atlas deployments setup.

5

The following command creates a local Atlas deployment with Atlas Search capabilities enabled. It also returns a connection string.

Example:

docker-compose up

You can also run Docker Compose in detached mode.

Example:

docker-compose up -d
6

To connect to the local Atlas deployment from the host (not container), copy and paste the following command, and replace the {connection_string} variable with your connection string.

Note

The following example uses mongosh, but you can use the connection method that you prefer.

mongosh {connection_string}

Example:

mongosh mongodb://root:{your-password}@localhost:27017/?directConnection=true

To learn all of the actions that atlas deployments supports, see atlas deployments.

← Create a Local Atlas Deployment