How to Run a Python Script using Docker?
Docker helps you to run your Python application very smoothly in different environments without worrying about underlying platforms. Once you build an image using dockerfile you can run that image wherever you want to run. Docker image will help you to package all the dependencies required for the application which will used further while running the container.
Docker is a set of platforms as a service (PaaS) products that use Operating system-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their software, libraries, and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating system kernel and therefore use fewer resources than a virtual machine. To know more about Docker refer to Introduction to Docker.
Creating Dockerfile for Python Application
Here are the detailed steps to create Dockerfile for the Python application:
Step 1: Creating the Files and Folders
We will be creating a Folder docker_2 at the desktop location in our PC. Inside the Folder another Folder called docker_assignment is created. Then two files dockerfile and test.py are created in this folder.
Folder –
Step 2: Creating the Dockerfile
Inside the dockerfile we will start by first taking the python base image from docker hub. A tag latest is used to get the latest official python image. It is very important to set your working directory inside your container. We have chosen /usr/src/app. All commands will be executed here as well as the images will be copied here only.
We have then copied the test.py file from my pc to the container current working directory(./ or /usr/src/app) by using the COPY command.
#Deriving the latest base image
FROM python:latest
#Labels as key value pair
LABEL Maintainer="roushan.me17"
# Any working directory can be chosen as per choice like '/' or '/home' etc
# i have chosen /usr/app/src
WORKDIR /usr/app/src
#to COPY the remote file at working directory in container
COPY test.py ./
# Now the structure looks like this '/usr/app/src/test.py'
#CMD instruction should be used to run the software
#contained by your image, along with any arguments.
CMD [ "python", "./test.py"]
Build the Docker Image for Python Application
Step 3: Building the Docker Container
After you have created both the Python script and the Dockerfile, you can now use the Docker build command to build your Docker Image.
Here -t is for adding tags so as to identify your image easily.
docker image build -t python:0.0.1 /home/roushan/Desktop/docker_2/docker_assignment
Step 4: Verify the Image Build
After you have built your Docker Image, you can list all the Images to check whether your image has been successfully built or not.
docker images
You will find your Image name listed here and with the tag name, you can easily find it.
Run the Docker Container for Python Docker Image
Step 5: Running the Docker Container
Now, you can use the Docker run command to run your Docker Container.
docker run python:0.0.1
After running the Docker Container, you will see the output printed after adding the two numbers.
Common Issues When Creating a Dockerfile and How to Fix Them
While creating Dockerfiles, it’s easy to encounter issues that can disrupt your workflow. Here’s a practical guide to some common problems and their solutions:
1. Syntax errors in Dockerfile
Problem: You might see an error like this
unknown instruction: RUNNN
Why it Happens: This typically happens due to a typo in the Dockerfile. For example, you might accidentally type RUNNN
instead of the correct RUN
.
How to Fix it?
- Carefully review your Dockerfile for spelling mistakes or unsupported instructions.
- Refer to Docker’s official documentation to verify the correct syntax.
- Using an editor like VS Code can help, as it highlights errors in Dockerfiles.
2. Dependencies not Installed
Problem: You might see an error like this
ModuleNotFoundError: No module named 'flask'
Why it Happens: This error indicates that the necessary Python packages haven’t been installed in the container.
How to Fix It:
- List all required Python packages in a
requirements.txt
file. - Make sure your Dockerfile includes a line to install these dependencies:
RUN pip install --no-cache-dir -r requirements.txt
- Confirm that the
requirements.txt
file is properly copied into the container using theCOPY
command.
3. COPY failed: File not found
Problem: You might see an error like this
COPY failed: stat /path/to/file: no such file or directory
Why it Happens: This occurs when the Dockerfile is trying to copy a file or directory that doesn’t exist in the build context.
How to Fix It:
- Ensure that the files or directories you reference in your
COPY
command exist in the same folder where the Docker build command is run. - Verify that the paths specified in the Dockerfile are correct.
- Always run the
docker build
command from the directory containing your Dockerfile.
4. Permission denied Errors
Problem: You might see an error like this
Permission denied
Why it Happens: This happens when the container lacks the necessary permissions to access or execute a file.
How to Fix It:
- Use the
chmod
command in your Dockerfile to ensure proper permissions:
RUN chmod +x ./script.sh
- Consider running your application as a non-root user for security and better reliability.
5. Container exits right after Starting
Problem: The container starts and then stops immediately without running the expected task.
Why it Happens: This error comes because the CMD
or ENTRYPOINT
in your Dockerfile is incorrect, or the script being executed has errors.
How to Fix It:
- Check container logs to understand why it stopped:
docker logs <container_id>
- Verify that the
CMD
orENTRYPOINT
instructions are correctly written. Example:
CMD ["python", "./app.py"]
- Use an interactive shell to debug:
docker run -it <image_name> bash
6. Docker image is too large
Problem: The Docker image takes up too much space.
Why it Happens: Your Dockerfile may have unnecessary layers or use a heavy base image.
How to Fix It:
- Use a lightweight base image like
python:3.8-slim
orpython:3.8-alpine
. - Remove temporary files during the build process:
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
- Combine multiple
RUN
instructions into one to reduce the number of layers. For instance:
RUN apt-get update && apt-get install -y python3 \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
7. Network problems while installing dependencies
Problem:
Failed to fetch URL https://pypi.org/simple/
Why it Happens: Docker is unable to connect to the internet to download required packages.
How to Fix It:
- Check your computer’s internet connection.
- If you are using a proxy, configure Docker to use it by setting environment variables:
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
- Switch to an alternate mirror for PyPI if the default one isn’t accessible.
8. Changes not reflected after rebuilding
Problem: You modify your Dockerfile or application, but the changes don’t appear after rebuilding.
Why It Happens: Docker caches intermediate layers from previous builds to save time.
How to Fix It: You can force docker to rebuild the image from scratch by using the --no-cache
option:
docker build --no-cache -t my-python-app .