Skip to main content

Command Palette

Search for a command to run...

Getting started with DOCKER Beginners Guide

Updated
10 min read
Getting started with DOCKER  
Beginners  Guide
V

Another tech enthusiast

what actually Docker is ?

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. This is what you may find in Google right? 😅. In real world, Docker is important to both the development community and container community because it made using containers soo easy that everyone started doing it. Before diving into Docker let us discuss about Containers:

What are containers ?

Containers or Linux Containers are technologies that allows us to isolate certain kernel processes and trick them into thinking they're the only ones running in a new computer.

Unlike Virtual Machines, containers can share the kernel of the operating system while only having their different binaries/libraries loaded with them.

In other words, you don't need to have whole different OS (called guest OS) installed inside your host OS. You can have several containers running within a single OS without having several different guest OS's installed.

image.png

Hope You got the difference between Virtual Machines and Containers.

This makes containers much smaller, faster, and more efficient. While a VM can take about a minute to spin up and can weigh several Gigabytes, a container weighs, on average, 400 to 600mb (the biggest ones) and you can save a lot of time, resources.

They also take only seconds to spin up. This is mostly because they don't have to spin a whole operating system before running the process.

Now you got clarity about Containers...So lets get into the DOCKER ⏩⏩

Prerequisites

  • Familiarity with the Linux Terminal.

DOCKER

History

Finally in 2010, Solomon Hykes and Sebastien Pahl created Docker during the Y Combinator startup incubator group. In 2011 the platform was launched.

Originally, Hykes started the Docker project in France as part of an internal project within dotCloud, a PaaS company that was shut down in 2016.

After 2013, several companies started adopting Docker as default container runtime because it standardized the use of containers worldwide. In 2013, Red Hat announced a Docker collaboration, in 2014 it was time for Microsoft, AWS, Stratoscale, and IBM.

In 2016, the first version of Docker for a different OS than Linux was announced. Windocks released a port of Docker's OSS project designed to run on Windows. And, by the end of the same year, Microsoft announced that Docker was now natively supported on Windows through Hyper-V.

In 2019, Microsoft announced the WSL2, which made possible for Docker to run on Windows without the need of a virtualized machine on Hyper-V. Docker is now natively multiplatform while still leveraging Linux's container approach.

Finally, in 2020, Docker became the worldwide choice for containers. This happened not necessarily because it's better than others, but because it unifies all the implementations under a single easy-to-use platform with a CLI and a Daemon. And it does all of this while using simple concepts that we'll explore in the next sections. {source : FreeCodeCamp}

How Docker Works ?

Docker packages an application and all its dependencies in a virtual container that can run on any Linux server. This is why we call them containers. Because they have all the necessary dependencies contained in a single piece of software. Docker is composed of the following elements:

  • a Daemon, which is used to build, run, and manage the containers.
  • a high-level API which allows the user to communicate with the Daemon.
  • a CLI, the interface we use to make this all available.

Docker Architecture Overview:

image.png

Docker Containers

As I mentioned above, A container is an isolated place where an application runs without affecting the rest of the system and without the system impacting the application. Because they are isolated, containers are well-suited for securely running software like databases or web applications that need access to sensitive resources without giving access to every user on the system.

Containers are abstractions of the app layer. They package all the code, libraries, and dependencies together. This makes it possible for multiple containers to run in the same host, so you can use that host's resources more efficiently.

Docker Images

Docker images are instructions written in a special file called a Docker file. It has its own syntax and defines what steps Docker will take to build your container. Images are read-only templates containing instructions for creating a container. A Docker image creates containers to run on the Docker platform.

Think of an image like a blueprint or snapshot of what will be in a container when it runs. An image is composed of multiple stacked layers.For example, to build a web server image, start with an image that includes Ubuntu Linux (a base OS). Then, add packages like Apache and PHP on top.

You can manually build images using a Dockerfile, a text document containing all the commands to create a Docker image. You can also pull images from a central repository called a registry, or from repositories like Docker Hub using the command docker pull [name]. Images are Immutable!.

Images contain the code or binary, runtimes, dependencies, and other filesystem objects to run an application. The image relies on the host operating system (OS) kernel. By this we can say Docker have a disadvantage at last.. i.e image built on particular OS kernel can't be executed on different OS kernel. For example, If an application is designed to run in docker windows container then same application can't run on Linux docker container.

conclusion [Docker Images vs Docker Containers]

A Docker image executes code in a Docker container. You add a writable layer of core functionalities on a Docker image to create a running container.

Think of a Docker container as a running image instance. You can create many containers from the same image, each with its own unique data and state.

Docker Commands

  • docker run – Runs a command in a new container.
  • docker start – Starts one or more stopped containers.
  • docker stop – Stops one or more running containers.
  • docker build – Builds an image from a Docker file.
  • docker pull – Pulls an image or a repository from a registry.
  • docker push – Pushes an image or a repository to a registry.
  • docker export – Exports a container’s filesystem as a tar archive.
  • docker exec – Runs a command in a run-time container.
  • docker search – Searches the Docker Hub for images.
  • docker attach – Attaches to a running container.
  • docker commit – Creates a new image from a container’s changes.

Check out the complete list of Docker commands in the Docker Documentation

What is a Docker Registry?

You've already learned about two very important pieces of the puzzle, Containers and Images. The final piece is the Registry.

An image registry is a centralized place where you can upload your images and can also download images created by others. Docker Hub is the default public registry for Docker. Another very popular image registry is Quay by Red Hat. You can share any number of public images on Docker Hub for free. People around the world will be able to download them and use them freely.

I 'll try to be in a practical way {pasting the execution and output} in my upcoming blogs.

What is Docker Compose?

Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.

Docker Compose is used for running multiple containers as a single service. Each of the containers here run in isolation but can interact with each other when required. Docker Compose files are very easy to write in a scripting language called YAML, which is an XML-based language that stands for Yet Another Markup Language. Another great thing about Docker Compose is that users can activate all the services (containers) using a single command.

For example:

If you have an application that requires an NGINX server and Redis database, you can create a Docker Compose file that can run both the containers as a service without the need to start each one separately.

Benefits of Docker Compose

  • Single host deployment - This means you can run everything on a single piece of hardware.
  • Quick and easy configuration - Due to YAML scripts.
  • High productivity - Docker Compose reduces the time it takes to perform tasks.
  • Security - All the containers are isolated from each other, reducing the threat landscape.

Now, you might be thinking that Docker Compose is quite similar to Docker Swarm, but that’s not the case

So What is Docker Swarm ?

In simple terms, A Docker Swarm is a group of either physical or virtual machines that are running the Docker application and that have been configured to join together in a cluster. The activities of the cluster are controlled by a swarm manager, and machines that have joined the cluster are referred to as nodes.

How Does Docker Swarm Work?

In Swarm, containers are launched using services. A service is a group of containers of the same image that enables the scaling of applications. Before you can deploy a service in Docker Swarm, you must have at least one node deployed.

There are two types of nodes in Docker Swarm:

  • Manager node.- Maintains cluster management tasks
  • Worker node. - Receives and executes tasks from the manager node

Consider a situation where a manager node sends out commands to different worker nodes.

manager-node.

The manager node knows the status of the worker nodes in a cluster, and the worker nodes accept tasks sent from the manager node. Every worker node has an agent that reports on the state of the node's tasks to the manager. This way, the manager node can maintain the desired state of the cluster.

The worker nodes communicate with the manager node using API over HTTP. In Docker Swarm, services can be deployed and accessed by any node of the same cluster. While creating a service, you'll have to specify which container image you're going to use. You can set up commands and services to be either global or replicated: a global service will run on every Swarm node, and on a replicated service, the manager node distributes tasks to worker nodes.

Now a question may arise: does the task and service refer to the same thing? The answer is no.

A service is a description of a task or the state, whereas the actual task is the work that needs to be done. Docker enables a user to create services that can start tasks. When you assign a task to a node, it can't be assigned to another node. It is possible to have multiple manager nodes within a Docker Swarm environment, but there will be only one primary manager node that gets elected by other manager nodes.

Therefore, the working of the Swarm can be summarized as follows:

managernode-workernode

A service is created based on the command-line interface. The API that we connect in our Swarm environment allows us to do orchestration by creating tasks for each service. The task allocation will enable us to allocate work to tasks via their IP address. The dispatcher and scheduler assign and instruct worker nodes to run a task. The Worker node connects to the manager node and checks for new tasks. The final stage is to execute the tasks that have been assigned from the manager node to the worker node.

image.png

Docker Swarm sounds similar to Kubernetes but there are many differences between them. For now this all about Docker and We will see Kubernetes in the next blog. I'll try to mention differences in it.

Here are some of the differences between Docker Compose and Docker Swarm:

image.png

Suggestions are Open.

Amazing Resource (if possible, give a try) : The Docker Hand-Book

Video Resources :

  1. KK's Docker Tutorial.
  2. KodeKloud's Tutorial highly recommended if you want hands-on practice.
  3. Tech world with Nana

Hope you got know a little about Docker . Happy Learning . For now 👋

Connect me at