Containerization with Docker... šŸ³

Containerization with Docker... šŸ³

Hey everyone šŸ¤—, in this tutorial we will see about Docker, what is it? why is it being used and how to create your own docker image?

let's start...

Before going to docker, we should know about the "Containers".

What are containers?

Containers are an effective way to build, test, deploy, and redeploy applications on multiple environments from a developerā€™s local laptop to a data center and even the cloud. It allows applications to be more rapidly deployed, patched, or scaled. But in non containerized practice it is comparatively harder to maintain.

suppose let's say you want to share the source code of your application with your friend, once he received the source code he will run it on his local machine. while running he gets lots of issues and problems unlike you, due to some internal dependencies problems in his system. You want to overcome this problem. Here come the containers to rescue.

you can create a container and store all the code files + required dependencies + packages and everything you needed for that application to run, then you can share that container with your friend. Now your friend runs that container in his machine and it works like how it worked on your system.

What is the difference between containerization and virtualization?

doc1.png

Virtualization:

Virtualization helps us to create software-based or virtual versions of a computer resource. These computer resources can include computing devices, storage, networks, servers, or even applications.

It allows organizations to partition a single physical computer or server into several virtual machines (VM). Each VM can then interact independently and run different operating systems or applications while sharing the resources of a single computer.

Hypervisor software facilitates virtualization. A hypervisor sits on top of an operating system. But, we can also have hypervisors that are installed directly onto the hardware. Hypervisors take physical resources and divide them up so that virtual environments can use them.

Containerization:

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.

Each container is an executable package of software that runs on top of a host OS. A host can support many containers concurrently. For example, in a microservice architecture environment, this setup works as all containers run on the minimal, resource-isolated process that others canā€™t access.

Now let's see...

What is docker?

Docker is a container platform that allows you to build, test and deploy applications. We can define all the applications and their dependencies in a Dockerfile which is then used to build Docker images that defines a Docker container. Doing this ensures that your application will run in any environment.

You can pull an image from the docker registry from your CLI. The existing images of yours will be stored in the docker daemon. If it doesn't present, the daemon pulls it from the registry automatically.

doc2.png

Dockerfile

It Describes steps to create a Docker image. These images can be pulled to create containers in any environment. These images can also be stored online at docker hubs.

When you run docker image you get docker containers. The container will have the application with all its dependencies.

Steps to Follow:

  • Create a file named ā€˜Dockerfileā€™ as it is the convention.

  • By default on building, docker searches for ā€˜Dockerfileā€™ $ docker build -t sample-image.

  • During the building of the image, the commands in the RUN section of Dockerfile will get executed. $ docker run ImageID.

  • The commands in the CMD section of Dockerfile will get executed when you create a container out of the image.

example:

FROM ubuntu
RUN apt-get update
CMD [ā€œechoā€, ā€œHello Dockerā€]

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, like a template. Docker images also act as the starting point when using Docker. An image is comparable to a snapshot in virtual machine (VM) environments.

doc4.png

Docker is used to creating, run and deploy applications in containers. A Docker image contains application code, libraries, tools, dependencies, and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container.

doc7.png

Docker can build images automatically by reading instructions from a DockerFile. A single image can be used to create multiple containers. Images are built in layers. Each layer is an immutable file but is a collection of files and directories. Layers receive an ID, calculated via an SHA 256 hash of the layer contents.

BASIC DOCKER COMMANDS:

$ docker pull mongo // it pulls the MongoDB container from the registry
$ docker images // list all the docker images
$ docker run image // creates the container out of the image.
$ docker start // Starts one or more stopped containers
$ docker stop // Stops one or more running containers
$ docker search // Searches the Docker Hub for images
$ docker rmi image // deletes the image 
$ docker rmi $(docker images -q) // deletes all the present images.
$ docker ps // list all containers
$ docker start ContainerName/ID
$ docker kill ContainerName/ID // Stops a running container
$ docker rm ContainerName/ID // Deletes a stopped container
$ docker rm $(docker ps -a -q) // Delete all stopped containers

Outro:

I hope this tutorial gave you a brief understanding of what are containers and how it works, then about docker and how can we create containers using docker and about the inner architecture of docker.

I strongly encourage you to go through the docker documentation and have a deeper understanding of the Docker CLI.

related resources:

Thank you for reading šŸ˜Š

Happy learning

- JHA

Ā