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.
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."
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.
Before diving into containerization, you need to install Docker on your machine. Docker provides installers for different operating systems:
The installation process is straightforward and user-friendly, with Docker providing detailed guides for each platform.
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.]
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,
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:
Let's break down what each part does:
FROM python:3.9
WORKDIR /app
COPY . .
CMD ["python", "main.py"]
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:
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.
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!
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.