Docker image and container

In this tutorial we will cover importance or docker image and container. We will learn in depth about how it works. I would suggest to learn basics before you start this tutorial check out my previous tutorial on basics of docker:

What is Docker?

What is a Docker Image and Container?

Docker image is a single file that contains different dependencies or configurations that is required to run a program. Container is an instance of an image that runs a program.

Consider an example of class and object. Class is a blueprint while object is an instance of that blueprint. In this case class is an image and object is a container that runs a program.

Creating a container from the image

Once you have a docker installed on your machine open your terminal window and make sure it is installed properly by running following command:

docker version

You will see the following output on your terminal. Keep in mind it could be different depending on version on your local computer.

First thing we learn is when dealing with image or container is docker run command. Let's see the syntax of this command.

docker run <image-name>

Let's now understand what this command does and then we learn in depth about how docker translate this command internally. Let's run following command on our terminal window and see what it does:

docker run hello-world

Let's look at the output of the command:

Following is a diagram on what docker does when it gets the command from the terminal:

Let's now understand what happened when we ran this command:

  • Docker cli takes our command and hands off to Docker Server
  • Docker server looks at the local cache system to see if image exists on your local machine
  • If image does not exists then it shows you the following command
  • Unable to find image 'hello-world:latest' locally
  • Then it tries to find this image from Docker hub ( Public Repository )
  • When it finds the public image available on docker hub it gives you following message
  • latest: Pulling from library/hello-world
  • Once image is downloaded docker run command does following two things
    • Creates a container from the downloaded image
    • Runs the container created from the docker image
  • Now, this image just display following message
  • Hello from Docker!
  • That is it!!

How docker talks to your computer?

Your computer contains following three important layer. Software layer, Kernal Layer and Hardware layer. You might have different softwares or tools running on your computer like:

  • a browser
  • terminal window
  • NodeJs etc...

Kernal allows your tools or software to access some hardware on your computer. Your tools can not talk directly to hardware. Let's see a diagram to understand.

Consider a scenario where you have to install two software or program on your computer. First program requires python v2 and the other program requires python version 3.

Technically you can only have one version of python on your computer. You can not install two different versions of python in your computer.

Docker solves this problem in following manner:

  • it uses a a feature of operating system called namespacing
  • it creates two different namespace to store two different versions of python
  • when your run the first and second program system directs its call to kernel and kernel finds appropriate namespace to talk to correct version of python

Let's have a look at the following diagram to understand:

Namespacing or Versioning

Docker make use of namespacing to isolate resources per process or group of processes. Namespacing can be used with software or hardware.

You can either combine multiple software or multiple hardware or both to create a namespace. Basically your docker image can make use of namespace to allocate specific softwares or hardwares to given image.

You can have multiple images that can reserve different namespaces. Therefore you can run multiple program that make use of different versions of software to run on your computer without any problems.

If you work on a different projects then you will know how hard it is to install and manage different tools on your machine and dealing with different kind of installation issues.

Docker helps you to install different programs and create an image out of it so that you can run this program by creating a container out of that image.

You can create multiple containers out of the single image and create or distroy as per your need.

Let's look at how docker containers uses name-spacing to isolate themselves using different hardware or softwares:

  • Docker image contains list of softwares that is needed to run a process in container
  • Docker server creates the container out of an image and isolate resources that a container needs

I hope in this tutorial I was able to clear your concept about docker image and container. Stay tune for my next tutorial on docker and spread your love by hitting my heart below.