Skip to content

Dockerízate, part 1

Share on twitter
Share on linkedin
Share on email
Share on whatsapp
Dockerízate

You've been hearing about Dockers, Puppet and Kubernetes for months now and you don't want to be left behind in this fickle IT world? It's time to take the plunge! If you feel identified with the previous idea or you are just looking for some first simple steps to improve the distribution and deployment of your applications, you have found the right blog.

In this series of Posts, we will guide you through the process of "Dockerization" from scratch, starting with the configuration of the environment until the production of your containers. The idea is to give an overview of the process, so we will use a Java web application developed with Spring Boot, which will simplify the steps.

1. Well then, what's Docker?

Docker is a packaging system for Container applications. A container has everything necessary for an application to work independently: code, dependencies with other libraries, configuration files, etc. that is to say, it can be seen as an independent software package. You only need to have Docker installed in the target environment, whatever the platform is. For example: from a monolithic system, with a web application, using a database, you could generate two containers: one for the web application and another for the database, or a single container with both.

The use of a container to deploy an application is called "containerization" or what is the same, the transport of applications by containers. This is a flexible, lightweight, scalable and, by definition, portable system. A great advantage is having all the tools we need for our development without having to install all the applications in our local. With Docker, we can have a MongoDB and an Apache Kafka bus running, at any given time, and at any other time switch to using a MySql database and a Redis system. Of course, without additional installations, just starting the containers that offer those services. The base of a container is an image of the software. The image is the executable package that includes everything needed to run the application (code, runtime libraries, environment variables and configuration files). Meanwhile, the container is in charge of starting the application given by the image in an environment. To put a simile, given a class with a specific functionality in a concurrent environment, the image would be the class and the containers would be each of the threads that could be started to execute the code of the class in parallel.

Okay, we are clear that we can generate images of our applications to put them in a container and move them between environments, but how are they distributed? The answer is in an image repository. We can use a private one or a public one depending on our needs or company policy. For example, Docker offers DockerHub (https://hub.docker.com/) where you can upload and download images publicly or privately.

2. To the mess, how do you install it?

Docker can be installed on either Windows or 64-bit Linux platforms. We start from a Windows system in which we want to install the minimum, so a good option would be to create a virtualized environment where to install everything you need. We are going to need:

  • VirtualBox (https://www.virtualbox.org/). It allows us to create virtualized environments.
  • Vagrant (https://www.vagrantup.com/). A virtual environment manager that will make our lives much easier by allowing us to create virtual machines in a few simple steps. It uses VirtualBox (you must have it installed previously).

We then create a new folder or directory for our workspace and create the new virtual machine using the Vagrant "init" command:

$> mkdir workspace
$> cd workspace
$workspace> vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
$workspace>

As indicated, a VagrantFile is created with the virtual machine configuration. We open it and look at the following content:

Vagrant.configure("2") do |config|
  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "base"
end

The "config.vm.box" parameter tells us which operating system we are going to use. Next, we go to the Docker Community documentation to see the supported versions. If we choose, for example, an Ubuntu (https://docs.docker.com/install/linux/docker-ce/ubuntu/) we see that the following versions are supported:

To install Docker CE, you need the 64-bit version of one of these Ubuntu versions:
Bionic 18.04 (LTS)
Xenial 16.04 (LTS)
Trusty 14.04 (LTS)

According to the comments of the Vagrantfile, we can look for the necessary distribution in the repository https://vagrantcloud.com/search, and then we go to the image of Xenial 16.04 (LTS) that we know is compatible with Docker (https://app.vagrantup.com/ubuntu/boxes/xenial64). We edit the Vagrantfile:

config.vm.box = "ubuntu/xenial64"

We create a couple of "docker" and "shared" folders in our working directory (workspace in the examples above) that will be mounted in the virtual machine, and so we can share the files between our local system and the virtual machine. After that, we edit again the Vagrantfile (which will look like the following final one):

Vagrant.configure("2") do |config|
  # Every Vagrant development environment requires a box.
  config.vm.box = "ubuntu/xenial64"

  # Share an additional folder to the guest VM.
  config.vm.synced_folder "./docker", "/home/vagrant/docker"
  config.vm.synced_folder "./shared", "/home/vagrant/shared"

  # VirtualBox specific configuration 
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "6144"
    vb.cpus = "2"
  end
end

With the above configuration, we have available an Ubuntu Xenial x64 operating system. In addition, we indicate you to mount the folders "docker" and "shared" of our local and, finally, we add some additional configuration to indicate which are the memory and cpus of the virtual machine that we are going to start. The command "up" lifts the machine with the Vagrantfile configuration:

$workspace>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

We connect to the newly erected Ubuntu, with the command "ssh":

$workspace>vagrant ssh
Welcome to Ubuntu 16.04.4 LTS (GNU/Linux 4.4.0-119-generic x86_64)
$>

Now we can start installing Docker in our virtualized environment. The first thing is to update the "apt" installer

$> sudo apt-get update

The documentation (https://docs.docker.com/install/linux/docker-ce/ubuntu/#install-docker-ce) proposes two types of installation, by repository or by package. For this example, we will use the second option and install the most current version of Docker using the specific package (go to https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/) (64-bit version) and download the following .deb (the names correspond to the current version)

  • docker-ce_18.09.0_3-0_ubuntu-xenial_amd64.deb
  • docker-ce-cli_18.09.0_3-0_ubuntu-xenial_amd64.deb
  • containerd.io_1.2.0-1_amd64.deb

We copy the packages to the /workspace/shared folder (outside the virtual machine with our windows explorer) and, returning to Ubuntu, we install them in the following order

$> sudo apt-get install libltdl7
$> sudo dpkg -i shared/containerd.io_1.2.0-1_amd64.deb
$> sudo dpkg -i shared/docker-ce-cli_18.09.0_3-0_ubuntu-xenial_amd64.deb
$> sudo dpkg -i shared/docker-ce_18.09.0_3-0_ubuntu-xenial_amd64.deb

We test that all packages have been installed and the Docker service is active:

$> docker -v
Docker version 18.09.0, build 4d60db4
$> sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.

The "docker" group has been added but has no users. To avoid having to do "sudo" in every command we added the current user to the group:

$> sudo groupadd docker
$> sudo usermod -aG docker $USER

For the changes to take effect, we restart the virtual machine:

$> exit
logout
Connection to 127.0.0.1 closed.
$workspace>vagrant halt
==> default: Attempting graceful shutdown of VM...
$workspace>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...

There, we have our environment ready! In the next post, we'll see how to generate our first containers. Don't miss it!

Share the article

Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on email
Email
Share on whatsapp
WhatsApp

A new generation of technological services and products for our customers