A Beginner's Guide to Docker

Starting Docker? here are some important things to know

A Beginner's Guide to Docker

Photo by Ian Taylor on Unsplash

If you have some experience with contributing to open source projects, there is very good chance that to set up the project in your local machine, Docker is required. So, what is it, why is it and how is it, I will cover this in the blog.

What is Docker?

Docker is an open platform that allows us to develop, ship, and run applications. It allows us to separate our application from our underlying infrastructure (our local machine), making it easier to deliver or scale. It achieves it via using containers.

What is a container?

Ever heard of Virtual Machines? I am 90% sure you have. Virtual Machines allow us to run multiple applications in a completely different environment. For example, WSL(Windows Subsystems for Linux) is a virtual machine that allows us to run the Linux kernel on Windows. Every process on this Linux kernel is isolated from Windows and vice versa. But it has a drawback. Each virtual machine has its own separate OS. Like WSL has Linux to run applications. What if we want to run several isolated applications on a single OS? That is what containers enable us to do.

Containers allow us to run applications in isolated environments on a single OS. Think of it like an actual container that we use to transport goods across oceans. It packs the applications with all resources it requires to run and isolates it from the underlying infra.

Coming back to Docker. With the help of this container, Docker allows us to develop, test & deploy applications as containers. This container has everything an application needs to run i.e. other applications.

Docker Architecture

Docker has a lot of things going on BTS to enable all this we discussed earlier. Now, let's talk about it.

Docker works as a client-server architecture. Here, Docker Daemon is the server & Docker Client is the Client (CLI/Docker Desktop). Both of these communicate through REST API.

Docker daemon (dockerd)

Docker daemon manages all the Docker objects, like containers, images, volumes, and networks. It also listens to API requests from CLI and performs actions as per them.

Docker client (Docker)

This is the primary way by which we will communicate with Docker. When you run a command, via API it send it to dockerd and it executes the required actions. Docker clients can communicate with other daemons too.

Docker Objects

Docker Images

Images are the read-only templates that act as a reference to form running containers. To run a Ubuntu container on your machine, you need to download the Ubuntu image and then you can run it on Docker.

Docker Containers

Containers are runnable instance to an image. For example, by using Ubuntu Image, you can create several instances or containers where you can run multiple applications in an isolated environments. You can start, stop, run, create, move or delete container. You can also create new images from containers.

Here is a good analogy. Consider images as class (as in OOPS) and containers as an object. If you are familiar with OOPS, I am sure you will get it.

Docker registry

If we need images to run containers, how de we get it? Either you can make it on your own or you can download or pull from Docker Registry. It is an online hub which hosts popular images like Ubuntu, MongoDB etc.

Dockerfile

As I mentioned above, we can make our own images. But how? There is a thing called Dockerfile. You might have seen it in repo of various projects. It is a file which has instructions on how to create a Docker image of that file. After you build the project based on instructions in DockerFile, your image is formed. So, the flows goes like

Basic Commands in Docker

In order to use and experiment with Docker (which you should), first you need to install it on your local machine. You can check out https://docs.docker.com/engine/install/ to know how to install Docker.

For windows, WSL 2 is required to install Docker, for LInux is to install the package directly via command line (easiest).

Now, here are some basic commands:

$ docker images

Obviously, it lists down current images you have on your local system.

$ docker pull Alpine

This pulls "Alpine" image from Docker registry. Similarly, "push" command pushes image to registry.

$ docker run -it Alpine

The run command starts the container from its image. If image is not present, then it automatically downloads or pulls it from docker registry and starts the container. Here, '-it' start container in interactive mode, so you can access the container from the CLI. To run container in the background, use '-d' flag (detach mode). Here, you can use Alpine from CLI.

$ docker ps

ps (process status) command shows current running containers. You can use '-a' flag after ps to show recent past processes too, that are exited now. For example after running first command and then ps command, we get

NOTE: Every container has its own unique container id. We will be using later on.

$ docker build

This builds an image using Dockerfile in current directory.

$ docker stop caf18635da95

"stop" stops the container. Note that I used container ID to stop the container, same as in the screenshot above.

$ docker rm caf18635da95

This will remove the container from Docker.

$ docker rmi 1d34ffeaf190

This will remove the image from Docker. To check your image's id, use "image" command mentioned above.

There are many others as well. However, let's ignore them for now.

This was a basic overview of Docker. Surely there is a lot more, but to get started, this is enough. In this era where shipping and making applications scalable is a challenge, Docker comes in handy. And this allows us to learn further about container orchestration tools like Kubernetes.

That's it.

Connect with me on LinkedIn and Twitter. And if you have suggestions, please leave a comment.

Have a good day!