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.
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.
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:
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.
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.
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.
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:
The next step is to create a Dockerfile:
RUN mkdir -p /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.
# Virtual Environment
# Project Settings
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.
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.
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.
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.
As we have shown in our example, using Docker with https://www.netguru.com/blog/development-with-python-what-kind-of-applications-can-you-build-in-python[. Developers are easily able to harness the benefits of these two powerhouses working together.