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.
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.]
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.
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:
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!
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 firstname.lastname@example.org and we will schedule a call.