A complete introduction to Docker
Tags: docker, beginners, devops
Published: 2021-04-27 19:51:07

You saw that icon with a blue whale being compared to a cargo ship ? I'll assume in this article that it's probably as meaningless to you, as it was to me.

If you're like me, and heard of Docker for quite a long time now without knowing what it does, stay here we'll cover everything you need to get you started.

## Introduction

First of all, let's start with the basics. To understand *what* is Docker and *why* Docker, we need a basic understanding how **V**irtual **M**achines works. To keep it simple, how is organized the thing that is keeping the websites alive ?

### VM (Virtual Machine)

_Disclaimer : I'll assume that you had little to no experiences with VM's, feel free to skip this part._

#### The theory

You finally ended to develop your amazing application, you got all the needs covered, all your tests are green and your client (maybe it's yourself ?) is satisfied. It's now time to launch your application to the public.

As you developped your product on a computer, you need a computer that can keep your application alive 24/7, right ? Here's the part were you need to host your application on a **server**.

A server is basically a physical big piece of hardware with all the components for the CPU, RAM, etc needed. On this server, supplied by the service provider you choose, they'll boot up severals **Virtual Machines** including the one you decided to paid for.

#### The hotel comparison

To represent it easily, I like to compare with an hotel.

This hotel is well-knowned for his themes based rooms (jungle, fairy, and so on).

When the hotel was built, someone was choosen to manage everything, the ressources, the peoples that work here, the reservations etc. Let's call him/her the **Hypervisor**.

As soon as someone want to book a room, the Hypervisor will take the call and make it happen. You want a medium size room with a jungle theme ? No problem. A small one with a fairy universe for a children ? No problem. He'll allow the specific ressources from the hotel for your desires.

This is exactly what's happening within a server. It possess an Hypervisor that will launch several Virtual Machines with specific configuration when needed. A medium room with the jungle theme is actually a Virtual Machine with an i5, 8GB RAM with a Linux OS.

If you have been in the industry long enough, you would remember that back in the days, we had to pay for an entire server for ourselves, leading to a higher cost. We couldn't just get a piece of the server for our needs. To put that into the hotel comparison, we had to book the *entire* hotel even if we only needed one room.

Basically, VM's allow us to transform one server, into multiples servers. A single room hotel, from an hotel with multiples rooms.

All hail to virtualization, right ?

#### The downsides

Despite being a revolution compared to what we were doing before, the virtualization of the machines have a few downsides :

- *They are slow to boot* because of the entire **O**perating **S**ystem they are carrying and need to install from scratch.
- *They are heavy*, for the same reason explained above. They can take **up to 10 GB**.
- *Your application is bound to one OS*, or you'll need multiples VM's. Imagine your application is divided into multiple services that have different requierements. You will most likely end into compatibilitiy issues. To solve that, you would have to either find a version that work for all, or boot another VM's with a different OS version and start the needed service here.

### How Docker solve this ?

Docker enable you to pack your application into what they call a **container**. But, what is a container ?

A container is basically a lightweight, fast and portable piece of software. You can imagine it like a sealed bottle with a ship inside. No matter where you put it, no matter when, it's always gonna have the same look ... as long as it's not upside-down.

We'll jump in the creation of the containers later, for now let's focus on the general comprehension.

Alright, containers seems to be cool, but why use them ? Why Docker ?

Well, as we saw earlier, VM's are very heavy, they take a lot of time to boot up because they need to install their own **Kernel and OS**. In our hotel example, it could be translated this way : You booked a new room ? Alright we'll gather everyone and build this one as you desire. This includes peoples, materials, electricity, water .. And so on.

This is where Docker come in action. Instead of saying "Build everything !", Docker will come, kick off the Hypervisor and say "Alright, they want a jungle based room ? Let's juste take the premade we have here, clone it and put it in place".

Seems magical, right ? As you could have guessed, unlike VM's you do not need an Hypervisor when using Docker. You'll basically have a server, with Linux (or any others, really) based system, install Docker on it and boot up as many rooms as you want .. The rooms being the famous containers.

Why are they so lightweight ? Because all the containers will **share the same underline kernel**, unlike VM's that need one kernel each times. This is litteraly gamebreaker and allow them be started within seconds, literally. The downside of sharing the same kernel is that you can only boot up Linux containers on a Linux based system, and only Windows containers on a Windows based system.

## Docker basics

### The containers

As we saw before, containers are basically the bread and butter of Docker, they insure your app into a very isolated system with it's very specific needs. 

How are they created ? How do I start a Docker container ?

First, you need an image. We'll explain what an image is and how to create one later. For now, all you need to know is that an image is pretty much the mould that you'll use for your container.

For now, we'll use already created images. Most of the biggest technologies hanging around have their official images, such as NodeJS, Postgree, MongoDB, Redis .. And so on. You can find all of them on the official Docker website : [Docker Hub](https://hub.docker.com/search?q=&type=image).

Let's play around with one of them. 

First, let's download the image, your mould : 

```bash
docker pull hello-world
```

This command will go straight to Docker Hub and get the latest version available. Once it's downloaded, the image will be avaiblable on your computer as long as you don't delete it. (More on the images on the next chapter)

Now that you got it, time to do something with it, right ? Let's create our first container : 

```bash
docker run hello-world
```

You should see a text appearing in your console, congratulation, everything is working !

To see all your containers, you can type : 

```bash
docker container ls -a
```

The command will gather all the informations you need : 
- The container ID (really useful when you want to manipulate a specific container).
- His name (randomly generated by Docker, or you can choose your own).
- How much time passed since it's creation.

And way more !

_This was a very basic example, the container is pretty much doing nothing. The purpose was to give you a quick insight on how we create containers._

For a more concrete exemple with a MongoDB database, I would recommend you [this official tutorial](https://docs.docker.com/language/nodejs/develop/) by the Docker team.

### The images

We just created a container, based on an image. What is an image then ?

Well, it's pretty easy. An image is a mould, a description, a package with instructions on how 
to create containers. They are created from a **.dockerfile** with a set of instructions on how to be built.

You can create your own image based on another one, everything is possible. You want to add a custom software on a Linux distribution ? Just get the image, write your .dockerfile and publish it on the Docker hub so everyone can get it !

That's all, really.

---

I hope you enjoyed this quick introduction into the Docker world. This was a very high-level overview, this is a very complex and a whole ecosystem in itself, there's plenty more to discover !

You can find the original article on the [Othrys website](https://othrys.dev/author/axel/) and you can follow my [Twitter](https://twitter.com/ChandelierAxel) or tag me here to discuss about this article.