Hi @Jakub_Lazinski, Iâve had similar issues, and can share my details. I appreciate your engagement and trying to get this all to work in a local experience.
- docker-compose. We want to use an Atlas Docker container both for local dev and a CI pipeline.
A. CI pipeline. The CI pipeline seems doable; the Docker container spins up, we can create a deployment using command, and based on what weâve seen, we should be able to connect, populate data, run tests on functions/endpoints accessing/manipulating that data, etc. Running this all once, in isolation, seems to work ok.
datastore:
image: mongodb/atlas:v1.14.0
hostname: mongo_atlas
container_name: mongo_atlas
privileged: true
command: |
/bin/bash -c "atlas deployments setup --type local --port 27017 --bindIpAll --username root --password root --mdbVersion 7.0 --force && tail -f /dev/null"
ports:
- 27017:27017
volumes:
- ./run/docker.sock:/var/run/docker.sock
The one concern we have about CI is that there doesnât seem to be any caching of the MongoDB binaries downloaded in step 2 of creation of the cluster. Not being able to cache costs money in the form of additional pipeline minutes.
B. Local dev. Local dev is the real issue for us. We want to persist data, so that as devs start and stop containers as they need to, they donât have to keep recreating the entire database (losing whatever objects they may have populated on their own by running a mongorestore on shared data or whatever).
Or, in other words, we want to be able to create a dev deployment once, and basically restart it and reconnect to it whenever we start the container, storing the data on a persisted volume. Ideally, we could do all of this via a single command: create the deployment if it doesnât exist, start if it does.
Any input on how to do that would be wonderful. Here is what we have tried so far.
We have kinda been able to persist the data to a mapped volume, but have not been able to access the same deployment once a container is stopped. And although we can see the data on the mapped volume, we havenât been able to access that data again via the deployment (more on that below).
We tried giving the deployment the name dev. We searched for the .wt files in the Docker container, which led us to adding this volume mapping:
command: |
/bin/bash -c "atlas deployments setup dev --type local --port 27017 --username root --password root --mdbVersion 7.0 --force && tail -f /dev/null"
ports:
- 27017:27017
volumes:
- ./run/docker.sock:/var/run/docker.sock
- ./data/atlas:/var/lib/containers/storage/volumes/mongod-local-data-dev/_data
In my own attempts, that does allow me to create the deployment and persist the data. When first upping my docker-compose, I get:
mongo_atlas | 3/3: Creating your deployment dev...
mongo_atlas | Deployment created!
mongo_atlas |
mongo_atlas | connection skipped
mongo_atlas |
mongo_atlas | Connection string: mongodb://root:root@localhost:27017/?directConnection=true
⊠and I can connect using that connection string as well as see the data in my ./data/atlas folder.
If I then stop my container and re-up it using the same docker-compose, I have gotten a variety of responses.
Obviously, that docker-compose has a command of atlas deployments setup, which might well be inherently problematic to rerun.
In any event, I might be wrong, but there seems to be some relationship to the time elapsed since I stopped the container and the error that I get.
I seem to first get this if immediately re-upping:
mongo_atlas | Error: "dev" deployment already exists and is currently in "running" state
mongo_atlas exited with code 1
If I try to re-up rapidly after that, I get this:
mongo_atlas | 2/2: Creating your deployment dev...
mongo_atlas | Error: exit status 125: Error: network name mdb-local-dev already used: network already exists
If I get that ânetwork already existsâ error, it seems to repeat forever.
However, sometimes I have gotten this instead (which I think has happened when I have waited longer to re-up):
mongo_atlas | 3/3: Creating your deployment dev...
mongo_atlas | Error: failed to connect to mongodb server: connection() error occurred during connection handshake: auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
mongo_atlas exited with code 1
When I get that error, I am eventually able to restart the deployment by upping that same docker-compose, but it seems to re-initialize things with default data, losing whatever I tried to persist.
Again, that docker-compose has a command of atlas deployments setup. If I try changing the docker-compose command to this:
command: |
/bin/bash -c "atlas deployments start && tail -f /dev/null"
⊠and then immediately re-up after stopping my container, I get this message:
mongo_atlas | Error: currently there are no deployments
Basically, no matter what I have tried, once the container has been stopped, I have not been able to subsequently access the same deployment I created the first time I ran the docker-compose and keep my data.
If there are different commands that I should be running, Iâd be grateful to learn what they are â and some further documentation for maintaining local dev environments might be helpful.
- Running the container and deploying manually. I canât speak for @Igor_Prokopenkov, who may know more and have done more, but here is what I have done:
- Execute
docker run -p 27017:27017 --privileged -it mongodb/atlas bash
- Set up a deployment via
atlas deployments setup dev --type local --port 27017 --bindIpAll --username root --password root --mdbVersion 7.0 --force or similar
Running atlas deployments lists shows me this:
sh-4.4# atlas deployments list
NAME TYPE MDB VER STATE
dev LOCAL 7.0.4 IDLE
And I can connect using the connection string.
If I then run atlas deployments pause dev before stopping the container, then atlas deployments list gives me this both before I stop the container and after I restart it:
sh-4.4# atlas deployments list
NAME TYPE MDB VER STATE
dev LOCAL 7.0.4 STOPPED
And after restarting, I can run atlas deployments start dev and reconnect.
However, if I simply stop the container without first running atlas deployments pause dev, then I get this when running atlas deployments list after re-upping:
sh-4.4# atlas deployments list
NAME TYPE MDB VER STATE
dev LOCAL 7.0.4 IDLE
⊠but I canât connect using the connection string. I also canât pause it:
sh-4.4# atlas deployments pause dev
Error: exit status 255: Error: OCI runtime error: runc: exec failed: cannot exec in a stopped container
Note that it still continues to show as IDLE when I run atlas deployments pause list.
And if I try to run atlas deployments start dev, it hangs.
So we could follow an OS-agnostic dev approach by using the Docker image to run a dev deployment in this way, but remembering to pause the deployment every time before stopping the container â or even being able to â doesnât seem feasible.