What is docker engine, docker image and docker container and how they connected

This tutorial shows how to start working with the docker commands and teaches the docker basics. Docker engine is a software which needs to be installed in your computer so that you are able to work with docker images and containers.

 

As an example, you can install Docker Desktop for Mac from Docker Hub website. This software has docker engine and once you install it, you are able to create or download existing docker images. Docker engine is able to make docker containers out of docker images. So the flow is like this:

 

Install Docker Engine ==> Download Docker Images ==> Create and run Docker containers

 

So what is a docker image? Let’s imagine you have a computer or a virtual machine which has a Ubuntu operating system. You can run different commands and also do many things such as IO, networking, etc. But the thing is the Ubuntu operating system has lots of modules or programs which most of them won’t be used by users all the time.

 

As an example, if you write a simple hello-world program in C++ or Java in ubuntu, you might only need a few modules of the operating system to run your program.

 

So the idea of docker is making a light-weighted operating system which does only a few fundamental of tasks that is necessary for running your application. This helps to save a lot of memory, cpu, and space which a full operating system needed to have otherwise.

 

Also operating systems such as ubuntu is always changing and upgrading. If your applications is only tested in a specific version of operating system and hasn’t tested or upgraded for any reason, you can create a package and make it as a docker image so that you won’t need to worry about the latest changes and patches and it works seamlessly by docker engine.

 

So docker images simply are a minified and optimised version of a software or in our example (operating system) which saved somewhere and people are able to download them and utilise them to run their applications.

 

Docker Hub is a place which you can find many docker images and also you are able to push or save your own docker images there.

 

The important point is docker images are just files which need a software called docker engine to recognise them and work with them. So you must install docker engine in your system before working with docker images. In my other post, I explained how to install docker engine in Ubuntu if you are interested: https://fullstacklogs.com/docker/installing-docker-on-ubuntu/

 

So what is the docker container then?

 

let’s start by an example; if you install docker engine in your laptop you should be able to run below basic command:
docker run hello-world

 

The first time you run the command the docker engine in your laptop connects to the docker hub and downloads the image from https://hub.docker.com/_/hello-world . Then after downloading the image in your laptop it makes a docker container of that image and runs the hello-world program inside it.

 

As you see below the docker image gets pulled from the docker hub and a new docker container gets created and a hello-world program gets executed and you can see the Hello from Docker! output:

 


docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:31b9c7d48790f0d8c50ab433d9c3b7e17666d6993084c002c2ff1ca09b96391d
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

 

Now if you run the command : “docker images” you can see the list of all the images which docker engine has download from the docker hub and has saved in your system:

 


docker images   
REPOSITORY              TAG                     IMAGE ID            CREATED             SIZE
hello-world             latest                  bf756fb1ae65        13 months ago       13.3kB

As you see the hello-world image has the latest TAG and has an ID .

 

Now if you run the command: “docker ps –all” you can see all the docker containers which docker engine has created from docker images:

 


docker ps --all
CONTAINER ID  IMAGE       COMMAND  CREATED        STATUS                     PORTS  NAMES
46bba1f9d4b8  hello-world "/hello" 23 minutes ago Exited (0) 23 minutes ago         priceless_faraday 

As you see a docker container with the ID of 46bba1f9d4b8 from the hello-world image has created and the command /hello finished running and you see the outputs.

 

Now if you run again the command “docker run hello-world“, the docker engine first looks at the local images download folder to see if the image already downloaded or not; if it has already downloaded it creates a new container and runs the hello world program otherwise downloads and runs a new container:

 


docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

As you see the second time , docker engine realises that the image already exists so it only creates another container and runs the program. If you remember in the first run the engine wrote: Unable to find image ‘hello-world:latest’ locally and started downloading the image.

 

If we run the “docker ps –all” command again we can see there are two containers based on the hello-world image which is created:


docker ps --all
CONTAINER ID        IMAGE         COMMAND     CREATED         STATUS                     PORTS  NAMES
1e514798dff2        hello-world   "/hello"    4 minutes ago   Exited (0) 4 minutes ago          compassionate_dewdney
46bba1f9d4b8        hello-world   "/hello"    34 minutes ago  Exited (0) 34 minutes ago         priceless_faraday

So the point is every time you run a docker image it creates a container and runs the initial command inside the container.

 

If you want to just run an existing container instead of creating a new container you can run the command: “docker start container-id -a“, which container-id can be found when you run “docker ps –all“, also “-a” means to wait for output & stdout

 


docker start 1e514798dff2 -a
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/

 

I hope by reading this post you understand the concepts of the docker engine, docker images and containers. In the next tutorials we will explain useful docker commands and docker-compose.