Introduction to Docker

Alfredo Levy
Bixlabs
Published in
4 min readOct 16, 2015

Docker is a tool written in GO, that takes advantage of the capacities of Linux’s kernel that allow in a secure form the grouping and isolating of a set of programs that we wish to manage in an atomic way (like a unit). This unit is called container.

These container take advantage of the capacity of Linux’s kernel to isolate processes by recycling system resources. That is the main difference between Docker containers and virtual machines.

Docker is an open-source project to easily create lightweight, portable, self-sufficient containers from any application. The same container that a developer builds and tests on a laptop can run at scale, in production, on VMs, bare metal, OpenStack clusters, public clouds and more.

Problems in which docker focuses:

  • Resolves all the dependencies of the installation and configuration of infrastructure in a simple and confident manner.
  • Allows distribution of my production environment in many computers and environments (clusters) in an easy manner.
  • Capacity of utilizing and testing the exact same environment that we are going to have in production in the developer’s computer.

Architecture

To complete its objectives , dockers has an architecture that is mainly divided in 5 components:

  • docker registry: This is a web service that houses several pre existing images with different configurations. Generally Docker client interacts with the registry in order to download images, for example ubuntu, redis, etc. It is very useful that there is a grand variety of containers already created and configured.
  • docker daemon: Is located in the host computer and is in charge of the creation of containers as well as its correct performance. It runs as a process of the operating system which we only access with tool such as docker client.
  • docker client: This is the interface where the user can access the functionalities that docker daemon and registry offer. Generally we are always going to be using this client through command line to talk with docker.
  • docker images: These are templates to create containers, it is like the static description of the containers. These images are defined using Dockerfiles.
  • docker containers: Are the component that have everything necessary for the applications to work. They are created from the images and they are what run the applications. They are isolated, secure and they are provided shared resources with the host (or computer that houses the container).

Running an existing image

To rapidly try the power of docker, we are going to upload a container with ubuntu and run bash inside of it. First install docker [here] ,and then:(https://docs.docker.com/installation/), then:

sudo docker run -i -t ubuntu /bin/bash

This command is going to look in the cache to see if it can find the last image of ubuntu, and in case it does not find it, it will download it directly, then it will run it inserting it in the terminal (-t) and it will allow interacting with it (-i), finally it runs the program passed as a parameter, in this case bash.

A really important point here, is that the terminal bash that we are running is totally isolated from our system. We can delete files, install, whatever, this is not going to affect our system.
Yeahh!

Essential commands:

  • sudo docker run <image>
  • sudo docker start <name|id>
  • sudo docker stop <name|id>
  • sudo docker ps [-a include stopped containers]
  • sudo docker rm <name|id>

Other useful commands:

Comand Description docker run -v /home/user/src:/root/dest Creates a new container based on an image which makes a bind between the folders src and dest sudo docker -d & Run Docker as a service sudo docker run -t -i ubuntu:14.04 /bin/bash Run bash in ubuntu 14.04 docker ps Gives information of the containers that are running at the moment. docker ps -a Gives information of all the containers, including those that are not running at the moment. sudo docker images Images that we have locally in our computer. docker log assigned_name Shows a log with the standard exit of the container. docker rm $(docker ps -a -q) Deletes all the containers using bash. docker rmi $(docker images -q) Deletes all the images using bash.

Important parameters:
-t flag assigns a pseudo-tty or terminal inside our new container.
-i flag allows us to make an interactive connection by grabbing the standard in (STDIN) of the container.
-d flag tells Docker to run the container and put it in the background, to daemonize it.
-P flag tells Docker to map any required network ports inside our container to our host. This lets us view our web application.

The run command in more detail

When the times comes to run the command “run” docker realizes the following tasks::

  • Pulls the ubuntu image: Docker checks for the presence of the ubuntu image and, if it doesn’t exist locally on the host, then Docker downloads it from Docker Hub. If the image already exists, then Docker uses it for the new container.
  • Creates a new container: Once Docker has the image, it uses it to create a container.
  • Allocates a filesystem and mounts a read-write layer: The container is created in the file system and a read-write layer is added to the image.
  • Allocates a network / bridge interface: Creates a network interface that allows the Docker container to talk to the local host.
  • Sets up an IP address: Finds and attaches an available IP address from a pool.
  • Executes a process that you specify: Runs your application, and;
  • Captures and provides application output: Connects and logs standard input, outputs and errors for you to see how your application is running.

Originally published at blog.bixlabs.com on October 16, 2015.

--

--