An introduction to Docker

An introduction to Docker

what is docker?

Introduction

About Docker

The world of software development has changed radically in the last decade. Gone are the days of writing a program that would run natively on a single operating system. It is now standard for developers to write applications in JavaScript or other languages, which can be deployed on any operating system without major changes.

Docker is a popular open-source project written in go and is developed by Dotcloud which is A PaaS Company. We will see the Architecture of docker and how it works

Architecture of docker

Have you ever used virtual box? If yes you would have noticed that it takes a lot of time to start as it starts the os from the scratch it will have its own kernel and all the resources will be divided between the main os and the virtual box. But on the other hand, the Docker containers share the same system resources, they don’t have separate, dedicated hardware-level resources for them to behave like completely independent machines, it uses the kernel of the base os itself. Docker works on containerization technology.

Containerization is a lightweight alternative to virtualization. This involves encapsulating an application in a container with its own operating environment. Thus, instead of installing an OS for each virtual machine, containers use the host OS.

Introduction to Docker (2).png

What's the need for Docker?

With Docker, it's possible to run a single version of software on multiple OSes without having to worry about compatibility. This means that you can deploy a NodeJS application that can be run on Linux, macOS, and Windows without any trouble at all!

As an added benefit, containers are very easy to share between developers. If another developer has written a program for NodeJS, for example, and all you need is the database back end, you can quickly and easily take their containerized application and use it without having to go through the hassle of setting up your own development environment.

Installation

Since the installation depends on what operating system you are using, To install Docker on your OS please follow the official docs:

Group 88.png

Understaning Terminologies

Docker Engine

Docker engine is the underlying layer that Docker runs on. It's a lightweight runtime and set of tools for managing containers, images, builds, and other things.

Docker Client

Think of this as a UI for the docker, Client is what you as the end user of docker that is used to communicate with.You are interacting with the Docker Client, which relays your instructions to the Docker Daemon.

Docker Daemon

The Docker daemon is responsible for executing commands sent to the Docker Client, such as building, running, and distributing containers. The Docker Daemon runs on the host machine, but you never communicate with it as a user. The Docker Client can, but is not required to, run on the host machine. It can run on a separate machine and communicate with the Docker Daemon on the host machine.

Dockerfile

A Dockerfile contains the instructions for creating a Docker image. These instructions can be as follows:

FROM CentOS

RUN yum install python3 -y

CMD python3

Docker Image

A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container. Images are read-only templates that you build from a set of instructions written in your Dockerfile.

Docker Containers

A Docker container is a virtualized runtime environment used in application development. It is used to create, run and deploy applications that are isolated from the underlying hardware

Practicals

As we have downloaded the docker in our OS Let's head over to the practicals and run docker images.

The command to run the docker image and get the iterative terminal

docker run -it {nameofimage}

If the image is present in the local system then it will run directly and by chance, the image isn't available on the local system then it will search in the Docker Hub, install that image and then run the image.

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
9db2ca6ccae0: Pull complete
Digest: sha256:4b8ff392a12ed9ea17784bd3c9a8b1fa3299cac44aca35a
Status: Downloaded newer image for hello-world:latest
Hello from Docker!

This message shows that your installation appears to be working correctly. If you want to see all the images present then you can use the cmd

docker images

Capture2.PNG

To list all the docker containers you can use the below cmd. "-a" will show the results with details.

docker container ls -a

Capture3.PNG

To run any of the images every time you run a docker container it will be assigned a default name (That you can change), as well as an id, is assigned. here in the below example, OS1 is the name of the os that is assigned at the time we are launching the docker container.

docker container run -it --name  OS1  centos:7

Capture4.PNG The docker exec and docker attach commands allow you to connect to a running container. To remove all docker containers weather start or stop using the below command.

docker container rm -f  $(docker container ls -a -q)

How to make custom images ?

Using existing image

Setp1 :- Download any of the image for example centos from docker hub.

Step 2:- Make the nessacesy changes that you want to do lets say install net-tools, Apache.

Step 3:- Commit the changes that you have done by typing the below command

docker commit OS2 Myimage:V1

Now you can launch your own image to launch multiple containers. even you can check by typing the command docker images it will show you the images Myimage1:V1 with other images that you have downloaded form the docker hub.

Using Dockerfile

We will create a custom image to run the python code. For that first, we will create a docker file.

FROM CentOS
RUN yum install pyhton3 -y
CMD python3

first make a directory by mkdir ws/ change the directory to ws gedit Dockerfile. here SigNoz:v1 is the name you wanna give to a custom image.

docker build -t --name SigNoz:v1 /ws/
docker run -it --name os2 SigNoz:v1

Fun Facts

  • More than 50% of companies using Docker environments use orchestrators such as Kubernetes, Mesos.
  • The Most Widely Used Images Are ( NGINX, Redis, and Postgres )
  • The Average Size of a Docker Deployment Has Grown 75% in One Year