Docker Basics: Beginner’s Introduction



This content originally appeared on DEV Community and was authored by Aashish Koshti

In this blog, I’ll walk you through Docker basics, architecture, and important concepts like Image, Containers with a simple & practical example.

History of Developing & Deploying application.

In past, we used to deploy applications on host system (a simple machine) & ran them directly on it.

A host machine has a fixed Operating system, softwares, libraries etc, installed on it and we were able to develop and deploy applications on them.

1

But there were certains issues like dependencies conflicts, software conflicts, data corruption etc. Imagine you are running two applications on the same machine both of them access the same database and one of them fails and corrupts the entire DB. Because of this we sought for new solutions.

Then came a concept of Virtualization, which helped managing and distributing the resources of a host system in much more efficient manner.

The idea of a Virtual Machine came. A Virtual Machine (VM) in simple terms can be understood as a small host system running inside another host system, which has its own Operating System, Kernel, binaries, applications etc.

By using VM, we were able to create multiple host systems inside our main host system and deployed our applications efficiently.

2

But as time passed, we realized that managing these VM were still a time consuming process. Imagine, your application accidently gets 1 million users traffic and now you need to scale it up. But to prepare a new VM on a host system will take time, because it’s a whole OS (just a smaller size) and setting it up takes a lot of time. Think of the time when you updated your windows, or installed linux on your machine.

Because of these downsides, a simple solution was needed.

And then came a new concept called Containers which Docker uses.

Containers proposed a new way of managing the application environments. Instead of all the bloated VM which contains their own OS, binaries, Kernels etc, Containers said, instead of dividing our host system to cater our application, why don’t we package our application and it’s dependencies together and then run them on the system.

3

This allowed developers to run as many packaged apps, called as Containers, on their host system efficiently. This overall improved the whole development to deployment cycle in many companies.

By using Containers, we were able to isolate the applications into their own individual environments. This solved the problem of shared resources between the applications as well as managing & deploying these containers were much easier than VM.

Docker Architecture.

Docker uses the philosophy of Containers to provide developers better experience of developing & deploying applications. It provides us a whole tooling for managing the lifecycle of our Containers.

Let’s look at the architecture diagram of Docker.

4

  • Docker Daemon listens and performs action on the API requests given to it by the client.

  • Docker Client provides CLI CMDs using which we can communicate with the Docker Daemon.

  • Docker Registries stores Docker images. Docker Hub is a public registry on which you can find multiple software images like golang, node js etc.

In most cases, we Developers don’t need to go in-depth about Docker architecture & how it’s build. We are more concerned about the core things which it provides us for working with containers.

Image, Containers & Registry.

Most important things we as Developers need to know about are Images, Containers & Registry. So, let’s look into them.

  • Images

An Image is a blueprint or a template which contains the necessary steps for creating a container.

To give an example, let’s take Burger. To make a burger, you take buns, lettuce, tomato, ketchup, patty, and place all of them in layers to form a burger. Similarly, to build a container, in which your application will run. You specify all the necessary steps inside an Image. These steps are also called as Layers.

To build a Image, we use DockerFile. Inside this file we write instructions. We’ll work with Images in more detail in upcoming blogs.

  • Containers

A Container on the other hand is a runnable instance of an Image. Using the Image as a template, you create the Container. You can create, start, stop, etc. and perform multiple actions on it.

Containers are isolated, meaning they don’t interfere with other Containers running on the same host system. We can also provide configuration options as per our requirements if we want multiple Containers to work together. We’ll work with Containers in more detail in upcoming blogs.

  • Registry

A Registry is a place which stores Images. Docker Hub is public registry on which you can find many images for different softwares.

If you want to run a software on your machine, instead of installing it, you get it’s Image from the public registry and run it as a Container on your machine. This removes the overhead of installing softwares on our machine.

Some CLI CMDs & Practical Example.

Now, let’s look at some of the commands which Docker provides us using a simple example. By the way, follow these instructions for installing Docker on your system. Official Installation Docs.

Suppose you want to run a simple Nginx web server using Docker. Here are the basic steps:

  • docker pull

This command downloads an image from Docker Hub to your local machine.

docker pull nginx

5

  • docker images

Lists all Docker images available on your local machine.

docker images

6

  • docker run

Runs a container from the specified image. The following command starts an Nginx container and maps port 8080 on your machine to port 80 in the container.

docker run -d -p 8080:80 --name mynginx nginx

7

  • -d runs the container in detached mode (in the background).

  • -p 8080:80 maps port 8080 on your host to port 80 in the container.

  • --name mynginx gives your container a name.

  • docker ps

Lists all running containers.

docker ps

8

Now, if you open your browser and go to http://localhost:8080, you should see the default Nginx welcome page running from your Docker container.

9

Summary.

This was a very basic introduction to Docker, but in upcoming blogs I’m going to cover a lots of important topics like Building efficient Images, Docker Networking, Storage, Multi Stage builds, CI/CD Actions and many more.

So, keep an eye on this series Docker In Depth.

Conclusion.

Thanks for reading it, and if you liked the blog, please consider following me on my socials.


This content originally appeared on DEV Community and was authored by Aashish Koshti