I am trying to make a container where I install Mongo DB
server as well as python 3.8
with pymongo
module but it does not work. In the dumps folder I am trying to create a backup of tar.gz
file of which will have volumes with the host machine using the code mongodump --archive=backup.tar.gz --gzip
. How would I be able to change the code so that the container could be created and wont be faulty. How would I also be able to test if the backup is properly working?
Error when run docker-compose up --build
:
#0 3.837 Reading state information...
#0 3.895 E: Unable to locate package python3.8
#0 3.895 E: Couldn't find any package by glob 'python3.8'
#0 3.895 E: Unable to locate package python3.8-dev
#0 3.895 E: Couldn't find any package by glob 'python3.8-dev'
#0 3.895 E: Couldn't find any package by regex 'python3.8-dev'
Dockerfile
FROM mongo:4.0.4
# Install Python 3.8
RUN apt-get update &&
apt-get install -y python3.8 python3.8-dev python3-pip &&
ln -s /usr/bin/python3.8 /usr/local/bin/python3
# Install pymongo
RUN pip3 install pymongo
EXPOSE 27017
# Create src and dump directories
RUN mkdir /app
VOLUME ["/app/src", "/app/dump", "/data/db", "/var/www/html"]
CMD ["/bin/bash"]
docker-compose.yml file:
version: '3.2'
services:
py-mongo:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./src:/app/src
- ./dump:/app/dump
- ./mongo-data:/data/db
- ./mongo-app:/var/www/html
command: tail -f /dev/null
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=1234
ports:
- "27017:27017"
Tree Structure
.
├── Dockerfile
├── README.md
├── docker-compose.yaml
├── dump
├── mongo-app
├── mongo-data
└── src
2
Answers
The Docker images aim to be as small as possible. That’s why they often do not include software we have in our workstations.
In the Mongo image there’s no repository with python3.8 package for apt-get.
You have 2 options:
Choose which is better for your purpose.
Adding the repository:
The Python installation:
will work then.
A Docker container is a wrapper around a single process. This backup task is running as a separate process and so it probably needs to run in a separate container. Conversely, this means you can just use the standard Docker Hub
python
image for your script, and the unmodifiedmongo
image for the database.In the Compose file, you’d have separate containers for the database and script. You will need to pass along some connection information to both containers. I’ve specified these settings as environment variables; your code will need to get these settings back from
os.environ
.Now just running
docker-compose up -d
will start the whole thing. Notice that we’ve never specified an interactive shell as the main container command or usedtail
to "keep a container alive", and conversely, we don’t needdocker exec
or to manually start processes inside running containers.Your question has a very brief mention of an HTML directory. Whatever’s serving this would go into a third container, again possibly built
FROM python
and including a server framework like Django or Flask in itsrequirements.txt
file.