Why use Docker?

Diogo Lopes
6 min readJul 13, 2021

--

Everyone has heard of Docker and how it is an amazing tool for developers. But what is Docker? What are the advantages of using Docker? We’ll take a look at how Docker was built, why it was built and how Docker can help you set up a brilliant application.

By definition, Docker is a Platform as a Service (PaaS) used to deliver software in packages called containers. Docker does that by using OS-level virtualization, that is, the kernel allows multiple user spaces instances inside the same OS.

Need for Docker

Now, you are probably asking “But why do I need Docker for my software projects?” and you are right to ask. To put into another words, Docker allows you to run software without any “dependencies”.

Let’s say you develop a Python application: when you do so, you have multiple dependencies such as Python modules, the Python environment, and OS level dependencies perhaps. Imagine that you are developing an app on a Ubuntu machine, but you want for the application to be able to run on different OS, without the need of having even a Python environment installed, you can use Docker to do so.

Most applications are made to run on different machines for everyone, with or without software engineering knowledge. So, Docker comes in hand when developing such kind of an application.

How does Docker do it?

As previously explained, Docker uses OS-level virtualization to encapsulate applications inside packages, called containers.

Container Architecture

So, let’s go step by step of the Docker functioning:

  1. The apps live inside packages called containers, which contain a lightweight version of the application with all the dependencies needed for its functioning
  2. Containers are build on top of Docker Engine; the technology responsible for building and setting up the containers, acting like a client-server application
  3. Docker Engine is built on top of the host OS of your machine. Let’s say you are running the containers on a Linux machine, then Docker Engine will adapt the building process according to your machine’s OS
  4. Last, we have the infrastructure, the hardware. As you know, all OS are built on top of a specific hardware

Docker vs Virtual Machines

You may be wondering why not just use a Virtual Machine (VM) to do the work? Well, Docker becomes more efficient than a VM and here’s why.

  • Containers are only a separate user space of the OS, meaning they all share the same kernel resources
  • On the other hand, VM’s use their own OS (they do not share the host OS) and besides taking a user space, they also take up kernel space.
    So, in theory, if you have 3 VM’s running in your machine, each one would take up kernel space, which would translate in a lower performance and higher CPU usage

Building an application with Docker

Let’s see how we can create our own application and build it as a Docker container, and run it on different OS.

Build a simple Python script

In order to run an application in our container, we must create a Python script. This simple Python script takes an input from the user and creates a random matrix.

Build a requirements.txt

In order for the container environment to be able to run Python scripts, we must specify its dependencies in a file called requirements.txt. Note that random is a built-in Python module, so it doesn’t need installation.

//requirements.txtnumpy

Creating a Dockerfile

Second, we will need to define how our container is being built, and for that we are going to use a Dockerfile. A Dockerfile is a description file which describes what dependencies should the container contain.

FROM python:3.7.9-stretch#Installs dependenciesRUN apt-get update && apt-get install -y \
software-properties-common \
curl \
git \
python3-pip
#Upgrades pip for every iterationRUN pip install --upgrade pip
WORKDIR /home/my-project
COPY ./requirements.txt /home/my-project/requirements.txt
#Installs all module dependencies
RUN pip install -r requirements.txt
#Copy necessary files into environment
COPY ./test.py test.py
#Tells Docker shell what command to execute on build
CMD [ "/bin/bash","-c","python3 test.py" ]

Let’s break down the Dockerfile:

  1. First we must declare what other Docker images we wish to use, we can choose to not use any Docker image available on Docker Hub, but for this example, let’s use the Python image
    We can import an image by using the FROM instruction
  2. Next, we specify what command line dependencies our little container needs, such as command line dependencies (git, curl, etc) using RUN to install those dependencies
  3. Then, since we are building a Python application, we must install all the necessary Python modules for a proper application functioning
  4. Copying files: we must copy to our application environment /home/my-project some files we wish using the COPY instruction
  5. Finally, we want for the application to start when the container building process is finished. To do so, we must use the instruction CMD for specifying which command should be executed

Building a container

$ docker build -t <image_name> .

This should be the output of your build. Note that the . refers to the current work directory

Running the container

$ docker run -it <image_name>

Publishing your Docker image

Images are similar to containers. Basically, an image is a read-only template of one or more containers. An image can be made up of multiple container.
In order to give other people the chance to use your image, you must publish it inside a repository on Docker Hub.

  1. Register your account
  2. Create a repository

Now that you’ve successfully created a repository for your Docker image, we must push it.

Tag creation

Each image published on Docker Hub, must possess a tag. The tag identifies the image by version for example.

$ docker login$ docker tag <image_name> <repository_name>:latest

Push image

$ docker push <repository_name>:latest 

Using the image in another machine

If you wish to pull the Docker image and test it on your machine, all you must be assure is to have Docker installed, and then:

$ docker pull <image_name>

Use and test the Docker image

The Docker image built throughout this article is available here.
To pull the Docker image and use it in your machine:

$ docker pull cyberking18/medium-test

--

--

Diogo Lopes

Software Engineer | Building a Discord Bot for fun, moved by coffee