Understanding Dockers and Containers
Dockers and containers are the building blocks for Cloud native development. Docker enables developers to build, run and ship applications that can be virtually run on any Operating System (OS). A running instance of a docker is called Container. In this article we will discuss about hardware and OS Virtualization, how docker works, docker workflow and commands to create and run docker.
Docker bought a revolutionary change in Cloud Computing. Docker is inspired by a technology in Linux kernel called LXC (Linux Containers). LXC refers to the capabilites of Linux kernel, specifically namespaces and control groups, which allow sandboxing of processes from one another, and controlling their resource allocations. Docker utilizes the capabilities of LXC and hence it does not require any OS, instead it relies on the OS own functionality. LXC is a OS level virutalization method for running multiple isolated Linux systems (we call them containers) on a control host using a single Linux kernel.
Hardware Virutalization vs OS Level Virtualization
If you use VMWare or Virtualbox, then you are already using hardware virtualization. Below diagram shows the difference between the two.
The primary advantage of OS virtualiation is that multiple workloads can run on a single OS instance. With hardware virtualization (VMs), the hardware is being virtualized to run multiple OS instances.
What are Containers?
OS virtualization had grown tremendously over the last decade. Containers sit on top of a physical server and its host OS (Linux/Windows). Each container shares the host OS kernel and libraries in a read-only mode. Containers are thus exceptionally light (few mega bytes) and will start in few seconds when compared to a VM. Containers’ speed, agility, and portability make them the primary tool to help building cloud native apps.
What is a Docker?
Docker is a layered binary file with all the necessary ingredients to run an application. For example, I want to use PostgreSQL database in my project and I don’t want to install it on my machine. I can download PostgreSQL docker from docker hub and run it with
docker command and use it for my development. You can also create your own docker for your application and upload that to docker hub for others to use. You can install docker community edition from here.
Let’s run a Nginx docker
In this section we will download and run nginx docker. Nginx is a popular web server that is used in most of the production ready environments. In the process we will learn some basic docker commands.
The above command
docker run -p 80:80 nginx will pull
nginx from docker hub as I don’t have it locally and runs it on port 80:80 (local:container). If I go to
http://localhost/ I will see nginx default html page with
Welcome to nginx! message. To kill it, simply press CTRL+C. To run the docker in the background or as daemon we will add
-d option to the above command. List of some basic commands below:
|docker images||Prints all local images|
|docker run||Run a docker image|
|docker run -p <host-port:container-port> <image>||Run docker image with port forwarding from host to container|
|docker run -d <image>||Run docker image as daemon or in the background|
|docker run -e <environment variable> <image>||Run docker image with defined environment variable|
|docker ps||Displays all running containers|
|docker ps –all||Displays all containers including idle ones|
|docker kill <container-id>||Kill (force kill) the running docker image with the container id|
|docker rm <container-id>||Remove the container|
Below diagram shows the docker workflow. I have taken this image from a LinkedIn training.
At a high level the image explains:
- Building docker images from docker file
- Tagging docker images
- Pushing docker images to docker hub
- Pulling images from docker hub
- Running docker images
- Starting/stopping/restarting docker containers
- Loading and saving docker images externally
In this article we learn what is docker and container? We have learnt how to download and run a docker and some basic commands on managing docker images and containers. We have looked into docker workflow. In the coming articles we will see how to create our own docker and publish to docker hub. Thanks for reading.