How Does Docker Work?
Tags: docker, devops
Published: 2021-06-19 03:32:47

## What is Docker?

Docker is a containerization tool that enables developers to easily pack, ship, deploy and run their applications.

If your application requires the installation of Node.js, Express, MongoDB, and Redis, you have to manually install these on your machine. Depending on the OS, this can get tedious and many things can go wrong. 

However, with Docker, all those configurations, services, dependencies are installed and all you have to do is run the image in a container. You no longer have to directly install anything yourself.  

## Image vs Container

*Image*
<ul>
    <li> File that holds the source code, configurations, dependencies, tools, etc needed to actually run the program </li>

    <li> This is the actual package/artifact that can be moved around. </li>
</ul>

*Container*
<ul>
    <li> A virtualized environment created during runtime that actually runs the image. </li>
    <li> The container actually runs the image in its own virtual environment. </li>
</ul>

<hr/>

## Docker vs Virtual Machine

To understand Docker, we need to understand the Operating System and it's layers. 

![Docker Whale Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbcspm0ipeqmp1q0bqm7.png)

The application layer is everything you interact with. This would include any desktop applications, IDE's, internet, etc. This application layer runs on top of the Kernel. 

This OS Kernel handles device management, memory management, resource management, and accesses computer resources. It communicates with the hardware that includes the RAM, CPU, etc. 
  
*Docker*
<ul>
    <li> With Docker, the application layer is virtualized.
    <li> Docker Containers run/start faster! 
    <li> Images are smaller. </li>
</ul>

*Virtual Machine*
<ul>
    <li> A VM virtualizes both the application layer AND Operating System Kernel. </li>
    <li> A VM can run an operating system on any host machine. However, with Docker, you can't. </li>
</ul>

<hr/>

## Docker Engine

<ul>
    <li> When you install Docker, you install the Docker Engine under the hood. </li>
</ul>

The Docker Engine is comprised of three components:

1. **Server**
    - Responsible for pulling images
    - Responsible for managing images and containers
2. **API** 
    - Responsible for interacting with docker server
3. **CLI**
    - This is the client that is responsible for executing 
      docker commands
<hr/>

## Docker's Architecture

<ul>
    <li> Docker uses a client-server architecture. </li>
    <li> Docker's client (CLI) talks to the Docker 
         server(daemon) which does the heavy lifting of 
         building, running, distributing your containers. 
    </li>
    <li> When we talk about the daemon, it is simply a process called dockerd running in the background waiting for any requests by the API </li> 
    <li> The docker daemon (dockerd) listens for Docker API requests and manages docker objects(images, containers, networks, and volumes).</li>
    <li> A daemon can also communicate with other daemons to manage the services. </li>
</ul>

<hr/>

## Docker Server's Responsibilities:
1. **Container Runtime**
   - Responsible for pulling images and managing container 
     life-cycle.
2. **Volumes**
   - Responsible for persisting data in containers when a 
     container stops running or something goes bad. 
3. **Network**
   - Responsible for configuring networks for container 
     communication
4. **The functionality of building docker images**.

<hr/>