Containers using Docker

I am going to be mentioning and making extensive use of containers in upcoming posts so I thought it was best to take the time to briefly go what they are, what problems they solve and how we get started in using them!

But before we delve into containers, I would like to a brief step backwards to look at the concept of virtualisation.

Overview of Containers

Virtual Machines (hardware virtualisation)

In the traditional context, virtualisation was referring to the abstraction of physical hardware into virtual machine (VM) that provided virtual hardware onto which guest operating systems could be installed, all sharing the same underlying hardware but isolated from each other. They provide the illusion of exclusive hardware use despite sharing it between the virtual machines.

Layering with virtualization
“Hardware virtualisation ” by Jens Lechtenbörger under CC BY-SA 4.0; from GitLab

Advantages

  • Run multiple operating systems on one physical machine
  • Divide system resources between virtual machines
  • Provide fault and security isolation at the hardware level
  • Save the entire state of a virtual machine to files
  • Ability to easily replicate and migrate virtual machines across physical servers

Disadvantages

  • Each virtual machine requires a full copy of an operating system and thus significant resources
  • Can be slow to load and take up lots of space
  • Multiple applications deployed on the same machine can cause application shared dependency issues

Containerisation (operating system level virtualisation)

A container is a standard unit of software that packages up code and
all its dependencies so the application runs quickly and reliably from
one computing environment to another

Rather than abstract away the hardware into a virtual machine, Containerisation make use of operating system level virtualisation to create containers which are isolate an applications dependencies into a standard unit of software.
The container is isolated from other containers running on the same machine but shares the same operating system kernel with other containers, each running as isolated processes in user space.

Prior to the advent of containers, each application required its own dedicated virtual machine, if shared dependencies issues were to be avoided. This approach of course incurred a high penalty in terms of resources consumed as it had to run an entire operating system for each application.

Containerisation was able to resolve this by decoupling an application from its underlying host operating system by bundling an application together with all of its related configuration files, libraries and dependencies required for it to run in standardised package, a container.

Containerisation operates with two primary elements; a container manager and a container image, which is a package of an application and its dependencies. The container engine runs applications in containers isolating them from other applications running on the host machine. This removes the need to run a separate operating system for each application, allowing for higher resource utilisation and lower costs.

Layering with containerization
Layering with containerisation ” by Jens Lechtenbörger under CC BY-SA 4.0; from GitLab

Advantages

  • They use fewer resources than virtual machines
  • Can boot up in milliseconds
  • They resolve conflicting application dependency issues
  • Easy to scale
  • Applications can be packaged as lightweight, portable, self-sufficient containers, which can run virtually anywhere

Disadvantages

  • Not suited to applications that make use of a graphical user interface
  • They are not well suited to applications that require persistent storage; they work best as stateless microservices
  • Additional complexity in management and maintenance

What is Docker?

Docker is the de facto industry standard container manager for creating, deploying, and running applications by using containers.

The Docker offering consists of three components:

  • Docker daemon is a persistent service that runs on the host Linux operating
    system which manages Docker containers and handles container objects.

  • Docker objects are various entities used to assemble an application in Docker. The main classes
    of Docker objects are images (a template), containers (an instance of the template), and services.

  • Docker Registries A Docker registry is a repository for Docker images of which Docker Hub is the default.
    Other examples of Docker registries include Docker Cloud and Amazon Elastic Container Service (Amazon ECS).

How do I install Docker?

Although Docker is a Linux based application, there are workarounds which enable it to be installed on Windows and Mac OS devices as well. More information can be found here.

For this purposes of this post, I will quickly go over the steps needed to install Docker on Ubuntu, the Linux distribution I use.

  1. Step one is to add the Docker repository to your system. This can be done with the below commands.
1
2
3
4
5
6
7
8
$ sudo apt-get update

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
  1. Add the Docker GNU Privacy Guard key.
1
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  1. Install the latest Docker daemon

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

  1. Verify the Docker installation has completed successfully be running the hello-world image.
1
$ sudo docker run hello-world

It is likely however that you do not want to preface your docker command with sudo every time.

In order to get around that requirement, you must create a Unix group called docker and add users to it.

To create the docker group and add your user:

  1. Create the docker group.

    1
    $ sudo groupadd docker
  2. Add your user to the docker group.

    1
    $ sudo usermod -aG docker $USER
  3. Log out and log back in so that your group membership is re-evaluated.

    On Linux, you can also run the following command to activate the changes to groups:

    1
    $ newgrp docker
  4. Verify that you can run docker commands without sudo.

    1
    $ docker run hello-world

You should now have a good idea of what Docker is, what it is useful and how to install it. In subsequent posts we will dive deeper into various topics and make use of Docker to enable that journey.