@@ -30,7 +30,7 @@ Docker.
3030
3131.. _atlas-cli-deploy-docker-setup:
3232
33- Create a Local Atlas Deployment with Docker
33+ Create a Local |service| Deployment with Docker
3434-------------------------------------------
3535
3636.. procedure::
@@ -54,21 +54,89 @@ Create a Local Atlas Deployment with Docker
5454
5555 .. tabs::
5656
57- .. tab:: No Authentication
57+ .. tab:: Manually Connect no Auth
5858 :tabid: no-auth
5959
6060 .. code-block:: sh
6161
6262 docker run -p 27017:27017 mongodb/mongodb-atlas-local
6363
64- .. tab:: With Authentication
64+ .. tab:: Manually Connect with Auth
6565 :tabid: with-auth
6666
6767 .. code-block:: sh
6868
6969 docker run -e MONGODB_INITDB_ROOT_USERNAME=user -e MONGODB_INITDB_ROOT_PASSWORD=pass -p 27017:27017 mongodb/mongodb-atlas-local
7070
71- The logs display as the Docker image runs.
71+ .. tab:: Automate Connection
72+ :tabid: automate-connection
73+
74+ To automate a containerized deployment of |service|, you'll need to wait
75+ for the container to be in a healthy state before you can connect to |service|.
76+
77+ The following example demonstrates deploying an |service| image in Docker
78+ and polling Docker to check the state of the container. Once the
79+ container is in a healthy state, the script automates a connection
80+ to your |service| instance with `Mongosh <https://www.mongodb.com/docs/mongodb-shell/install/>`__.
81+
82+ a. Create a file called ``mongodb-atlas-local.sh``, and paste the
83+ following script into your new file.
84+
85+ .. code-block:: sh
86+
87+ # Start mongodb-atlas-local container
88+ echo "Starting the container"
89+ CONTAINER_ID=$(docker run --rm -d -P mongodb/mongodb-atlas-local:latest)
90+
91+ echo "waiting for container to become healthy..."
92+ function wait() {
93+ CONTAINER_ID=$1
94+ echo "waiting for container to become healthy..."
95+ for _ in $(seq 120); do
96+ STATE=$(docker inspect -f '{{ .State.Health.Status }}' "$CONTAINER_ID")
97+
98+ case $STATE in
99+ healthy)
100+ echo "container is healthy"
101+ return 0
102+ ;;
103+ unhealthy)
104+ echo "container is unhealthy"
105+ docker logs "$CONTAINER_ID"
106+ stop
107+ exit 1
108+ ;;
109+ *)
110+ sleep 1
111+ esac
112+ done
113+
114+ echo "container did not get healthy within 120 seconds, quitting"
115+ docker logs mongodb_atlas_local
116+ stop
117+ exit 2
118+ }
119+
120+ wait "$CONTAINER_ID"
121+ EXPOSED_PORT=$(docker inspect --format='{{ (index (index .NetworkSettings.Ports "27017/tcp") 0).HostPort }}' "$CONTAINER_ID")
122+
123+ # Build the connectionstring
124+ CONNECTION_STRING="mongodb://127.0.0.1:$EXPOSED_PORT/test?directConnection=true"
125+
126+ # Example usage of the connection string to connect to mongosh
127+ mongosh "$CONNECTION_STRING"
128+
129+ b. Run the following command to make the file executable.
130+
131+ .. code-block:: sh
132+
133+ chmod +x mongodb-atlas-local.sh
134+
135+ c. Run the executable.
136+
137+ .. code-block:: sh
138+
139+ ./mongodb-atlas-local.sh
72140
73141 .. step:: Connect to the local |service| deployment.
74142
@@ -368,4 +436,4 @@ directory:
368436 curl --output mongosh.deb https://downloads.mongodb.com/compass/mongodb-mongosh_2.2.1_amd64.deb
369437 sudo dpkg -i mongosh.deb
370438 mongosh --version
371- - run: mongosh 'mongodb://localhost/?directConnection=true' --eval 'show dbs'
439+ - run: mongosh 'mongodb://localhost/?directConnection=true' --eval 'show dbs'
0 commit comments