I'll show you the simplest docker container example. If you hate dependencies it will be your best ally.
What is Docker?
You may haven't heard anything about Docker, however, you have certainly head about virtual machines and software such as VirtualBox or VMware. Docker is used to isolate the applications we are developing through virtualization, nevertheless, its internal development is very different from virtual machines.
Docker VS Virtual Machine
In the following image, you can see the different implementation of each one.
Virtual Machine VS Docker
Starting from the bottom to up, the two layers represent almost the same, however, the third one has totally different functionality.
The hypervisor is the software that is managing the operating systems that have been virtualized by distributing hardware resources such as CPU and volatile/non-volatile memory. The equivalent in containers is the Docker Engine which is responsible for managing hardware resources. But, unlike the hypervisor, it doesn't have to assign the hardware to each of the operating systems, what it does is optimize the resource between the different containers sharing with the local operating system.
On the other hand, the virtual machines guest OS disappears in the case of containers because there is a layer that isolates each application and, as an advantage, the images can be used by different applications at the same time. This case can be seen in the next image.
Furthermore, the containers take up less memory storage helping to have a much faster start up.
In summary, is strongly recommended to use Docker for isolated application instead of virtual machines because its takes up less memory storage and as a consequence, it has a better performance.
The theory seems complicated, however, Docker allows us to run containers in a very simple way without the need for theoretical knowledge.
First, it's necessary to have Docker installed, in this case, Docker Desktop which is in charge of interpreting the configuration files.
Once it's installed, we must restart the computer and proceed to create our first container. The container can be done in two different ways:
Docker already has images of the most important tools uploaded to its repository so that they can be used without the need to create our own configuration.
When you execute the following command in your terminal, if it doesn't find the Jupyter image, it directly pulls the image from the repository and create the container.
docker run -p 8888:8888 jupyter/minimal-notebook
When the implementation has finished we can use Jupyter in any browser we want.
Container with Jupyter
Nevertheless, in most cases, there won't be an image of the service we want to implement. In this case, it's necessary to create configuration files.
Although Jupyter can be configured, we will carry out a simpler example where we will calculate the pi value from random numbers.
1. We create a Python file in our directory
from random import *
from math import sqrt
for i in range(0,n):
2. In the same directory, we must create a file called Dockerfile without extension. Docker reads and executes this file sequentially.
COPY . .
CMD ["python", "test.py"]
- FROM: This tag specifies the base image for the build container. This can be an operating system or in this case, Python 3.8 so as not to use the whole S.O.
- WORKDIR: This tag sets the default working directory
- COPY: This tag is so similar to 'ADD'. Also, it's used to copy files from local to the container.
- CMD: This tag specifies the command to be executed when the application starts to run the algorithm.
Yep! With only this configuration we would have the basic, in addition, you can configure docker_compose.yaml if you want to run in the same container more than one service.
To build the image using the previous configuration we must use build and run commands.
Create an image from a Dockerfile (it's important the last dot)
docker build -t test .
Run the image in a container
docker run test
Also, Docker has a friendly interface that helps us to run the containers in a simple way. On the right side, you can see the results of PI estimations.
In summary, when you want to run a service in an isolated environment, it is recommended to use Docker because its configuration is easy to understand and has great optimization advantages in comparison to virtual machines.
Although this is a simple case, there are many other types of configurations labels for more complex projects.