Using docker-compose, how do I access another container's bash shell within a different container's shell?

I currently have two containers within a docker-compose. One container runs a mongoDB while the other is running a flask server that uses the mongoDB. I have a problem calling mongodump within the flask server container as that shell doesn’t recognize mongo. How can I access the other container’s shell so I can call mongodump successfully?

This is my docker-compose.yml file:

services:
mongo:
build:
context: .
dockerfile: mongo.dockerfile
ports:
- “27017:27017”
volumes:
- /Users/nmserver/Documents/RunningProjectWiki/backup/2024.04.30.220222:/data/db/backup
networks:
- pw-network
environment:
MONGO_DATA_DIR: /data/db/backup

project-wiki:
build:
context: .
dockerfile: app.dockerfile
networks:
- pw-network
ports:
- “31415:31415”
depends_on:
- mongo
volumes:
- /Users/nmserver/Documents/RunningProjectWiki/uploads:/app/uploads
environment:
MONGO_DATA_DIR: /data/db/backup
DB_SERVICE: mongo

networks:
pw-network:
driver: bridge

Hello and welcome,
why not install mongodump within the Flask-image/container and use this to create the database-dump?
At least that is the way I’ve used to create dumps from PostgreSQL- and MariaDB-databases.

That’s a great idea, I am just unsure how to get mongoDB into the flask container. The flask container is running python 3.6 while the mongo is running a docker image of mongo 3.4. How would I get mongo into the python image container?

Hello,

I’ve tested with the image python:3-alpine
Within the Dockerfile I installed m̀ongodump using the following command:

apk add mongodb-tools

Afterwards I was able to dump a collection from a database from the MongoDB running in its own container on the same Docker-host using this command

mongodump --uri=mongodb://user:password@172.17.0.1/databasename --collection=thiscollection

If you are using a different python-image as base you might have to use the apt-way instead of the apk-way within alpine.

Thank you,

I appreciate the help because it got me in the right direction. Unfortunately the install command you used didn’t work for my image of python. (The official python image, version 3.6) But I did end up finding something that worked.

Here are two parts of my dockerfile:
FROM python:3.6
...
RUN wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-debian92-x86_64-100.3.1.deb && \ apt install ./mongodb-database-tools-*.deb && \ rm -f mongodb-database-tools-*.deb