what i know about docker

Docker is one of the most fascinating tools I have ever seen. Even though I have used Docker, I never had a deep understanding of what it actually is.

So if you are someone who is new to Docker or trying to know more about it, this is a perfect place to start.

Let's learn about Docker. But before knowing what Docker is, we need to know...

Why Even Use Docker

You probably had a scenario where, when you are installing an application on your system, it needs some extra dependencies for it to run.

If not, let's say you want to install Spotify, and Spotify needs Python to be installed on your system. So now you need to download Python and set it up. You may need other additional dependencies too. This is such a drag.

Let's check another example.

You developed a program and now you want to share it with someone else. What will they do? First, they will get a copy of your program on their system, install all the necessary dependencies, and run it. This only works when they know how to set up your program. What if they don't? Or maybe you developed a program on Linux and they are using Windows or Mac. Now you have to provide accurate instructions based on their OS and help them install all the required dependencies. As said earlier, this is such a drag.

Here is where Docker comes in. With Docker, you just need to share the Docker image of your program and with a single command they will be able to run it.

This is what Docker helps to resolve. It helps to set up applications in any system. Yes, in any system. Let it be Windows, Mac, or Linux. Irrespective of the environment, Docker helps to set up applications with ease.

This feature enables portability of applications among different environments and platforms.

So you got an idea of why we use Docker. Now let us see...

What Is Docker

Docker is a platform that helps to run software in packages called containers.

Let's break it down.

Containers. What are they?

Containers are isolated programs with their own portion of system memory, network, storage, and other resources.

Think of it as putting the software and all its dependencies along with some portion of system resources like memory, storage, and network into a box and running it.

Container concept diagram

So Docker helps to create, manage, and run boxes like these with ease.

How does it do that? We will get into that later.

Before moving further, let us install Docker. You can install it by referring to the official documentation based on your system from here.

After installation, run the following command to verify the setup:

docker run hello-world

If the installation was a success, you will receive a welcome message from Docker.

Docker Client and Docker Server

Docker mainly consists of two parts: the Docker Client and the Docker Server.

The Docker Client is a tool that helps you communicate with the Docker Server. The Docker Server is responsible for creating, managing, and running images.

Image? Yes. Remember the box I mentioned above? An image is a shallow copy of that box. That is, an image is a snapshot of the application, its dependencies, its resources, and a startup command. This startup command is what the container runs on start.

You can create any number of containers from an image, each container with its own isolated space.

What Happens When You Run docker run hello-world

When you run this command, the Docker Client identifies it and interacts with the Docker Server.

The Docker Server first checks whether the hello-world image is already downloaded (located in the cache). If not, it downloads the image from Docker Hub.

Docker Hub is a cloud repository provided by Docker that allows users to store and share Docker images.

After successfully downloading the image, Docker creates and starts the container.

The command docker run hello-world is actually a combination of two other commands:

  • docker create 'image-name' creates a container from the image and returns the container ID.
  • docker start 'container-id' starts the created container.

docker create output

You can start the created container with docker start 'container-id'.

docker start output

It is not necessary to enter the entire ID. Docker is smart enough to understand when you provide just the starting portion.

To list all created and running containers:

docker ps --all

Without the --all flag, it will only show the running containers.

docker ps output

In short, you can run docker run 'image-name' to create and start a container in one step. When starting the container, it runs the startup command that was initialized during the creation of the image.

Now you understand what Docker is and why it is used. But...

How Does Docker Even Make It Happen

Docker makes it possible through containerization.

Docker achieves containerization through two features called namespaces and cgroups. These are features of the Linux kernel.

Namespacing is a method for isolating system resources per process. Cgroups limit the resources being used.

When we run two applications, let's say Spotify and Chrome, how do they work without conflicting with each other?

This is achieved using namespaces and cgroups.

When we run Spotify, it needs internet access, system memory, and storage. Similarly, Chrome needs internet access, some more memory (as you know), and storage. So how does Chrome not interrupt the data of Spotify, or vice versa, when used together?

This is where namespacing comes in. It isolates the processes of Spotify and Chrome separately and provides the necessary system resources such as memory, network, and storage in the form of a container, making sure they are not interfering with each other. On the other hand, cgroups limit and prioritize the system resources being used.

This is a top-level overview of how it runs. In actuality, it is much more complex but efficient.

Namespacing and cgroups diagram

This containerization technique is what Docker uses to run applications in containers efficiently without interfering with the host operating system or other containers on the same system. This allows for greater flexibility, scalability, and portability of applications.

But Namespaces and Cgroups Are Only on Linux...

Then how can Docker run on different environments?

When we install Docker on Mac or Windows, Docker is installed on top of a Linux virtual machine in our system. If you run docker version, you can see an output like this:

docker version output

Notice the output OS/Arch: linux, even on a Windows or Mac system.

Wrapping Up

Hope you got a clear picture of the what, why, and how of Docker.

There is much more to know, such as Docker commands, Dockerfiles, and creating and managing Docker images and containers.

If you liked this article, like, share, and provide feedback. Follow for more and hit me on Twitter.

Peace.