top of page

Getting Started With Docker:

Docker is one of the most popular DevOps Tool nowadays and in this blog, I will cover basic introduction to Docker and what problem does Docker solve in DevOps world.In first part of blog, we will cover what is Docker and why we use Docker and in second part , I will show you, how we can install and configure Docker in Linux based operating system.


What is Docker:


Let us think of a simple flow of installing some new software in computer. Normally to install new software in our computer, first we need to download the software and run that. If we get any error message during installation, we need to troubleshoot and fix the issue and rerun the installer again . If we found another error, this cycle will continue.This is what Docker is trying to fix.Docker makes it really easy to install or run software without worrying about setup or dependencies. Using Docker, we can create identical environments across various stages of development and having isolated environments for each application.Hence, it's become essential for DevOps practice. Docker Community Edition is open source, while Docker Enterprise Edition is a commercialized version offered by Docker Inc.Security is built into the Docker Engine by default.We can run any type of application (legacy, cloud native, monolithic, 12-factor etc) in a Docker container.


Now what is Container? A container is an instance of an image that packages an application with all of its dependencies and libraries. We can deploy applications within containers regardless of hardware configuration or operating system. Unlike virtual machines, containers use the host OS rather than bundling an OS with the application.

Docker ecosystem is a set of tools to build container images, pull these images from known registries, deploy containers, and manage containers across a cluster of machines.


What is the difference between Container and Virtual Machine (VM):


A hypervisor is computer software, firmware or hardware that creates and runs virtual machines. A computer on which a hypervisor runs one or more virtual machines is called a host machine, and each virtual machine is called a guest machine. We can host multiple Virtual machines in same physical server and each virtual machine runs a unique guest operating system.UNIX VM can sit alongside a Windows VM, and so on. Each VM has its own binaries, libraries, and applications that it services.

Containers sit on top of a physical server and its host OS—for example, Linux or Windows. Each container shares the host OS kernel and, usually, the binaries and libraries, too.

Container is lightweight in comparison to VM and we need less maintenance activities as we don't need to manage or patch multiple operating systems.


Comparison of how containers and virtual machines are organized. Source:Docker


Docker Architecture :

Docker uses a client-server architecture. And, its architecture consists of 3 major parts, such as:

  • Docker Host

  • Docker Client

  • Registry

The Docker client talks to the Docker daemon, which does the heavy lifting of building, running, and distributing your Docker containers. The Docker client and daemon can run on the same system, or you can connect a Docker client to a remote Docker daemon. The Docker client and daemon communicate using a REST API over UNIX sockets or a network interface.


Docker client: The Docker client (docker) is the primary way that many Docker users interact with Docker.We can install client in user computer running Windows, Mac-OS or a server running Linux, it doesn’t matter.Using Docker client tool, user can run different Docker commands.The docker command uses the Docker API. The Docker client can communicate with more than one daemon.


Docker Daemon: Docker daemon is a service that runs on host operating system where docker client is running or docker client can connect to remote Docker domain as well.

The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects like images, containers, networks, and volumes. A daemon can also communicate with other daemons as well to manage Docker services.


Docker Registry:A Docker registry is the place to pull Docker images. Docker Hub is a public registry , and Docker is configured to look for images on it by default. Based on the application requirement, we can set private registry as well You can even run your own private registry. When user runs the docker pull or docker run commands, the required images are pulled from your configured registry. When user runs the docker push command, image is pushed to your configured registry.


Docker Images: Docker images is a read only template with with instructions for creating a Docker container. It that contains the source code, libraries, dependencies, tools, and other files needed for an application to run. We can create custom image from another image.

For example, you may build an image which is based on the Cent OS image, but installs the Tomcat server and your application, as well as the configuration details needed to make your application run. To build Docker custom image,user can create Docker file with a simple syntax for defining the steps needed to create the image and run it.


Containers: As we discussed earlier, Container is an instance of an image and runs a program.We can create, start, stop, move, or delete a container using the Docker API or CLI. User can connect a container to multiple networks, attach storage to it and can create a new image based on its current state.


Docker architecture. Source: van der Mersch 2016.



Installation of Docker:


Now we will discuss how to install Docker in a Linux environment. We're going to install Docker on a CentOS server, but the steps for installation on other Linux distributions like Ubuntu etc are essentially the same.


For this demo purpose, I have used Linux cloud server running CenOs 7 operating system.


First we need to ensure that all required packages are installed in server:

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2
Sample output#
Updated:
  device-mapper-persistent-data.x86_64 0:0.8.5-2.el7                 lvm2.x86_64 7:2.02.186-7.el7_8.2                 yum-utils.noarch 0:1.1.31-54.el7_8

Dependency Updated:
  device-mapper.x86_64 7:1.02.164-7.el7_8.2            device-mapper-event.x86_64 7:1.02.164-7.el7_8.2       device-mapper-event-libs.x86_64 7:1.02.164-7.el7_8.2
  device-mapper-libs.x86_64 7:1.02.164-7.el7_8.2       lvm2-libs.x86_64 7:2.02.186-7.el7_8.2
Complete!

Add the repository that we need to install the version of the docker, we are looking for:


$sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

Sample Output:
Loaded plugins: fastestmirror
adding repo from: https://download.docker.com/linux/centos/docker-ce.repo
grabbing file https://download.docker.com/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo

Install Docker:


$yum install docker-ce

If prompted to accept the GPG key, verify that the fingerprint matches 060A 61C5 1B55 8A7F 742B 77AA C52F EB6B 621E 9F35, and if so, accept it.
Sample output:
Dependency Installed:
  container-selinux.noarch 2:2.119.1-1.c57a6f9.el7               containerd.io.x86_64 0:1.2.13-3.2.el7               docker-ce-cli.x86_64 1:19.03.8-3.el7

Complete!

Start Docker:


$sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
$sudo systemctl start docker

Check that your installation appears to be working properly:


$ sudo docker run hello-world
[sudo] password for cloud_user:
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Above output proves that docker client is able to connect docker daemon and docker daemon pulled the image hello-world from docker hub and created new container.


Check Docker version:


$docker version 
Client: Docker Engine - Community  Version:           19.03.8  API version:       1.40  Go version:        go1.12.17  Git commit:        afacb8b  Built:             Wed Mar 11 01:27:04 2020  OS/Arch:           linux/amd64  Experimental:      false  Server: Docker Engine - Community  Engine:   Version:          19.03.8   API version:      1.40 (minimum version 1.12)   Go version:       go1.12.17   Git commit:       afacb8b   Built:            Wed Mar 11 01:25:42 2020   OS/Arch:          linux/amd64   Experimental:     false  containerd:   Version:          1.2.13   GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429  runc:   Version:          1.0.0-rc10   GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd  docker-init:   Version:          0.18.0   GitCommit:        fec3683

Docker Hub: Docker Hub is a registry service for storing private and public Docker

images. From Docker Hub, user can download the Docker images built by other communities. User can upload own Docker image as well to Docker hub.

Official site for Docker Hub is https://hub.docker.com/. Once user signed up, user can logged into Docker Hub and search for the image, user is looking for. For example, if user is looking for Jenkins image, user can search it and get the Docker pull command to download Jenkins image onto user local server.



Go to your local Cent-OS server and run below command:

$ sudo docker pull jenkins

Once the pull is completed, please run below commands to run it:


$docker run -p 8080:8080 -p 50000:50000 jenkins

Above command will store the workspace in /var/jenkins_home. All Jenkins data lives in there - including plugins and configuration.


$docker run -p 8080:8080 -p 50000:50000 -v /your/home:/var/jenkins_home jenkins

Above command will store the jenkins data in/your/homeon the host. Ensure that/your/homeis accessible by the jenkins user in container (jenkins user - uid 1000) or use-u some_other_userparameter withdocker run.


Useful Docker Commands:


docker start [container]: Start a particular container.

docker stop [container]: Stop a particular container.

docker exec -ti [container] [command]: Run a shell command inside a particular container.

docker run -ti — image [image] [container] [command]: Create and start a container at the same time, and then run a command inside it.

docker run -ti — rm — image [image] [container] [command]: Create and start a container at the same time, run a command inside it, and then remove the container after executing the command.

docker pause [container]: Pause all processes running within a particular container.

docker history [image]: Display the history of a particular image.

docker images: List all of the images that are currently stored on the system.

docker inspect [object]: Display low-level information about a particular Docker object.

docker ps: List all of the containers that are currently running.

docker version: Display the version of Docker that is currently installed on the system

docker start [container]: Start a particular container.

docker stop [container]: Stop a particular container.

docker exec -ti [container] [command]: Run a shell command inside a particular container.

docker run -ti—image [image] [container] [command]: Create and start a container at the same time, and then run a command inside it.

docker run -ti—rm—image [image] [container] [command]: Create and start a container at the same time, run a command inside it, and then remove the container after executing the command.

docker pause [container]: Pause all processes running within a particular container.

docker kill [container]: Kill a particular container.

docker kill $(docker ps -q): Kill all containers that are currently running.

docker rm [container]: Delete a particular container that is not currently running.

docker rm $(docker ps -a -q): Delete all containers that are not currently running.


Summary:


Now, we have covered very basics of Docker. Hope, you will like it. In my next blog, I will cover some advance topics like Docker compose and Docker swamp.


Happy Reading!!! :)




















Recent Posts

See All

Steps to Setup kubernetes Cluster!!

In this blog, I will show you, how you can set up a Kubernetes Cluster using Kubeadm. For this hands on, I have used Ubuntu EC2s hosted in AWS environment.I have launched one master node and one work

bottom of page