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:
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:
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:
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:
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":
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.
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.
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:
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?
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.
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.
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:
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:
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:
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: