This tutorial shows you how to create a local Atlas deployment with Docker. In this tutorial, we will deploy a single-node replica set with Docker.
Example:
docker pull mongodb/mongodb-atlas-local:latest
Example:
docker run -p 27017:27017 mongodb/mongodb-atlas-local
docker run -e MONGODB_INITDB_ROOT_USERNAME=user -e MONGODB_INITDB_ROOT_PASSWORD=pass -p 27017:27017 mongodb/mongodb-atlas-local
To automate a containerized deployment of Atlas, you'll need to wait for the container to be in a healthy state before you can connect to Atlas.
The following example demonstrates deploying an Atlas 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 Atlas instance with Mongosh.
Create a file called mongodb-atlas-local.sh
, and paste the following script into your new file.
# Start mongodb-atlas-local containerecho "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=$1echo "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 esacdoneecho "container did not get healthy within 120 seconds, quitting"docker logs mongodb_atlas_localstopexit 2}wait "$CONTAINER_ID"EXPOSED_PORT=$(docker inspect --format='{{ (index (index .NetworkSettings.Ports "27017/tcp") 0).HostPort }}' "$CONTAINER_ID")# Build the connectionstringCONNECTION_STRING="mongodb://127.0.0.1:$EXPOSED_PORT/test?directConnection=true"# Example usage of the connection string to connect to mongoshmongosh "$CONNECTION_STRING"
Run the following command to make the file executable.
chmod +x mongodb-atlas-local.sh
Run the executable.
To connect to the local Atlas 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.
The following example uses mongosh
, but you can use the connection method that you prefer.
mongosh {connection_string}
Examples:
mongosh "mongodb://localhost:27017/?directConnection=true"
mongosh "mongodb://user:pass@localhost:27017/?directConnection=true"
Create a local Atlas deployment with Docker Compose.
Create the docker-compose.yaml
file in the same directory that you run Docker Compose from.
Example:
1services:2 mongodb:3 image: mongodb/mongodb-atlas-local4 environment:5 - MONGODB_INITDB_ROOT_USERNAME=user6 - MONGODB_INITDB_ROOT_PASSWORD=pass7 ports:8 - 27018:27017
The following command creates a local Atlas deployment with Atlas Search capabilities enabled.
Example:
To connect to the local Atlas 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.
The following example uses mongosh
, but you can use the connection method that you prefer.
mongosh {connection_string}
Example:
mongosh "mongodb://user:pass@localhost:27018/?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.
Update the docker-compose.yaml
file to mount the necessary data directories as volumes.
Example:
1services:2 mongodb:3 hostname: mongodb4 image: mongodb/mongodb-atlas-local5 environment:6 - MONGODB_INITDB_ROOT_USERNAME=user7 - MONGODB_INITDB_ROOT_PASSWORD=pass8 ports:9 - 27019:2701710 volumes:11 - data:/data/db12 - config:/data/configdb13volumes:14 data:15 config:
The following command creates a local Atlas deployment with Atlas Search capabilities enabled.
Example:
You can also run Docker Compose in detached mode.
Example:
To connect to the local Atlas 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.
The following example uses mongosh
, but you can use the connection method that you prefer.
mongosh {connection_string}
Example:
mongosh "mongodb://user:pass@localhost:27019/?directConnection=true"
You can generate a list of the dependencies for the mongodb/mongodb-atlas-local
Docker image.
syft mongodb/mongodb-atlas-local
You can verify the signature of the mongodb/mongodb-atlas-local
Docker image.
Example:
curl -O https://cosign.mongodb.com/mongodb-atlas-local.pem
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.
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.
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"]
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/configdbvolumes: - data: - config:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4