Share this post

I’m pretty sure everyone who has something in common with developing code has heard about Docker. Its popularity isn’t a coincidence. It’s a handy toolkit that has changed the way people develop their applications. Thanks to Docker testing, deploying and scaling applications has become much simpler.

Let’s imagine a situation when we developed an application with many prerequisites like additional tools and packages. If we want someone to run our project we have to provide the code and description of how to prepare the system to be compatible with the code. A person who wants to run it has to install all those dependencies. Needless to say, in such situations, there are often various kinds of errors and incompatibilities. Docker solves this problem because it allows you to provide the application with all necessary dependencies, so everybody can easily run your app.

In this article, I want to introduce the basics of Docker and show you how to use it in a very simple way. I hope all information and tips collected here will convince you to use Docker in your application.

What is Docker?

Docker allows you to create a portable, lightweight container that contains the program and all its dependencies, such as libraries, configuration files, environment, databases, packages, etc. The container can be available to other people who can run it on any operating system like Linux, Windows, macOS. 

Docker is often compared to virtual machines, but there is a very important difference between them. Virtual machines have their own operating system, which explains why virtual machines, especially when we run many of them on one computer, could be a heavy load. Additionally, along with setting up a new virtual machine, all required libraries and tools have to be installed. Docker solves this problem using the computer operating system and  isolates some resources to create an isolated environment.

Main pros of using Docker:

  • you can easily test different versions of the software
  • you can distribute your application with all system requirements
  • you can check if your application works on all operating systems
  • docker creates a lightweight and isolated runtime environment

Find out about other uses of Docker.

Docker images

Docker image is a read-only file containing a set of instructions for creating a docker container. You can think about docker images as a CD that contains information on how to install the game, but it isn’t the game itself. It’s only a template, the set of instructions on how to do that. 

If you want you can create your image or use existing ones that are published to the https://hub.docker.com/ website. To create your image you need to write a file called Dockerfile. What is amazing about docker images is that they are made of layers. It means you can use other images, add all requisites that fit your needs, and voilà! A new docker image is created. Want to change your image? Just add all changes to the dockerfile and that’s it –  a piece of cake!

Docker containers

Docker containers are created based on the docker images. Thanks to containers, we can create an isolated and self-sufficient environment in which, for example, we can run an application, create a database or host a website. All docker containers work independently – that’s why it is possible to store several containers in one operating system. Communication between containers is possible as well. The only thing you have to do is to create the appropriate network connection between the containers.
To better understand the difference between images and containers let’s look at the example below.

First of all, we want to create a new image. In this example, it will be based on the ubuntu:20.04 image and we will run a command which shows information about the environment.

 

 

 

 

 

Let’s build our image:

To list all built images run docker images command:

You can see our image named my_ubuntu_20_image was created 3 minutes ago and weighs 72.8 MB. 

Next, let’s run our docker image to create a docker container and see the result of cat /etc/lsb-release command. To do that run docker run my_ubuntu_20_image command.

To list all created containers run docker ps -a command.

Simple Python project with external libraries

In this chapter I want to show you how to containerize simple Python application. I believe this hands-on example will show you that using Docker is simple. 

Let’s start with an example of Python code. I prepared a basic script main.py that calls https://aqicn.org webpage and, using the additional library BeautifulSoup, reads information about air quality in Warsaw.

Like I mentioned before, this script uses additional libraries. Let’s collect all requirements in the requirements.txt file.

Let’s imagine you want to send this application to your boss. What does he have to do to run it? 

  • determine and install proper Python version
  • install all requirements from the requirements.txt
  • ask you how to run your script (maybe he has to pass some additional arguments)

Confused? Let me show you how to make life easier with Docker.

Containerize Python application

The only thing you have to do is to download Docker (instruction available here: https://docs.docker.com/desktop/#download-and-install) and write Dockerfile.

Dockerfile uses python:3.8 image, adds files main.py and requirements.txt to the docker directory, installs all requirements and runs the script. 

Let’s get back to the story. What needs to be done to run your containerized script?

  • install docker if needed
  • build the docker image and run it

Yes, that’s it!

If you would like to improve your Python code click here.

Conclusion

Docker toolkit is very popular these days and every software developer should know how to use it. How it was proved in the article using Docker isn’t difficult. The applications developed with Docker are more portable, easier to test and scalable. I hope this article inspired you to dockerize your apps.

Data Pipeline Automation_2

Author

Share this post
Close

Send Feedback