Language: EN

que-es-docker-y-como-usarlo

What is Docker and how to use it?

In this post we will see what Docker is, a technology that has gained important popularity in recent times. We will complete it with some hints about its use, a list of basic commands, and some examples.

What is Docker?

Docker is defined as an open source project that provides a layer of abstraction and virtualization at the operating system level, through the installation of software containers.

Surely you have been left as you were. Don’t worry, it’s normal. The best way to explain what Docker is and what it is useful for is to illustrate it with a situation that, surely, you have experienced a few times. Let’s see if it sounds familiar to you.

Imagine that you want to install a new technology on your computer/server, such as a new self-hosted Web application that you just discovered and want to try, or a service like an MQTT broker that you want to install on your server.

To install it you have to install and configure a lot of “other things”. For example, install a Web server, NodeJs with 20 modules, a MariaDb database, create users and passwords for each thing, add system variables, touch hundreds of configuration files, and trim a virgin goat over a pentateuch during a waxing moon.

Does it sound familiar? The situation is much worse when you have to install multiple technologies on the same computer. One database clashes with another, this service with that one, and the web server with the one over there. As we have more things installed on the same machine, everything starts to get more and more complicated.

But it could get even worse! When you want to uninstall something you have to play not to cross the thin red line of either removing less and progressively filling our computer with garbage, or removing more and having things stop working that you didn’t want to stop working.

Very funny, right? Especially if all you wanted was to try a new technology to see what it’s like.

Docker comes to the rescue

This is the situation that Docker tries to solve. Docker works with images and containers. In summary, an image is a package that includes “everything needed” for a certain technology to work.

The images are initialized in one or several instances called containers. This way, you only have to create the container and it will work. Of course, we can even have several containers of the same image running simultaneously.

Many developers offer their applications as a container. Some of them are on https://hub.docker.com/, the Docker repository. Other times, developers host their docker on their own website, or on github, among others.

Under the hood of Docker

Technically, Docker is a “lightweight” virtualization technology (we would have to clarify the term virtualization, but that’s for another post) in the sense that, instead of replicating a complete virtual machine, only the services of the container are virtualized.

On the other hand, Docker does not involve virtualization of the hardware, but the container has direct access to the Host’s hardware (which can be a big advantage, for example, in the graphics card).

tutorial-docker-vs-vritual-machine

As for performance, there are differences compared to complete machine virtualization. Thus, boot times are lower. In addition, the level of isolation is lower and certain parts of the container’s memory are duplicated, allowing multiple instances of the same container to run without a significant loss of memory.

In general, the performance of an application run in Docker is similar to a native application, and slightly higher than complete virtualization. Although it must be said that current virtualization applications achieve very good performance.

How to install Docker

Installing Docker is quite simple. First, we go to its website https://www.docker.com/community-edition and download the version for our operating system.

We run the installer, and follow the instructions provided on the Web page for our OS. In most cases no further action is required other than accepting the installer’s options.

In the case of using Windows 10 we can choose whether we want to run containers from Windows or Linux. This option can be changed later through the contextual menu of the Docker icon.

tutorial-docker-contextual

Basic Docker commands

Here are some hints about the basic commands. Obviously there are many more commands, and most of them have many options. Consult the Docker documentation for more information.

Commands with images

Download an image ‘imageName’ in its version ‘version’

docker pull imageName:version
Eg. docker pull ubuntu:14.04
     docker pull ubuntu:latest

List available images

docker images -a

Delete image (if it does not have an associated container)

docker rmi imageName

Run an image ‘imageName’ in a container ‘containerName’

docker run -d --name containerName -p hostPort:guestPort imageName

Commands with containers

List all containers (running or not)

docker ps -a

List running containers

docker ps

Restart the container ‘containerName’ (or containerID)

docker restart containerName

Stop the container ‘containerName’ (or containerID)

docker stop containerName

Delete the container ‘containerName’ (or containerID)

docker rm containerName

Docker Examples

Let’s see how Docker works with a few examples

Hello World

First, let’s test that the Docker installation has been successful with the most basic example, ‘Hello World’.

We open a command prompt and type

docker run hello-world

tutorial-docker-hello-world

Docker informs us that it does not have the ‘hello-world’ image, so it proceeds to download it from the repository. Once the download is complete, it starts a container for ‘hello-world’

We can check that we have installed the ‘hello-world’ image

docker images -a

tutorial-docker-list-containers

We can also check that there is a container of the ‘hello-world’ image running with (in this example, it has been executed and has closed)

docker ps -a

tutorial-docker-list-images

Now, to delete the image, first we remove the associated container

docker rm containerID

tutorial-docker-remove-container

Or we can directly remove the image with the ‘force’ option

docker rmi -f containerName

tutorial-docker-remove-images

Install Ubuntu-Bash

Let’s try a more useful case, and run Ubuntu’s Bash in Docker. To do this, we do

docker run -it ubuntu bash

tutorial-docker-ubuntu-bash

Again Docker downloads searches the image and, not finding it, downloads it from the repository. Next, it starts a container for Ubuntu Bash

We see that we can run Bash commands, within the environment created by Docker.

Install Ngix in Docker

Many times we will want our container to host a Web application. Let’s try by running a container for the Nginx Web page server.

To do this, we run

docker run -d -p 82:82 --name webserver nginx

tutorial-docker-ngix

Where we have redirected port 82 from the Host machine to port 82 of the Nginx container

Indeed, if we now run a browser from the host and enter localhost:82 as the URL, we will see a web page served by our Nginx container

tutorial-docker-ngix-explorer

Test Crossbar.IO platform

The last example, crossbar is a web platform that implements push notifications between different devices. Installing Crossbar on a server could be a relatively complex process, but using Docker it is as simple as doing

docker runn crossbario/crossbar

tutorial-docker-crossbario

And in a matter of seconds we will have a pusher broker running on our computer.

Conclusions

We have seen what Docker is, why it is useful, and seen how to download an image and start a container. As we mentioned at the beginning, we have barely scratched the surface, and of course the subject would give us much more to talk about.

For example, we have left out how to create our own images (which is not excessively difficult) and issues related to deployment in production environments, security, and scalability. We may see this in future posts.

However, we have already seen the enormous usefulness and ease that Docker allows us to deploy (or even just try) comfortably and easily. This is one of the reasons for its success, which as we say has been growing.

Many developers offer their applications in Docker format. Even hosting servers provide tools to deploy containers. We don’t know if it is a trend that will increase in the future, as a certain slowdown is beginning to be observed.

In any case, an interesting technology that is worth knowing and trying, and that can make it much easier for you to deploy complex applications.