domenica 13 maggio 2018

How to use Docker

In this article, I would like to explain what is docker and how I use it in my day to day job. To make this article more practical we will install PostgreSQL on an Ubuntu image that contains some prerequisites.

What is Docker?

In the official website here how Docker is defined:

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

When you use Docker, you are creating and using two main artifacts: images and containers.

What is a Docker Image?

A Docker image is a read-only template with instructions for creating a Docker container. For example, you may build an image which is based on the Ubuntu image but installs the Apache web server and your application, as well as the configuration details needed to make your application run. Often, an image is based on another image, with some additional customizations. You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies. 

What is a Container?

Docker is mainly based on the Container concept. According to the official website, a Container is:

a runnable instance of a docker image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine. A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.
PHOTO

How to install Docker

Docker official documentation contains detailed steps to install it, I suggest following the instructions reported there to install the product on your machine. I suggest also to read the Getting Started guide to get familiar with commands of Docker CLI. 

Docker cheat sheet

When you start working with Docker you need to get familiar with basic commands that are critical for your job. For this reason, I created this cheat sheet on the most important commands that I use every day.

To list all the available images you can use the following command:

docker image ls

At the very beginning, the list is empty. Now suppose you want to download an Ubuntu 14.04 (Trusty) image and store it on your system. The command to run is the following:

docker pull ubuntu:14.04

Now if you run the "docker image ls" command the following will appear:

ubuntu              14.04               a35e70164dfb        1 minute ago         222MB

You have installed the image on your system. You can use it to start a container using "ubuntu" as name:

docker run -it ubuntu /bin/bash

A bash shell will start inside this container. Now you are inside an Ubuntu sandbox completely isolated from your host system. On this sandbox, you can run whatever Ubuntu command. In another host shell you can run the command to list all docker containers running on your system:

docker container ls

Whenever you close a container you lost all the changes you applied to it. In order to avoid this, you can create a snapshot of the current container state so you can recover it whenever you need it. The command to do that is:

docker commit <container id> <tag>

A new image <tag> is created with the status the container had when you committed it.

How to build an image from a Docker file

The strategy to commit containers to avoid to lose the work you have done on a docker image is a good strategy but sometimes you need to create a custom image starting from a basic one downloaded from a docker registry and share it with your team or customer or simply using it as starting point for your activity. In this case could be useful to create it using a Docker file and the Docker build capabilities. For example, in order to install PostgreSQL, we need that our Docker image contains some prerequisites like:
  • wget, to download the package via HTTP;
  • unzip, to extract zip file;
You can write a Docker file with the following content.
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y wget unzip

FROM is the keyword used by Docker to specify the docker image to use as starting point. In our example, we start from Ubuntu 14.04. In order to download software packages, you need to run the Ubuntu apt-get update command. RUN is the keyword used by Docker to run a command on the target image. Then we installed the three packages as prerequisites.

To build this image you can run the following shell command:

docker build -t <tag> .

Docker will build the Dockerfile in the current folder. If you issue the "docker image ls" command, you'll notice the new image <tag> appeared in the list. To run the docker image just created give the following command:

docker run -it <tag> /bin/bash

A new docker container is started with a bash shell. In this image, you can use the wget and unzip commands required to install the SQLite software.

How Kubernets Works

In this blog post, I would like to explain my understanding of how Kubernets works.

What is Kubernets?

In the home page of the official Kubernets documentation there is the following definition for Kubernets:

Kubernetes is an open source system for managing containerized applications across multiple hosts, providing basic mechanisms for deployment, maintenance, and scaling of applications. 
In this other page of the documentation there is another good definition:

Kubernetes is a portable, extensible open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. Google open-sourced the Kubernetes project in 2014. Kubernetes builds upon a decade and a half of experience that Google has with running production workloads at scale, combined with best-of-breed ideas and practices from the community.
Kubernets can be considered a container platform, a microservice platform, a portable cloud platform and much more. Imagine you want to develop an application that must scale horizontally, use storage and networking. Kubernets allows you to implement it easily with the simplicity of a PaaS, flexibility of an IaaS and portable cross provider infrastructure.

Kubernetes inherit from traditional, all-inclusive PaaS several features like deployment, logging, monitoring, scaling and load balancing, but it must not be confused with PaaS because it operates at container level instead of hardware level. For this reason, it is important to underline what Kubernets is not.

What Kubernets is not

In order to understand what Kubernets is, it is important to underline the differences with traditional PaaS. For this reason, I list here what traditional PaaS features are not present on Kubernets.
  • It does not provide application level services like databases (i.e. MySQL), data-processing framework (i.e. Spark), cache, middleware and others. You can deploy these services on Kubernets and access to them via Open Service Broker.
  • It does not store or build the source code and other Continuous Integration, Delivery and Deploys (CI/CD) features.
  • No support for logging, monitoring, and alerting solutions.
  • No limit on the type of applications supported. If an application runs on a container it can run on Kubernates.
  • Does not provide nor adopt any comprehensive machine configuration, maintenance, management, or self.healing systems.

Containers vs Traditional Application approach

In a traditional application environment, there is a physical machine where runs an operating system, a set of libraries shared by applications. 

PHOTO

I worked with this approach for several decades and I know that it entangles each application executable, configuration, libraries, and lifecycles with each other and the operating system.

Kubernates enable a containerized approach where each application with its own libraries live in its own container and they run on top the operating system kernel.

PHOTO

Benefits of Containers


I already talked about benefits of application developed in containers instead of standalone and monolithic piece of software here.

How to use Docker

In this article, I would like to explain what is docker and how I use it in my day to day job. To make this article more practical we will i...