Intro to Docker

This article is a beginner's guide to Docker and its commands

·

6 min read

Intro to Docker

Before Containers

Let us dial back the clock. Back when computers were a novelty, and companies wanted to run their applications, they would have had to buy a server for doing so. However, this came with a caveat: you could only run one application per server. If you wanted to run multiple applications, you would have to buy many servers. As costs would skyrocket, this was a problem, for both the company and the environment on a larger scale

IBM fixed this issue by introducing the concept of virtual machines into the game. With them, we could run multiple applications on the same server. You may have done dual booting on your PC; like installing windows on your Mac device or Ubuntu on Windows. These are virtual machines. However, they had a problem as well. They needed their own operating system, which tended to take up a lot of memory and storage on your hard disk. This made them slow and not so efficient.

In came our Messiah Containers.

What is Containers....???

containerexample.jpeg

Imagine you are moving to Scotland from India. Would it make sense to send your belongings one by one, or would you rather put them all in one giant box and ship them directly? Obviously the latter, right?

That big box is, in simple terms, a container.

Let’s take this same analogy, and say you built a website that works well on your system but your friend runs into some problems with it when they try using it on their computer. To avoid such hassles, we can use a container, which would ship the entire website along with its dependencies, such as the web database, front end, back end, source code, etc. This would ensure that the website runs smoothly on every device.

In computing terms, containerization is an efficient method for running, deploying, and scaling applications.

Containers V/S Virtual Machines (VMs)

ContainerVSVirtual.avif

Any application which runs on a virtual machine will require a guest OS to run, which would require a hypervisor. A hypervisor manages the virtual machines and is used to create multiple machines on the host operating system. As you can see from the picture, every OS would require a dedicated amount of space in the hardware, which is virtually divided.

In the case of containers, you need to have only one operating system, and on top of that, you have a container engine, on which you run applications. They use the idea of isolating your application from the main operating system.

To sum it up simply, virtual machines use multiple operating systems to run multiple applications whereas containers use only the host operating system to run applications via the container engine.

In reality, however, containers run on top of virtual machines. You can say they are a lightweight alternative.

So what is Docker?

Docker is one among many container platforms that allows you to build such containers to test, build and scale applications rapidly and easily by running them in isolated environments.

this brings us to our next point.

Why use Docker?

  • Helps transport code faster
  • Makes scaling, deploying, and identifying issues in the application easier
  • Saves up space as you don’t need to install the whole application locally

Let's take a moment to familiarize ourselves with some little bit Docker Terminologies which will make our life easier

Docker Terminologies

Docker Runtime

It allows us to start and stop containers. There are two types:

  • Low-level runtime (runc)
  • High-level runtime (containerd)

Docker Client

It is what we use to interact with Docker. You can think of it as the user interface for Docker. Whenever you type in a command, the client sends these to the daemon.

Docker CLI

It allows users to issue commands to the Docker Daemon. Docker uses a client-server architecture

ServerClient.avif

Docker Engine

It is responsible for the overall functioning of the Docker platform. It is a client-server-based application with three components:

  • Server – which runs the daemon
  • Rest API – deals with the interaction of applications with their server
  • Client – which is nothing but the command line interface (CLI)

Docker Daemon

It is the heart of the Docker architecture, which does the crucial work of building, running, and distributing the containers. It also manages the Docker images and the containers.

Docker Image

This file contains the source code, and operating system files to run the application along with its other dependencies inside the container is called Docker image. A containerized application is the running instance of an image. Docker images are immutable.

Dockerfile

It contains the list of instructions to create Docker images.

Docker Registries

This is where Docker images are stored. Docker hub is the official online public repository of Docker where you can find all the images of popular applications. Docker hub also allows us to create our own images for our application.

Getting Started with our Docker commands

But before that...

Installing Docker

You can refer to the official documentation to get started.

If you want to skip installation, you can head to Play With Docker, an interactive online playground for Docker.

Now back to some Hands-On :)

docker run

DemoDocker.avif

This command first creates the container, and then it starts the container. Before creating the container, it will first check if the latest official image of hello-world is available on the Docker Host (the local machine on which the docker engine is running). If not, like in the case above, it will automatically download the same from Docker Hub before creating the container.

run keyword here means you are running an image to create the container, and hello-world is the image name.

docker images

This command lists all the Docker images that are present in the Docker Host.

ob1AOOp41.avif

Let's decode each and every term used here:

  1. REPOSITORY - This shows the name of the docker image.
  2. TAG - This basically represents the version of the image being used. Each image has its own unique tag.
  3. IMAGE ID - a string consisting of alphanumeric characters which are associated with every image.
  4. CREATED - This depicts when the image was created
  5. SIZE - This represents the size of the image
docker ps

It allows us to view all the containers that are running in the Docker Host.

cPiZ34uVR.avif

docker inspect  [image_name]

It gives information on the image

docker container prune -f

It will remove all the inactive (i.e. stopped) containers from Docker.

DTafHwlVg.avif

docker rmi [image_name]

It allows us to remove an image(s) from the Host

a_wAV8xtG.avif

To remove all the images at once, we rewrite the rmi command in the following manner:

docker rmi $(docker images -q)

multipleremove.avif

Thank you so much for taking your valuable time for reading :)