How to Run a Python Script Using Docker

by Joana Maria
12 minutes read

How to Run a Python Script Using Docker


Sprinted Python Manuscript Docker

Running Python manuscripts is one of the most consistent jobs in automation. Singularly, taking care of dependencies throughout different mechanisms can be made complex. That’s whereby Docker comes in. Docker permits you package your Python script along via with one voice its obligatory dependencies into a container, assuring it runs the exact same means on any kind of equipment. In this response-by-response overview, we’ll walk via the procedure of devising a real-spiritedness Python script as well as sprinting it within a Docker container.

Why Swipe advantage of Docker for Python Manuscripts

As soon as you’re functioning via Python manuscripts, points can lugging out gain serviceability of awkward/contacted awfully speedy. Unalike vacancies need different libraries, as well as what runs on your equipment might respite on a man else’s. Docker solves that by merchandise packaging your script as well as its ambience with each other. So instead of dictum “It works on my equipment”, you can be sure it works the exact same almost everywhere.

It in enhancement retains your mechanism unspoiled. You wear’t have to place every Python package worldwide or agitation around iteration confrontations. Everything stays within the container.

If you’re deploying or handing your script off to a man else, Docker provides that simplified, as well. Zero arrangement instructions, most certainly no “place this as well as that”. Just one command, as well as it runs.

Create the Python Manuscript

Let’s invent a responsibility brochure to preserve your Python script as well as Dockerfile. Once invented, navigate into this brochure making gain serviceability of of the cd command:

mkdir docker_file_organizer
cd docker_file_organizer

Manifestation a script named “organize_files.py” to scan a brochure as well as group files into folders based on their paper extensions:

nano organize_files.py

Paste the obeying code into the “organize_file.py” paper. Under, we gain serviceability of two pre-constructed Python factors, named os as well as shutil, to swipe care of files as well as invent catalogs dynamically:

import os
import shutil

SOURCE_DIR = "/files"

def organize_by_extension(directory):
try:
for fname in os.listdir(directory):
path = os.path.join(directory, fname)
if os.path.isfile(path):
ext = fname.split('.')[-1].lower() if '.' in fname else 'no_extension'
dest_dir = os.path.join(directory, ext)
os.makedirs(dest_dir, exist_ok=True)
shutil.move(path, os.path.join(dest_dir, fname))
print(f"Moved: {fname} → {ext}/")
except Exception as e:
print(f"Error organizing files: {e}")

if __name__ == "__main__":
organize_by_extension(SOURCE_DIR)

In this script, we organize files in a granted brochure based on their extensions. We gain serviceability of the os module to list the files, discover if each thing is a paper, extract its expansion, as well as invent folders named after those extensions (if they wear’t already exist). Then, we gain serviceability of the shutil module to slide each paper into its integrating folder. For each slide, we print a message evidencing the paper’s brand name-gimmicky locality.

Manifestation the Dockerfile

Now, invent a Dockerfile to define the ambience in which your script will sprinted:

FROM python:latest
LABEL maintainer="[email protected]"
WORKDIR /usr/src/app
COPY organize_files.py .
CMD ["python", "./organize_files.py"]

We gain serviceability of this Dockerfile to invent a container via Python, consist of our script to it, as well as lugging out sure the script runs freely when the container propels:

Manifestation Docker Document
How to Run a Python Script Using Docker 17

Construct the Docker Image

In yesteryear you can construct the Docker image, you have to place Docker initially. After that, sprinted the obeying command to package every little thing into a Docker image:

sudo docker build -t file-organizer .

It reads our Dockerfile as well as puts with each other the Python arrangement as well as our script so they’re willing to sprinted in a singular container image:

Construct Docker Image
How to Run a Python Script Using Docker 18

Manifestation a Instance Folder via Documents

To surf through our script in response, we invent a checkup folder named “sample_files” via a few files of different kinds. We invented these files merely to lugging out the folder a diminutive snippet awkward as well as surf through how our Python script handles it:

mkdir ~/sample_files
touch ~/sample_files/test.txt
touch ~/sample_files/image.jpg
touch ~/sample_files/data.csv

Sprinted the Manuscript Inside Docker

Finally, we sprinted our Docker container as well as place the instance folder into it. The -v flag stains your urbane “~/sample_files” brochure to the “/files” brochure in the container, which permits the Python script to read as well as organize files on your host equipment:

docker run --rm -v ~/sample_files:/files file-organizer

Under, we gain serviceability of the --rm establishment to remove the container freely after it coatings sprinting, which conserves disk room:

Sprinted Manuscript In Docker
How to Run a Python Script Using Docker 19

In the run out, we gain serviceability of the tree command to discover if the files have been sifted into folders based on their extensions:

tree sample_files
Confirm Result Wearing Tree Command
How to Run a Python Script Using Docker 20

Note: The tree command isn’t pre-design on most mechanisms. You can comfortably place it making gain serviceability of of a package manager prefer apt on Ubuntu, brew on macOS, as well as so on.

Final Pointers

Wearing your Python script sprinting within Docker, you’re with one voice kit to swipe full privilege of a unspoiled, transportable, as well as recurrent figment arrangement. You can comfortably reuse this containerized workflow for other automation jobs, share your script without upsetting around dependencies, as well as preserve your mechanism blunder-free of price. As a next response, ponder dissecting how to construct multi-script Docker images, itinerary cylinders via cron vacancies, or integrate your manuscripts via other tools prefer Git, Jenkins, or also cloud belvederes to improve your automation as well as deployment procedure.

Related Posts