What is Docker? What are Containers?

What is Docker? What are Containers?

To understand what Docker containers are, we have to understand what is Docker and why it is useful, then we will learn what a Docker Image is so that we can finally define what a container is.

Why Docker?

To learn the power of docker, demonstration is better than a thousand words. Here is a challenge for you. Open a new tab right now and try to install Redis on your system within 2 minutes.

I hope you tried to do that.

If you did, you should have seen how painful it was to install a simple lightweight program like Redis. Dealing with such tough situations in the world of software development is more common than you think, so I want you to go beyond your current limited experiences and trust me that this indeed is a very very common issue, and your future career will prove my point.

What if I told you that you can install Redis using Docker within 1 minute?

Yes, Docker solves this problem of installing any software on any system without worrying about the OS compatibility or the dependencies we need for that software to run and what versions to be installed and so on.

What is Docker?

Although most people believe that Docker is a program, but in reality Docker is an ecosystem around creating and running containers. Some of the tools inside the Docker ecosystem are:

  1. Docker Client (Docker CLI)

  2. Docker Server (Docker Daemon)

  3. Docker Machine

  4. Docker Images

  5. Docker Hub

  6. Docker Compose

How Does Docker Work?

The Docker CLI goes to Docker Hub, which is a public free repository of Docker images, and download a Docker Image - a system snapshot that contains all the dependencies to run a specific program.

A container is an instance of that Docker Image. In other words, it is a running program. What is special about this container is that it has an isolated set of hardware resources like RAM, networking and hard drive space.

Let's install Docker on our computers and get our hands dirty.

Go to docker.com and install Docker for your operating system.

When you install Docker on your system, you actually installed two tools:

  1. Docker Client

  2. Docker Server

Docker CLI is just a tool we interact with through the terminal by executing commands and the Docker CLI will talk to the Docker Server to create, delete, manage containers.

Now, run Docker after being installed and then open your terminal and run this command docker run hello-world

You will see a message like this:

Let me break down what is happening here.

  1. Docker Server looked at a Docker Image called hello-world locally in your Image Cache but it didn't find that image file because you have just installed Docker on your system.

  2. Docker Server reached out to Docker Hub and looked for the Docker Image and installed it in your image cache on your hard drive.

  3. Docker Server created a container (a running program instance) of that image and run it.

  4. Docker Server streamed the output of that program inside the hello-world image to the Docker CLI and you saw that text in ornage: Hello from Docker!.

So far, I didn't give any explanation of what a container really is. So let's do it right now.

Let's see how your programs run on your system without Docker.

Basically this diagram shows that all the processes or programs (Redis and PostgreSQL in this case) running on your system share the same hardware resources.

Now let's look at how those programs will run with Docker Containers.

You can see that there is a container for every program (Docker Image), and each container has its own group of hardware resources.

I can hear you saying "BUT I ONLY HAVE ONE HARD DRIVE".

Yes you do, but with containers, your hardware resources are segmented into groups and each group can be accessed by a single container. This is called Namespacing.

Does that sound like a.. virtual machine?

YES! But containers are NOT virtual machines, but they are similar. Please read this article: Containers are not VMs.

What's the benefit of having containers? Let's suppose that the version of Dependency #1 inside PostgreSQL image has conflicts with the version of Dependency #1 inside Redis image. How can you resolve that issue without containers? That's why Docker is useful. Also you saw previously how installing Redis locally without a Redis image is simply a pain in the neck and it's not as straightforward as it sounds.

That's when a container shines.

There is one thing I want to highlight here. I mentioned earlier that in Docker we have Namespacing to segment our hardware resources to be used by different containers. But the thing is Namespacing is a feature specific to Linux, not Windows, not MacOS. How come that Docker can create Namespaces?

The answer is simple. Docker runs a Linux virtual machine under the hood and all the containers are created inside that virtual machine and that virtual machine talks to your OS's Kernal to use your hardware resources.

Now it's time for the real fun!

💡
Run Redis on your system using Docker and let me know in the comments below!

That's it! I hope I was able to breakdown these confusing concepts and make them easily understood!

If you like my style of writing and explaining concepts, please consider subscribing to my YouTube channel here: