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.
Oct 26, 2023
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.

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.]

Install Docker

First things first—equip your machine with Docker. Whether you're on Windows, Mac, or Linux, you can snag the installation here. While installation nuances vary based on your OS, the process is straightforward and user-friendly.

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

Check out our LinkedIn account, to get insights into our daily working life and get important updates about BigQuery, Data Studio, and marketing analytics

We also started with our own YouTube channel. We talk about important DWH, BigQuery, Data Studio, and many more topics. Check out the channel here.

If you want to learn more about how to use Google Data Studio and take it to the next level in combination with BigQuery, check out our Udemy course here.

Have trouble setting up an ETL Pipeline on Y42 or if you are looking for help to set up a modern and cost-efficient data warehouse or analytical dashboards in general, send us an email at hello@datadice.io and we will schedule a call.