What is Docker and How to Use It With Python (Tutorial)

Photo of Krzysztof Szyda

Krzysztof Szyda

Updated Sep 22, 2023 • 8 min read
nikita-kachanovsky-445394-unsplash-962215-edited

If you live in the world of tech, there is an excellent chance that you’ve heard the buzz surrounding Docker, along with the claims that it is set to change the face of programming.

There’s no denying that the powerful container platform is making waves, but what is behind its meteoric rise to popularity, and should you be jumping on the bandwagon?

Here, we take a look at the reasons why developers are switching to Docker in droves, plus the benefits of using it with Python. We’ll also walk through an example of how to use Docker with a simple Python-based app.

What is Docker?

For those not yet fully in the loop, Docker is an open-source software containerization platform and is now the most popular alternative to virtual machines in the world. It provides the ability to isolate an application and its related dependencies into a lightweight, self-sufficient container that can be run virtually anywhere.

Because of this, Docker is widely used by developers and DevOps engineers to speed up the development process and standardise development and production environments.

What are the benefits of using Docker?

Although released only five years ago, Docker has enjoyed a spectacular rise to fame and widespread adoption. According to Docker:

  • 3.5 million apps have been containerized using their technology,

  • 37 billion containerized applications have been downloaded,

  • 15,000 Docker related jobs have been listed on LinkedIn,

  • Over 200 active Docker user groups now exist.

These impressive numbers are unsurprising, given the benefits of using the tool. Here are just a couple of them:

Lower resource requirements compared to running a virtual machine

Because containers use shared operating systems and leave behind the unnecessary padding associated with virtual machines, they are far more lightweight and efficient in terms of system resources. This also has the added benefit of making them more cost effective and faster.

Support from the biggest cloud services

A huge benefit of Docker is that all of the major cloud computing providers such as Amazon Web Services (AWS), Azure, and Google Cloud Platform (GCP) have introduced individual support for the platform. This means that containers can easily be ported between environments, provided that the host operating system supports Docker.

What makes Docker good with Python?

Fans of Python will be pleased to know that the language is well catered for by Docker. The latest versions of Python are available as Docker images and, once configured, the image will ensure a non-changing environment essential for the development, testing, and production of applications. Also, developing and distributing software is made easy thanks to Docker Hub.

Docker works best with web applications such as Django or Flask, but can be used in any other Python-related area.

How to use Docker with Python—Step by step instructions

Let’s take a look at an example of how Docker can be used with a simple Python application. This example assumes a basic level of competency in both technologies.


We will write an application similar to Docker’s ‘hello-world’: https://hub.docker.com/_/hello-world/. Its main purpose will be to print out a “Hello World!” message when we run the container, but using an app that’s purely Python.

To do this, we will have to create a new project. I’m using the PyCharm IDE, which automatically creates a venv directory with Python 3.7 for me.

I create a new file called main.py and fill it with my complex logic:

image3-7

Code:

print("Hello World!")

The next step is to create a Dockerfile:

image5-8

Code:

FROM python:3.7

RUN mkdir -p /var/docker-example

WORKDIR /var/docker-example

COPY ./ /var/docker-example

ENTRYPOINT python /var/docker-example/main.py

First, we need to decide which base image we want to use in ours. We can search for images in Docker Hub. Official Python images can be found here https://hub.docker.com/_/python/.

Next we run some commands to create a directory in our Docker image where we can store our project. It would be /var/docker-example.

Then we copy our entire project.

Finally we set an entrypoint, which is the command and parameters that will be executed first when we run our Docker image.

As we’re using PyCharm, it creates files and directories that I do not need in our project, such as .idea, and venv. We’re also using Git as version control, so there is also a .git directory.

To handle this, we will create a .dockerignore file. It looks similar to the .gitignore file.

image2-14

# Virtual Environment


venv/

# Project Settings

.idea/

# git

.git/

All that is left for us to do now is to build an image and then run it.

We can build our image by executing the command ‘docker build . -t docker-example

Where . indicates that our context (Dockerfile) is in the current directory. We also want to add a tag called docker-example to it so that we can identify the image more easily.

image4-7

Docker pulled Python:3.7 image. Since we didn’t have it before and then executed all steps from our docker file, docker-example is already built and can be used.

image1-21

We can run it by simply typing ‘docker run docker-example’:

As we can see, our entrypoint command was executed successfully.

You can find this example on the repository: https://github.com/krzyszti/docker-example.

Wrapping Up

The benefits of using Docker are almost unparalleled by any other technology. There may be other containerization platforms on the market, but none of them have yet managed to provide a product that can rival Docker.

Among its many benefits, Docker can vastly reduce resource requirements, which also has the effect of reducing costs, and increasing speed. In addition, the tool’s popularity has ensured that it is now supported by the major cloud computing providers which is invaluable in ensuring good portability.

Photo of Krzysztof Szyda

More posts by this author

Krzysztof Szyda

Build impactful web solutions  Engage users and drive growth Start today

Read more on our Blog

Check out the knowledge base collected and distilled by experienced professionals.

We're Netguru

At Netguru we specialize in designing, building, shipping and scaling beautiful, usable products with blazing-fast efficiency.

Let's talk business