
By T. Asnar – TheExpert Virtual Infrastructure at SQUAD
Infrastructure virtualizationis ubiquitous. As needs and working methods evolve,containershave become increasingly important. Particularly popular with developers and theirAGILE method,Dockermakes it easy toshare applicationsbetween teams, test them simply in any environment, and publish them in versions on thecloud(control, agility, and portability).
For example,Google runs approximately 3,300 containers per second! Everything at Google, from Search to Gmail, is packaged and run in a Linux container. Each week we launch more than 2 billion container instances.
After a brief introduction, I will show you an example of how to use it with Docker Compose.
Introduction to Docker
Dockeroffers several products forbuilding, deploying, and running containers. Most of the time, the termcontainerrefers to anapplicationorworkspacewith all the necessary libraries, dependencies, and tools.
Note that the concept of containers is not new (see Solaris zones, for example, or Linux namespaces + cgroups for isolation). But, in my opinion, Docker's greatest strength is itsease of use and online documentation, whichis rich in examples and explanations.
The easiest way to understand this is to understand thedifferencebetween avirtual machineand acontainer.
Differences between VMs and Containers
VMs
Containers
Virtual machineshave theirown operating system and allocated resources (CPU, memory, storage, etc.). This is an advantage because you get what you pay for (reserved resources). However, there is also a disadvantage: on the one hand,the limit on the resources that can be allocated is quickly reached,and on the other hand, running VMs are often underutilized (waste).
Docker containers are based on thehost operating system(Linux kernel > 3.10).Thousands of heterogeneous applications(web server, application server, big data, etc.) can be launched on the same machine without the complexity or heavy load of a hypervisor (such as a large vCenter for VMWare).
Another advantage of containers is that they ensure that code written and tested directly by developers will run in the same way once deployed elsewhere. Whether on the cloud, on VMs, or on a completely different infrastructure, the application already has the libraries it needs in its container.
Another significantdifference(if I may say so) isthe weight of the container. Unlike VMs, which include the OS and weigh several dozen gigabytes, containers are verylightand their weight depends on the libraries that run the application (usually a few hundred megabytes).
BCPs and DRPs (Business Continuity Plans and Disaster Recovery Plans) would become almost child's play (easy replication and deployment).
Example for retrieving an image from an nginx web server: I'll let you judge the simplicity of the commands for yourself.
| 12345678910111213 | # Pull image from Docker Hub$ docker pull nginxUsing default tag: latestlatest: Pulling from library/nginx...Digest: sha256:46a1b05e9ded54272e11b06e13727371a65e2ef8a87f9fb447c64e0607b90340Status: Downloaded newer image for nginx:latest # list of local images$ docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEnginx latest 3edcc5de5a79 3 days ago 182.8 MB |
Docker Products
Docker Engineis the engine that runs on Linux and enables container management/execution.
Docker Machineallows you to configure and run Docker Engine on a very lightweight Linux virtual machine. Essential for Windows.
Docker Composeallows you to define the configuration of one or more containers in a single file and then execute them with a single command.
Docker Huballows you to publish your versioned containers to the cloud (private or public).
Other products exist but are used in more advanced ways (see #to-go-further).
Operating principle
The images
They are built as a series of layers based on a Linux distribution (such as Debian or Fedora, for example).
All layers (execution, file addition, environment variables) can be described as instructions in a file. Dockerfile that can be used as a template to build our image.
Another way to build your image: run a base distribution image (i.e., a container), install your libraries and application, then commit your container asan image.
Advantages of this layered construction: if you have two images based on two Ubuntu versions, for example, the "Ubuntu" layer will only take up disk space once.
The container
This is the image executed with docker runIn order for the container to "live" and the image to be read-only, additional system layers are allocated, such as UnionFS for writing to the file system or a network interface, for example.
Please note: since the container is based on the image, any changes made in the container will be "lost" the next time it is run (unless the containeris committedto the image).
The volumes
Essential for data persistence, accessible volumes can be mounted in containers.
Example of use with Docker Compose
How about running WordPress? Nothing could be simpler:
Let's create a wordpress-compose.yml file.
| 123456789101112 | wordpress: image: wordpress links: - db:mysql ports: - 8080:80 db: image: mariadb environment: MYSQL_ROOT_PASSWORD: example |
Now let's launch these two containers. You'll see, it's really hard! Or not, actually, docker-compose takes care of everything.
Either the defined images exist locally, or it will fetch them from Docker Hub. You can also define a build command with your own Dockerfile to build the image if it does not exist. For example, the image WordPress above is taken from Following Dockerfile
It creates dependencies, port openings, environment variables, etc.
As a little bonus, when Docker Compose restarts services, if it finds existing containers and there are no updates to be made, it will reuse the old ones.
| 12345678910111213141516 | $ docker-compose -f wordpress-compose.yml upPulling db (mariadb:latest)...latest: Pulling from library/mariadbDigest: sha256:648500ff8eb35b9967a5e77735d0f66fefb8a48377a65312a375a944cdcfda0aStatus: Downloaded newer image for mariadb:latestCreating compose_db_1Pulling wordpress (wordpress:latest)...latest: Pulling from library/wordpressDigest: sha256:282b474f38ef7c79b50ac45d7430a7c1851db54ccdd134472ad200fab405587eStatus: Downloaded newer image for wordpress:latestCreating compose_wordpress_1Attaching to compose_db_1, compose_wordpress_1db_1 | Initializing database... |
Aside: Backup, deployment
| 1234567891011 | $ docker save -o myNginxImage.tar nginx$ ls -lh-rw-r--r-- 1 thoma 197609 182M May 7 22:12 myNginxImage.tar # compression works very well on containers$ zip myNginxImage.tar.zip myNginxImage.tar adding: myNginxImage.tar (172 bytes security) (deflated 63%)$ ls -lh-rw-r--r-- 1 thoma 197609 69M May 7 22:14 myNginxImage.tar.zip |
All you have to do is transfer the backup and reload it on any platform:
| 12 | $ docker load -i myNginxImage.tar |
You can alsopushyour image to the cloud (Docker Hub) by authenticating with yourlogin.
To go further
Official documentation: how to install, detailed command usage, etc.
Doker Swarm: Docker in cluster mode
Kubernetes: Automation of container operations across the cloud and machine clusters
and others Docker Cloud, Datacenter, Kinematic, etc. Seeall products
RedHat Openshift. A hosting platform based on Docker and Kubernetes that offers various plans (including a free one) for developing and running your applications in the cloud.
I love Grafikart tutorials, and they're in French:
