By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.
Jun 20, 2024
More

How to Dockerize your Python Script

From Script to Container. By Sanu Maharjan

Ever been hit with the exasperating line, "It works on my machine"? This phrase has been the bane of many developers' existence, echoing the unpredictability and inconsistency of diverse computing environments, like variances in dependencies, operating systems, environment variables, Python versions, and even networking, which can wreak havoc. Enter Docker—a superhero in the world of reproducibility. This post will guide you through the exciting journey of wrapping a Python script inside a Docker container.

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications. It enables developers to package applications along with their dependencies into a standardized unit called a container. Containers ensure that the application runs consistently across different computing environments, solving the age-old problem of "It works on my machine."

Key Concepts of Docker

To understand Docker better, let's break down its core components:

Dockerfile: A Dockerfile is a plaintext configuration file containing a collection of commands and parameters. It describes the steps required to create a Docker image, akin to a recipe with step-by-step instructions on how to make a dish.

Docker Image:A Docker image is a lightweight, standalone, and executable software package. It includes everything needed to run a piece of software: code, runtime, system tools, libraries, and settings. Think of it as a cooked meal following the recipe—ready to be served but not yet being consumed.

Docker Container:A Docker container is a runtime instance of an image. It is what the image becomes in memory when executed. This is like a diner eating a meal—the actual process of consuming the dish. If more people want to eat the same dish simultaneously, you serve them multiple instances of that meal, just like running multiple containers from the same image.

Here's a simple illustartion to understand the concept of docker.

Key concept of Docker (Image source: thedeploymentguy.medium.com)

Why Use Docker?

  1. Consistency: Containers encapsulate all the dependencies of an application, ensuring it runs the same way in different environments—development, testing, and production.
  2. Isolation: Each container runs in its own isolated environment, preventing conflicts between applications running on the same host.
  3. Portability: Docker containers can run on any system that supports Docker, be it a developer's laptop, on-premises servers, or cloud environments.
  4. Scalability: Docker makes it easy to scale applications horizontally by running multiple container instances.

Installing Docker

Before diving into containerization, you need to install Docker on your machine. Docker provides installers for different operating systems:

  • Windows: Follow the instructions here.
  • Mac: Follow the instructions here.
  • Linux: Follow the instructions here.

The installation process is straightforward and user-friendly, with Docker providing detailed guides for each platform.

Creating a Dockerized Python Script

For our maiden voyage, we'll be crafting a simple Python script named main.py that just proclaims, "Hello World!" Now, you might wonder, "Why put in the effort to containerize such a trivial script?" Well, while this script would indeed run smoothly on almost any machine, our goal is to understand the magic of Docker. And remember, once you grasp the basics, scaling up to more intricate Python applications will be a breeze.

When I run this script in my VSCode, it dutifully delivers a "Hello World!" But, imagine if I were to share this magnificent script with a friend, and they encounter a mysterious glitch. That's where Docker comes into play!

[For those itching for the full code, here's a link to my GitHub repo.]

Create a Dockerfile

Before creating the Dockerfile, let’s understand some terminology of docker. There are three terms that you would come across very frequently, namely Dockerfile, Docker Image, and Docker Container. In very general terms,

  • Dockerfile is a plaintext configuration file that contains a collection of commands and parameters used by Docker to create a new Image.
  • It is like a recipe with step-by-step instructions on how to make a dish.
  • Docker Image is a lightweight, standalone, and executable software package that includes all the necessary information to run a piece of software, including the code, runtime, system tools, libraries, and settings.
  • It is like a cooked meal following the recipe. It's ready to be served/eaten, but it's not yet being consumed.
  • Docker Container is a runtime instance of an image. It is what the image becomes in memory when executed.
  • It is like a diner eating a meal. It's the actual process of consuming the dish. If more people want to eat the same dish simultaneously, you serve them multiple instances of that meal, just like running multiple containers from the same image.

With that in mind, now let’s create a dockerfile or the blueprint for the Docker container. In the same directory as your main.py, create a file named Dockerfile. No extension is needed!

Here's how it should look like:

Understand the Dockerfile

Let's break down what each part does:

FROM python:3.9

  • We're starting with a version of Python 3.9, which has Python preinstalled.

WORKDIR /app

  • Inside the container, we'll work in the /app directory.

COPY . .

  • We copy everything from our current directory on the computer into our current directory inside the container.

CMD ["python", "main.py"]

  • Once our container starts, it will execute main.py using Python.

Building the Image

Pop open a terminal or command prompt in your project's directory. Fire off:

docker build -t docker-tut .

The -t docker-tut portion bestows a name upon our Image for easy reference later. Feel free to change your Image with a name of your choosing. And remember—the trailing dot (.) is essential! Once you run this command, it will build the image and it should look something like this:

Breathe Life into Your Script with Docker

Time for the finale. To run our Python script within the Docker realm, execute:

docker run docker-tut

Voila! Your main.py will run inside its very own Docker container.

Conclusion

In the vast cosmos of coding, Docker emerges as a beacon of consistency. With Docker, "It works on my machine" evolves from a sigh of frustration to a badge of honor. By following the steps above, you've not only learned to Dockerize a simple Python script but have also embarked on a transformative adventure into the realm of containers. As you sail further into Docker waters, may your coding journey be smooth and bug-free! 

Further links

Follow us on LinkedIn for insights into our daily work and important updates on BigQuery, Data Studio, and marketing analytics.

Subscribe to our YouTube channel for discussions on DWH, BigQuery, Looker Studio, and Google Tag Manager.

If you are interested in learning BigQuery from scratch, get access to our free BigQuery Course

Elevate your skills with Google Data Studio and BigQuery by enrolling in our Udemy course.

Need help setting up a modern, cost-efficient data warehouse or analytical dashboard? Email us at hello@datadice.io to schedule a call.