Day 12 Docker basics Part 1

Apurv SamadderApurv Samadder
6 min read

What is Docker?

Docker is an open platform that can be said a Platform as a service 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. Because of Docker the so-called dilemma, that "It runs on my system, but not running on your system" is removed

Before we dig deep into docker we must understand the concept of virtualization

Virtualization is a technology that allows multiple operating systems (OS) or applications to run on a single physical machine. It involves creating virtual instances of computing resources, such as servers, storage devices, or networks, which can operate independently of one another on the same hardware. This is achieved by using a hypervisor or virtual machine monitor (VMM), a software layer that abstracts and manages the underlying physical hardware

The key component of Virtualization is Hypervisor & there are 2 types of Hypervisor

Type 1 Hypervisor*: This is a small OS or Firmware (aka Hypervisor)that is installed on Bare Metal, and virtualization is done on this.*

Type 2 Hypervisor: It treats your OS as a Host and runs over it. On top of this Software like Virtual Box and Vmware kind of tools are used, which can be used, to Install different OSs, Where your Base OS interacts with Vmware / Virtual Box. and shares the resource as per need.

The biggest Disadvantage is that you need to install a complete OS on Vmware to work on it. And that takes wholesome resources of your host machine, on which Hypervisor is running.

virtual machine - Are hardware drivers needed to be installed on the  management OS of a type 1 hypervisor? - Super User

Why Docker is Lightweight?

Docker uses a different type of Virtualization, whereas in traditional Virtulization complete OS along with the Kernel is installed on VMware, however, in the case of Docker, we are using the kernel of the host operating system because of which the resource usage is optimized.

Docker Architecture

  • The Docker client talks to the docker host/server, which has Docker Deamon(dockerd) in it, which is installed in the host machine.

  • Docker is based on creating images and using the image to create a container.

  • In case the requested image is not present locally then dockerd makes a call to the DockerHub/ Docker registry and pulls the image from there to the host machine, which in turn helps to create and run a container.

Understanding Docker Architecture | by Vikram Gupta | Medium

Very Important point that must be understood

Program File or setup Files is used to create Image and from that image, Containers are created.

  • Files ------ > Images ------ > Containers

  • From Docker, File Image is created and From Image Container is created

So basically image is a set of files that is used to create and run a container.

Docker components

  1. Docker Engine: Docker Engine is a core component of the Docker environment that give a platform for creating and running container.

  2. Docker Deamon: Also known as DockerD / dockerd, is a background process that runs on the host system, for running and managing the container in the host system.

  3. Docker Container: This is an isolated, standalone executable package that has everything needed to run a piece of software, including codes, libraries, executables etc. An Important point to note here is Docker container is a platform to provide containerization. Docker is a container orchestration tool

ContainerD: It's an Open source tool from CNCF, which focuses on providing a platform to run the containers. Containerd is an industry-standard runtime that serves as the underlying container engine for many container orchestration platforms like Docker

  1. Docker CLI/ Docker client: This is a Command Line Interface, that is used to give commands to DockerD. The commands always start with docker <command_name>

eg. docker ps, list all container

  1. Docker image: Docker Engine uses Image as a basis for creating and running the container. Image is a lightweight, standalone executable package that has everything to run a piece of program.

  2. Docker File: The Docker File has all the set of steps that are needed to create a docker image. It defines the base image, required dependencies, and specific commands to run the container.

  3. Docker Hub/ Docker registry: Treat Docker Hub as a Git hub where we keep our development repository, but in Docker Hub we store images. In Docker Hub or Docker Repository, we can push, pull, tag our Docker Images, which can be pulled and used at any part of the world using a simple command. This is a cloud-based repository and is also used for community contribution

docekr pull <image_name>:<tag>

Installing Docker in Centos7

#Updating the Package Database
sudo yum check-update

# It will add the official Docker repository, download the latest version of Docker, and install it:
curl -fsSL https://get.docker.com/ | sh

#It will start Docker Daemon
sudo systemctl start docker

#Check the status if it started
sudo systemctl status docker

Adding a user in which docker is run, so that every time you need not give Sudo while running the Docker command. Once you add the user to the Docker group , make sure to restart your system using or even you can reload the Docker

sudo usermod -aG docker <username>
systemctl restart
or 
systemctl restart docker

Tasks

  • Use the docker pull command to pull images from the docker hub

  • Use the docker images command to see the images in your local host

  • Use the docker run command to start a new container and interact with it through the command line. [Hint: docker run hello-world]

    Or apparently, we can also use the image in the local host to run and create a container using the run. Normally it will exit after running, depending on what's in image that we are running

      docker run image_name
    
  • Use the docker ps command to check the container status

      docker ps # will show if the container are running
      docker ps -a # will show all the continer that are running and exited
    

  • Use the docker inspect command to view detailed information about a container or image.

      docker inspect <continer_name or Image_name>
    

  • Use the docker port command to list the port mappings for a container.

  • Use the docker stats command to view resource usage statistics for one or more containers.

  • Use the docker top command to view the processes running inside a container.

  • Use the docker save command to save an image to a tar archive.

  • Use the docker load command to load an image from a tar archive.

  • Use the docker kill command to load an image from a tar archive.

Docker File:

As described in the basics of Docker, a Dockerfile, is a file, that has all the steps to create an image from the program file, and that Image is the user to create a container.

Below is a simple project for a Dockerfile of a simple Java application

Hell.java (Simple java program)

public class Hello {
  public static void main(String Args[]){
      System.out.println("Hello New Image");
     }
}

Dockerfile
# pull base image for java 11
FROM openjdk:11

#create a working directory to copy all the files
WORKDIR /app

# Code copy to image for running continer 
COPY Hello.java /app

#Compile the code
RUN javac Hello.java

# App is now ready to run , pass the arguments
CMD ["java","Hello"]

After this file is created we just Need to build it and Run the container from the Image

docker build -t java-app:v1
# v1 is a tag

docker run java-app:v1

0
Subscribe to my newsletter

Read articles from Apurv Samadder directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Apurv Samadder
Apurv Samadder