Docker Containerisation world (Basics) Part 1

Nikhil Gavali
Globant
Published in
7 min readMay 17, 2022
Docker Containerisation

What is Docker

Docker is a platform or ecosystem around creating and running the containers. It makes life easy to install and run software without worrying about setup or dependencies.

It is a platform that allows users to easily pack, distribute, and manage applications within containers. In other words, It is an open-source project that automates the deployment of applications inside software containers.

Docker makes it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package. By doing so, the developer can be assured that the application will run on any other Linux machine.

To proceed with Docker, lets install the docker in your system. Once you have successfully installed the docker then verify the docker in your system by running the command called docker versionon you terminal.

Inside the Docker

Docker package comes with two tools namely Docker Client and Docker Server.

Docker Ecosystem

Docker Client: Docker Client is also called Docker CLI that issue commands to interact with another tool called Docker Server. And This happens behind the scene.

Docker Server: Docker Server is the second tool that is responsible for creating images, running containers, uploading images, etc. We never going to reach the Docker Server because it runs in the background. We deal only with the Docker CLI to run the commands.

Using Docker Client

First, run the command on your terminal. The command is docker run hello-world. As soon as your run this command you may notice some information printed on the terminal. Refer to the below screenshot and command.

  • In the above image, you can see we tried to install the hello-world image from the docker hub. So here you can consider the image as software.
  • When you ran the command the first statement we got is “unable to find image hello-world: latest” locally.
  • And the excepted is “Hello from Docker!”.This is the default message returned by the image or you can say that this is the output from the image.
  • Now you may have questions like what is happening here ?. Let’s jump into its internal working behind the scene.
working of docker client
  • In the above image how the things run behind the scene when we run the docker command with any image.
  • Here we have installed the hello-world image, Once you hit this on the terminal, So here the docker client comes into the picture which issues commands and interacts with the Docker server behind the scene.
  • Once the Docker server gets the request from the docker client it is going to check the images under the image cache. If it finds there then the docker server pulls the image from the image cache else it is going to look in the Docker hub or registry to pull down the images.
  • Once the Image cache has the copy of the images which has been initially downloaded, the next time when we run docker for the same image the docker server is going to pull the images from the image cache.
  • Now the second time you won't be able to see the message “unable to find image hello-world: latest” because the image is already present under the image cache in your machine.

What is Container (In-Depth)

The container is a program with its own isolated set of hardware and resources. So it can have its own space of memory or it can have its own hard drive space as well. Image is a single file with all the dependencies and configurations required to run a program. The container is an instance of an image. It means it runs that program.

Flow of container

Relationship between Image and Container

  • The above diagram illustrates the meaning of each keyword we use during the creation of the container. Let’s run the command docker run hello-world.
  • Let's understand what exactly happens behind the scene when you ran the above command.
  • Image is nothing but the file system which has all the programs which we need to install on our system.
before running the command
  • In the above diagram, we can see that somewhere in the hard drive we have the image and it has a file system, in which we have the hello-world program.
  • When we ran the command we took the snapshot of that file system and we stuck it into the container’s hard drive. Because each container is an isolated process that has its own set of resources.
  • And then we executed the command run hello-world. So the running process is the hello-world that ran and exited immediately by giving the output on the terminal.
After running the command

Life Cycle of Container

  • Till now we have seen the creation of containers with the docker run command, But we can also create the container with the docker createcommand. So what is the difference between both themes? Let's understand it.
  • When you use docker create <image-name>, it is going to create the container out of this provided image and give you the id to start it. Say for example I am executing the docker create command with a hello-world image.
  • You can see in the above image we have created the container and it has a printed container id on the terminal. Using this id we can start the container.
  • -a the command is used to show the output from the container.
  • Here we have to use two commands to create and run the container. What if there is one command which does both things alone. So here comes the docker run.
  • docker run => docker create + docker start
  • In the above image, you can see that the docker run command is creating the container, starting the container, and pulling the image from the docker repository if does not exists on the system.
  • We can list out the running containers with the docker pscommand. And there is one more command docker ps -awhich shows the created and exited container.
  • In the above diagram, you can see the headers like container id, image, command, name, status, port, etc.
  • We can again start the created container just by using the container id which shows after running the docker ps -acommand.
  • command header is nothing but the commands which we issue during the creation of the container. Now you may have a question about /hellocommand which is printed in the above diagram.
  • This is the default command for the image hello-world. When we ran docker run hello-world then the container got created along with the default startup command /hello.
  • These default commands get added to the start-up command of the created container. But we can also provide the other start-up command for the container during the run process. Let's understand with the below example.
  • Run docker run busybox echo hi there.
  • Here the busybox is the official image from the docker hub repository.
  • The above command got executed with echo hi there as a start-up command. This command has already been pre-programmed for the busybox image.
  • This is how it works behind the scene. It creates the container out of the image busybox and puts the image's file system in the container's hard drive. And start the container with the provided startup command echo hi there.
  • We can stop the container with the docker stop <container-id> command.

Conclusion

We have covered the basic concepts and in-depth knowledge about docker and its working in our system.

In the upcoming blog, we will be learning how we can achieve this using docker files in our project. If you have any queries then feel free to add them in the comments section.

--

--