Answer the question
In order to leave comments, you need to log in
What is the correct way to use Docker to execute a Python file?
Hello! I have a python script that uses some dependencies. I need to make sure that when I change this script, I can run docker-container with dependencies already installed and at the same time, so that changes in the file are taken into account. What is the best way to do it?
Answer the question
In order to leave comments, you need to log in
Look here: https://hub.docker.com/_/python/
In a nutshell, you can:
1. Create your own image based on the selected version of Python with the dependencies you need for your script installed and run the container with the mounted script.
2. Or build a new image by simply adding a script to it. Since the dependencies have already been added before, the docker will skip the dependency installation step and the new image will be ready almost instantly.
Image example:
FROM python:3-alpine
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
docker build -t docker-test .
docker run -it --rm --name my-running-script -v ${PWD}:/usr/src/app docker-test python your-daemon-or-script.py
1. We make a container on the image of the desired version of python.
2. "We go" into it.
3. Install the required packages via pip.
4. Make a pip freeze.
5. Copy the output to external requirements.txt
6. Exit the container.
7. We register in the docker file copying the requirements.txt file to the container.
8. We write the command for installing dependencies via pip from requirements.txt into the docker file
9. We build the container on a new one.
10. To start the container, we add the connection of the external folder with the folder in the container through the mount.
11. Run the container.
12. "We go" into the container.
13. We edit the code in the external folder and run it in the container with handles.
Achieved that the code is working. Now you can create an image with the application. To do this, we edit the docker file to add code copy from the folder to the container and run the application. We build and test the image through the creation of a container. Upload the image to the docker repository. Based on the image, we launch the container on an external host. "Sad" that it's over.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question