top of page

What is a Docker Container? Container vs Image



In my previous article you learnt what a container concept is. So now let's look at what a container is technically.


What is a Container Technically?

A container is made up of images. So we have layers of stacked images on top of each other and at the base of most containers, you would have a Linux-based image, which is either Alpine with a specific version or some other Linux distribution. Those base images need to be small.


That's why most of them are Alpine. Small images will ensure that the containers stay small in size, which is one of the advantages of using containers. So on top of the base image, you would have an application image, and this is a simplified diagram:

Linux-based Image
Linux-based Image

Usually, you would have these intermediate images that will lead up to the actual application image that is going to run in the container. And, of course, on top of that, you will have all this configuration data.


Example of Using a Docker Container

So now it's time to dive into a practical example of using a Docker container and how it looks when you install it, download it, and run it on your local machine.


To explain how this works, let's head to the Docker Hub and search for PostgreSQL:

Search for "postgresql" in Docker Hub
Search for "postgresql" in Docker Hub

Here is an official docker image; I can see some of the versions, and I'm looking specifically for older versions. For example 9.6 something, so I will pull that one:

Different Docker Image Versions
Different Docker Image Versions

So this is a Docker repository. This way, I can pull the image from the repository directly. Since it's a public repository, I don't have to log in, I don't have to provide any authentication credentials or anything.


I can just get started with a simple docker command without doing or configuring anything to access Docker Hub. So on my terminal, I can do a docker pull . I can even do a docker run and copy the image name. If I don't specify any version, it will just give me the latest. But I want a specific version, so I can provide a version with a column and start running it:

Simple "docker run" command
Simple "docker run" command

So as you see below, the first line says "unable to find images locally". So it knows that it has to go to the Docker Hub and pull it from there, and the following line says "pulling from library/postgres":

Downloading Layers
Downloading Layers

You will see several hash symbols that indicate downloads. This brings us back to where I mentioned that Docker containers consist of multiple layers, such as the Linux image layer and the application layers. Each of these layers is downloaded separately from the Docker Hub onto your machine. This helps to make the process more efficient and saves storage space.


Example

For example, if the image changes or I have to download a newer version of Postgres, what happens is that most of the layers are the same between those two applications or two versions of Postgres. So these layers will not be downloaded again. But only those layers that are different.


This means, now it will take around 10 or 15 minutes to download this 1 image, because I don't have any Postgres locally.

Downloading Image
Downloading Image

But if I were to download the next version, it would take less time, because some layers already exist on my local machine. ✅


Here you see that it's already logging:

Postgres application started within the container
Postgres application started within the container

The reason is that the command I ran here; the docker run container name and version, it not only fetches or pulls the container, but it also starts it. So it executes the start script right away as soon as it downloads it, and here you see the output of the starting of the application.


It gives some output about starting the server and doing some configuration stuff and you can see that the database system is ready to accept connections.


List all running containers

Let's open the new tab and with docker ps command, you can see all the running containers. So here you see that Postgres 9:6 is running and it actually says "image", right?

"docker ps" command
"docker ps" command

So this is another crucial thing to understand when discussing containers.


Two Technical Terms

There are two definitions or two technical terms "image" and "container" in Docker. A lot of people confuse those two terms, so here I want to give you a straightforward distinction between the two.


Image

The image is the actual package. The application package includes everything the program needs to work, like the settings and what it needs to run correctly. It is the artifact that is movable, which can be uploaded/downloaded to/from Docker repositories etc.


Container

The container is when I pull that image on my local machine and start it. So the application inside starts, creating the container environment.


If it's not running. It's an image. It's just an artifact that's lying around.
It is a container, if I start it and run it on my machine.

So that is the distinction.


So here it shows the active running containers with a container ID, image that it's running from and some entry commands that it executed and some other status information:

List of running containers
List of running containers

So this means that PostgreSQL is now running on my local machine.

Simple as that! 👍


Running multiple Docker containers of same application

If I would need another version of Postgres to run simultaneously on my local machine, I can just re-run the docker run command with the other version and it will download it from Docker Hub:

Another Version of Postgres Running
Another Version of Postgres Running

But as explained earlier, some parts of the image are already on my computer, so I don't need to download them again. It just fetches the ones that are different. It means it takes less time.


When you execute docker ps command again, you will see two Postgres containers with different versions running. Both of them are running and there is no conflict between them:

2 Postgres versions running
2 Postgres versions running

You can run any number of applications with different versions with no problems. 🚀


We will go through how to use those containers in your application and the port configuration in some other configuration stuff later in this blog when we do a deep dive. But this is just for you to get the first visual image of how docker containers work, how they look, and how easily you can start them on your local machine without implementing a specific version of the Postgres application. ✅


Watch this Docker Crash Course to learn everything about Docker to get started and use it in practice 🚀


 

You can learn more about Docker and other DevOps technologies on my Youtube channel 👏



Like, share and follow me 😍 for more content:


1 Comment


Laslo Keller
Laslo Keller
Oct 11, 2023

Docker containers are a game-changer in the world of software development and deployment. They provide a lightweight, portable, and efficient way to package applications and their dependencies, ensuring consistency across different environments. Understanding the distinction between containers and images is key to grasping the concept fully.


An image is like a snapshot of a container, a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. On the other hand, a container is an instance of an image, running as a process on a host machine.


The beauty of Docker containers lies in their ability to encapsulate applications, making them easy to deploy and scale.…


Like
bottom of page