Docker - a complete guide

Docker - a complete guide

What is docker?

Docker is an open platform 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. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Why we need docker?

is that ever happened to you that you have created a project on your local system and now you are trying to test on another system (for an example your friend's system) and you found that the application is not running as same as it was running on your local system !!! -it can be resolved by containerization of your application so in this particular guide we are going to see it.

-using docker your application can be shipped very seamlessly and it will guarantee that it will run as it was running on your local system because in containerization all the resources that the application needs to operate will be provided in container so there will not be any version or OS related issues. You can run microservices and scale it on big stage using docker. it will also utilize your resources like RAM and memory that we will see in the comparison of containerization vs VM. image.png

image.png

Containerization vs Virtualization

  • What used to happen was one server was able to run one application only. It is definitely not optimal because if you want concurrent access then it should be running on more than one server and you have to build more and more servers to do that because your sever can host only one application at a time.
  • Then VMware comes with the solution that on one system (virtual machine) you can have more than one OS so you will be able to host your app in each OS. If you see the architecture of the virtual machine it will distribute your computing as well as the data resources in parts and OS kernel to the parts also.
  • Then the concept of the containers come into picture you can see its architecture that it will not distribute the resources but they will share when its needed also on one OS you can run more than one containers at a time.
  • I have given the information in the brief you can see the architecture difference and all that stuff in the below pictures
  • Container internally runs on the virtual machine itself, yes you have read it correctly!!!

image.png

Architecture of Docker

-Docker uses a client-server architecture(T1 architecture). The Docker client talks to the Docker daemon, which provides us running ,stopping and creating the containers from images and many more such facilities.

image.png

Docker Daemon

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

The Docker client

  • The Docker client (docker) is the primary way that many Docker users interact with Docker. When you use commands such as docker run, the client sends these commands to docker daemon, which carries them out. The docker command uses the Docker API. The Docker client can communicate with more than one daemon.
  • if you want to install Docker desktop or on mac checkout the official documentation on the installation guide

Docker registries

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. You can even run your own private registry. When you use the docker pull or docker run commands, the required images are pulled from your configured registry. When you use the docker push command, your image is pushed to your configured registry.

image.png

How to make a docker file?

  • A docker file is a kind of structure and dependencies of your application. by docker file you can create an image (Docker image) that can be pulled by anyone if you are publishing it on the DockerHub. The running instance of an image is called container so you can run a container using an image.
  • Below one are the steps to create a docker file

  • create a file named 'Dockerfile' (exactly like this only)

  • then see the docker file given below and create your own file like this

FROM ubuntu
MAINTAINER kunj patel <patelkunj2021@gmail.com>
RUN apt-get update
CMD [“echo”, “Hello World , hi I am kunj !”]
  • Build your ‘Dockerfile’ using your CLI by just giving command

    $ docker build -t myimage:1.0 .
    
  • During building of the image, the commands in RUN section of Dockerfile will get executed.

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

Some detail about Docker Image

  • A docker image is a file that defines docker container. So whenever you will share your image the container will run exactly that you wanted because it contains all the dependencies required including OS information to execute the code of the application. once you build your image you can upload it on DockerHub and from there anyone can pull and run your Docker image.

  • checkout Docker Hub

Some commands of Docker CLI

$ docker pull ubuntu:18.04 (18.04 is tag/version )
$ docker rmi $(docker images -q) (deletes all Docker images)
$ docker run image (creates a container out of an image)
$ docker images (Lists Docker Images)
$ docker rmi image (deletes a Docker Image if no container is using it using the image name)

Docker Components

image.png

Docker images are build in layers. Layers are files and directories that can't be chaged or renamed . Each layer will consist the Hash values calculated by the SHA 256 (hash function) using the content of the layers.

Hash values of the docker images

image.png

  • As you can notice first 12 letters of hash is equal to the IMAGE ID.

image.png

Containers

image.png

-A container is a runnable instance of an image. This is where your application is running. You can manage containers using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. If we delete a container the data will be lost! Because when the container went down and we brought it back up, the last layer got created again as a new layer. This helps in development if you don’t want to store record for each test. To be persistent, use volumes to store data.

$ docker inspect ubuntu

You can see the layers now!!

image.png

Some more commands of Docker CLI :-

$ docker ps (list all containers)
$ docker run ImageName/ID (it will check for local file otherwise it will pull it by DOCKER_HOST)
$ 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)

Running Docker container

  • I have already images so first of all i am removing an image (-f is for forcefully deletion)

image.png

  • now trying to run but since i have deleted the ubuntu image it is not available in local system so it is pulling it from docker hub

image.png

  • it is smart that i have downloaded the image before so it will not download again

  • now i am running it as interactive environment and you can see that i can use the ubuntu bash it is very easy like this you can run any image in your local system

image.png

--If you have completed till here please 🙏don't forget to follow my socials

-github
-twitter
-Linkedin

Did you find this article valuable?

Support Kunj Patel by becoming a sponsor. Any amount is appreciated!