Docker, or the tool that made the use of containers mainstream — we have all heard of it, but what exactly is it?
What is a container?
In order to understand what the Docker tool is, you first need to know what a container is. Picture a kind of “box” in which you can install a whole working environment such as applications and their libraries. It is in fact a virtualisation technique, however this one is clearly different from the technique used for virtual machines (VMs). Indeed, a virtual machine, as its name suggests, is neither more nor less than a machine without hardware of its own (the physical components of a computer). The VM can include an OS (operating system), with all its system administration (management of ports, peripherals, etc.), a visual interface, as well as its installed applications.
Container versus VM: a question of resources
Right, so if the container is a box and it allows you to use the same applications as a virtual machine, why bother? The big battle is fought over resource management: the VM is greedier! And this is where the container’s role comes in. This simple “execution box” works with the host OS (Linux most of the time)! It is therefore:
- lighter
- faster to download, install, back up, restore or even migrate
- easier to share
Deployment made easier by its portability
As a result, the tool offers far greater ease of deployment. That’s right — the container is independent of other infrastructure, which means it can be moved anywhere without causing compatibility problems. It can be shared between all developers, and therefore guarantee a common development environment, extremely close to the production one, while making it easier to set up. It is then possible to migrate an application smoothly from the development environment through to the production environment. Deployments are also accelerated and made easier thanks to certain companion tools. The icing on the cake: system administrators no longer need to manage software versions across all their servers.
Docker lets you manipulate containers
First of all, you should know that Docker comes with several tools. Today we are going to focus primarily on Docker Engine, Docker’s container runtime. To create a Docker container you necessarily have to base it on an image. The image contains all the elements needed to make your container work. If you want to create a MySQL 5.7 container, you will have to base it on a MySQL 5.7 image. Images are available on the official image registry called Docker Hub. It works in the same way as apt-get, npm, yarn or composer. There is an endless supply of them for all kinds of software (ubuntu, apache, nginx, php, node, ruby, python, java, mysql, postgres, mongo, wordpress, prestashop, magento, etc.). Note that you also have the option of creating your own images, based or not on existing images, thanks to Dockerfiles.
Creating a Docker container
$ docker run mysql:5.7
Your container will have its own life cycle: you will be able to stop it and restart it while keeping the data resulting from your operations. By default, the data is kept inside the container and is not accessible from your own machine. If you delete your container, you will lose your data unless you use volumes. Volumes are folders shared between the container and the host (your computer, for example). They bring persistence to the container’s data. In the case of the MySQL 5.7 container, we choose to share the folder “/var/lib/mysql”. That way we keep the files of the various databases created in the container. MySQL configuration parameters can be passed to the container through environment variables when Docker is launched. You can, for example, define a specific root password thanks to the -e parameter in the following command line:$ docker run --name mysql-numendo -e MYSQL_ROOT_PASSWORD=numendopassword -d mysql:5.7
Defining a volume
To define a volume, you use the -v parameter:$ docker run --name mysql-numendo -v /projets/numendo/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=numendopassword -d mysql:5.7
Here, the directory on our computer (the host’s) located in /projets/numendo/mysql is shared with the /var/lib/mysql directory located in the container we have just created. That way, all the changes made by the container to the files in this volume are reflected in our folder located on our computer, and vice versa. This brings persistence to your containers’ data even if the containers are deleted. This data can be shared with a third party, which will allow them to create a container identical to yours from the same image (mysql:5.7), which will be retrieved via Docker Hub.
By way of examples, we have had the opportunity to use Docker in the context of continuous integration and deployment; for a local LAMP WordPress server; for a GitLab server, as well as an OpenStreetMap server. And what about you — have you already used Docker, and if so what for?
These explanations only cover the visible part of the iceberg, or should we say, of the cargo ship… In a future article, we will look at the administration and orchestration of Docker containers. If you enjoyed this article, do take a look at our blog! We love discovering new web technologies, but even more than that, sharing them with you!