Docker Intro

Docker is a platform that manages application using containers. Docker packages application and its dependencies as a container. It also provide an intuitive interface for users to interact with Containers. Docker is widely used as a tool for application deployment.

Compare Docker to VM


Benefits of Docker

  • fast deployment
  • lightweight. Docker images are small
  • performant compared to VM
  • require mimimal runtime requirements
  • portable. minimize environment issue
  • ability to version control the containers

Benefits to use Docker for Devops

  • Developers don’t have to setup local environment
  • Operations team can set up environment easily
  • Works with DevOps tools such as Ansible, Puppet and Chef

Architecture

Docker uses a client-server(CS) architecture. The Docker client talks to the Docker daemon.

Docker Daemon listens to the Docker API and process all the requests. constantly managing containers and images.

The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon.

A Docker registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to look for images on Docker Hub by default.

Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands.You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.

Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual host.

Docker has two segments

  • Community Edition(CE) - open source and free
  • Enterprise Edition(EE) - close source and needs to be licensed yearly. provides 24x7 support.

Important Concepts

Image

Image is a read-only template for creating Containers. The Open Containers Initiative (OCI) defines a Container Image Format Specification. This is the most commonly used image format.

Container

A container is a runnable instance of an image.

Registry

Images are stored in Registry. Docker Registry is a service.

Docker’s default registry is Docker Hub(docker.io). There are other public registries maintained such as quay.io. Cloud providers like Amazon, Google and Microsoft also provide registry. You can also host private registries to better integrated with CI/CD pipeline.

Repository

Repository is a collection of related images. Provide versioning for images. Usually it is the image with the same name but different tags.

Container Runtime

Lowlevel runtime for the container. The reference implementation for Open Containers Initiative (OCI) Runtime is runc.

lxc, runc and rkt are the most popular runtimes. Docker Engine runs on runc container runtime.

Container Engine

A Container Engine can handles user request. Allowing the user to interact with container runtime. When we refer to Docker, we often mean Docker Engine. rkt is also a container engine.

Installation for Ubuntu

see https://docs.docker.com/install/linux/docker-ce/ubuntu/ for the detail instructions.

Execute the following command to install docker-ce.

Uninstall old versions

1
sudo apt-get remove docker docker-engine docker.io containerd runc

Install Docker version 18.06.1-ce for ubuntu

1
2
3
4
5
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
sudo apt update
sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu

To Install a specific version:

  1. first list versions available in your repo
    1
    apt-cache madison docker-ce

output:

1
2
docker-ce | 18.06.1~ce~3-0~ubuntu       | https://download.docker.com/linux/ubuntu  xenial/stable amd64 Packages
docker-ce | 18.06.0~ce~3-0~ubuntu | https://download.docker.com/linux/ubuntu xenial/stable amd64 Packages
  1. Then install a specific version using version string from the second column
    1
    sudo apt install docker-ce=18.06.0~ce~3-0~ubuntu

To see docker version after installation

1
2
3
docker version
# or
docker info

add current user to the docker group. without this step, you will need to type sudo everytime you execute a docker command

1
2
sudo groupadd docker
sudo usermod -a -G docker $USER

You may need to restart after adding current user to docker group.

To test installation, run the following command.

1
docker run hello-world

If you want more fun, run a dwhalesay container

1
docker run docker/whalesay cowsay hello docker

Start and Stop Service

use init script to stop/start docker service

1
2
3
sudo /etc/init.d/docker stop
sudo /etc/init.d/docker start
sudo /etc/init.d/docker restart

or simply

1
2
sudo serice docker stop
sudo service docker start

To disable docker service on rebooting of Ubuntu

1
sudo systemctl disable docker

To enable docker service on reboot

1
sudo systemctl enable docker

Useful links

Reference