Don’t forget to share it with your network!
Ajaysingh Narayansingh Rajpurohit
Sr Developer, Softices
Cloud & DevOps
16 March, 2026
Ajaysingh Narayansingh Rajpurohit
Sr Developer, Softices
Python scripts are commonly used for tasks such as automation, data processing, file handling, or building small APIs. A developer may write a script that runs perfectly on their own computer, but when another person tries to run it, errors often appear.
These problems usually occur because the environment on the other machine is different. The script may depend on specific packages, a particular version of Python, or certain system settings.
Common errors include:
The code itself may be correct, but the system configuration is not the same.
Using Docker solves this problem. Docker allows you to package your Python script together with the environment it needs. Anyone with Docker installed can run the script without installing dependencies or configuring Python.
In this guide, you will learn how to package a script written in Python into a Docker container so it runs the same way on any machine.
A Python script depends on more than just the code written in the
.py file. It also relies on libraries, system tools, and
environment settings. When these differ between machines, the script may
stop working.
Many Python projects use external libraries. If another user does not have those libraries installed, the script will fail.
Example error:
ModuleNotFoundError: No module named 'pandas'
This means the required package is not available in the environment.
Different versions of Python may behave differently. A script written for Python 3.11 may not run correctly on Python 3.8 because some features may be missing or deprecated.
A script that works on macOS might fail on Windows or Linux because of:
For example:
/home/user/file.txt
is common on Linux or macOS, while Windows uses:
C:\Users\user\file.txt
Some scripts rely on environment variables or system libraries. If these are not configured properly, the script may fail even if the code is correct.
These issues make it difficult to share Python scripts between developers.
Docker solves this by packaging the script together with its environment.
Docker allows developers to create containers to run applications. A container includes:
Once packaged inside a container, the application runs the same way on any machine that has Docker installed.
Instead of sharing a script and asking others to install several dependencies, you can simply share a Docker image.
To follow this guide, you should have:
You can download Docker from the official website and install it on your system. Then, you can begin creating your container.
Start with a simple script called app.py.
import requests
import sys
import os
def main():
# Example of using environment variables
api_url = os.getenv('API_URL',
'https://api.github.com')
# Example of handling command-line arguments
if len(sys.argv) > 1:
api_url = sys.argv[1]
try:
response = requests.get(api_url)
print(f"Status Code:
{response.status_code}")
print(f"Content Type:
{response.headers.get('content-type', 'unknown')}")
except Exception as e:
print(f"Error: {e}")
sys.exit(1)
if __name__ == "__main__":
main()
This script:
Since it uses the requests library, that dependency must also
be installed.
Python projects usually list dependencies in a file called
requirements.txt.
Create a file named requirements.txt with the following
content:
requests==2.31.0
This file tells Python which packages are required for the project and specifies exact versions for reproducibility.
A .dockerignore file prevents unnecessary files from being
copied into the container.
Create the file with the following content:
__pycache__
*.pyc
.env
.git
.gitignore
.vscode
.idea
This keeps the Docker image smaller and cleaner.
A Dockerfile defines how the container should be built.
Create a file named Dockerfile in the same folder.
# Use an official Python runtime as the base image
FROM python:3.11-slim
# Set environment variables
ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1
# Set the working directory in the container
WORKDIR /app
# Copy the requirements file first (for better caching)
COPY requirements.txt .
# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Specify the command to run when the container starts
CMD ["python", "app.py"]
Here is what each instruction does:
Next, build the Docker image.
Run this command in your project folder:
docker build -t python-script .
This command creates a Docker image named python-script.
The build process reads the Dockerfile and installs everything needed for the script.
Once the image is built, run the container:
docker run python-script
The container will execute the Python script and display the output.
Example output:
Status Code: 200
Content Type: application/json
This confirms the script runs successfully inside the container.
After packaging the script with Docker, sharing it becomes much easier. There are a few common methods.
Upload the project to a Git repository containing:
Anyone can clone the repository and run:
git clone repository_url
cd project
docker build -t python-script .
docker run python-script
You can also upload the image to a container registry like Docker Hub.
Steps:
1. Tag the image with your Docker Hub username:
docker tag python-script username/python-script:latest
2. Push the image
docker push username/python-script:latest
3. Others can download and run it with:
docker run username/python-script:latest
For systems without internet access:
# Save the image to a tar file
docker save -o python-script.tar python-script
# Compress for easier transfer
gzip python-script.tar
# On the target machine, load the image
docker load -i python-script.tar.gz
Using Docker provides several practical advantages.
Docker is especially helpful for certain types of Python projects:
Here's a quick reference of useful Docker commands:
Command |
Purpose |
|---|---|
docker build -t name . |
Build an image from a Dockerfile |
docker run name |
Run a container from an image |
docker ps |
List running containers |
docker ps -a |
List all containers (including stopped) |
docker stop container_id |
Stop a running container |
docker rm container_id |
Remove a container |
docker rmi image_name |
Remove an image |
docker images |
List all images |
docker logs container_id |
View container logs |
docker exec -it container_id bash |
Access a running container's shell |
Solution: Check your internet connection and ensure the package name is correct. Use specific versions to avoid compatibility issues.
Solution: Check the logs with
docker logs container_id. Add debugging output to your script.
Solution: On Linux, you may need to adjust permissions or
use :Z for SELinux:
docker run -v $(pwd)/data:/app/data:Z python-script
Solution: Use slim Python images
(python:3.11-slim) and clean up cache in Dockerfile:
RUN pip install --no-cache-dir -r requirements.txt && \
rm -rf /root/.cache/pip
requirements.txt for
reproducibility
Sharing Python scripts can be difficult when different systems use different environments. Missing packages, incompatible versions, and configuration issues often cause scripts to fail.
Using Docker to make a Python script shareable solves this problem by packaging the code and its environment together. Anyone with Docker installed can run the script without additional setup.
By creating a Dockerfile, building an image, and running a container, you can make your Python scripts portable and easy to share across teams and machines.
For developers who frequently collaborate or distribute scripts, this approach saves time and prevents many common environment-related issues. Whether you're sharing a simple automation script or a complex data processing pipeline, Docker ensures it runs exactly as intended, everywhere.