# Getting Started with Docker

#### The Evolution of Computing Environments

In the early days, traditional servers were set up with **one server running one application**. This approach was inefficient because server resources were often **underutilized**, leading to wasted capacity. **Security** was also a concern, as multiple applications shared the same operating system, making it easier for attackers to exploit **vulnerabilities**. Additionally, these servers had **long boot times** and overall poor performance.

With the advent of virtualization, efficiency improved. Virtual machines (VMs) allow **multiple virtual servers to run on a single physical server**, each with its **own operating system**. This setup **enhanced security** and provided **isolated environments** for different applications on the same hardware. Despite these benefits, virtualization still had drawbacks, such as **high resource consumption** and the inability of one VM to use unused resources from another. Boot times improved but were not yet optimal.

![](https://k21academy.com/wp-content/uploads/2020/11/PhysicalHostvsVM_Diagram-min_result-1.webp align="left")

Containers took efficiency a step further. Unlike VMs, containers share the **host operating system's kernel**, making them much **lighter and faster**. They provide isolated environments for applications without requiring a full operating system for each container. This leads to significantly **reduced resource consumption**, \*\*faster bootup times (\*\*often in seconds), and improved scalability. Containers are highly **portable, lightweight** (with a small package of the OS), and make better use of resources since unused resources from one container can be allocated to others. They also **enhance security** and allow for **effective resource management** by limiting and reallocating resources as needed. Containers quickly became the preferred choice for **scalable, efficient, and portable** deployments.

![](https://k21academy.com/wp-content/uploads/2020/11/Docker-and-Vm-blog-image_result-1.webp align="left")

### **What is Docker?**

Docker is an open-source platform that automates the deployment of applications in lightweight, portable containers. It allows developers to package an application and its dependencies into a single container that can run consistently across different environments, whether it’s on a developer's local machine, in a data center, or in the cloud. With Docker, applications become more scalable, efficient, and easy to manage, thanks to its speed, portability, and isolated environments.

![What is Docker — explained. Docker has transformed software… | by Logik  Labs | Medium](https://miro.medium.com/v2/resize:fit:968/1*yc16sxwgPeOCHI8q46w5wA.png align="left")

### **Why Use Docker?**

* **Consistency Across Environments:** Docker ensures your application runs the same way in development, testing, and production by bundling all dependencies and configurations together.
    
* **Isolation:** Containers run applications in isolated environments, preventing conflicts between different applications or services on the same host.
    
* **Portability:** Docker containers can run on any system that supports Docker, making it easy to move applications between different environments or cloud providers.
    
* **Efficiency:** Containers are lightweight and share the host OS kernel, which makes them faster and more resource-efficient compared to traditional virtual machines.
    
* **Scalability:** Docker simplifies scaling applications up or down by allowing you to quickly start or stop containers as needed.
    

### Docker Architecture Overview

Docker Architecture is based on a client-server model, which consists of the following key components:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724667266541/5c4ec456-52e7-42e4-8039-68fe88e4f644.png?auto=compress,format&format=webp align="left")

### 1\. **Docker Client**

The Docker client is the main interface that users interact with. When you type commands like `docker build` or `docker run`, the client sends those commands to the Docker Daemon for execution. It's like the user's gateway to the Docker system.

### 2\. **Docker Daemon (dockerd)**

The Docker Daemon is the heart of Docker. It runs in the background and does all the heavy work. It builds, runs, and manages containers based on the commands it receives from the Docker client. The daemon also manages Docker objects such as images, containers, networks, and volumes.

### 3\. **Docker Engine**

The Docker Engine includes both the client and the daemon. It is the core component of Docker that creates and runs containers.

### 4\. **Docker Registries**

Docker registries are where Docker images are stored. The most well-known public registry is **Docker Hub**, but private registries can also be used. When you pull an image, Docker first looks for it in the registry and then stores it locally on your machine.

### 5\. **Images**

A Docker Image is essentially a blueprint for creating Docker containers. It contains everything needed to run an application, including the code, runtime, libraries, and dependencies. Think of it like a snapshot of a virtual machine but more lightweight and efficient.

When you run an image, it creates a container. Since Docker Images are immutable, once they’re built, they remain unchanged, ensuring consistency across different environments. This means that a container created from an image will run the same way regardless of the Docker environment or OS it’s on, as long as the underlying host OS supports Docker.

Docker images can be stored either locally or in remote registries like **Docker Hub**, allowing them to be easily shared and reused. Docker can build images automatically by reading instructions from a Docker file. A single image can be used to create multiple containers.

### 6\. **Containers**

A Docker container is a running instance of a Docker image. It's the environment where your application actually runs. Containers are lightweight and portable, allowing you to easily manage them using the Docker CLI or API. You can connect containers to networks, attach persistent storage, or even create new images from a container’s current state.

However, by default, containers are **ephemeral**, meaning once a container is deleted, all data inside it is lost. This is because containers are temporary and start fresh each time they run While this is useful for testing and development (as it allows a fresh state each time), it's not ideal for data you want to keep. To ensure data persists across container restarts, you need to use **volumes**, which allow you to store data outside of the container itself.

7. ### **Dockerfile**
    

A **Dockerfile** is like a recipe that lists all the ingredients and steps needed to create a Docker image. This image can then be pulled from Docker Hub, a public repository, to create containers in any environment. Once an image is built, it is stored in Docker Hub. When you run a Docker image, it creates a container that includes the application code and all the dependencies needed to run the app.

![Docker - Hub](https://www.tutorialspoint.com/docker/images/docker_hub_1.jpg align="left")

Here’s how to create a Docker image:

1. Create a file called **Dockerfile**.
    
2. Docker will automatically look for this file when building an image. To build the image, run the command:
    
    ```bash
    docker build -t myimage:v1 .
    ```
    
3. During the build process, any commands in the **RUN** section of the Dockerfile will be executed.
    
4. To run the built image as a container, use:
    
    ```bash
    docker run <ImageID>
    ```
    
5. The commands in the **CMD** section of the Dockerfile will execute when the container is created from the image.
    

**How Docker Works:**

* The **Docker client** sends a request to the **Docker daemon**.
    
* The daemon builds, runs, or manages containers based on those requests.
    
* Images can be pulled from or pushed to a **Docker registry**.
    
* Containers are created from images and can run your applications in isolated environments.
    

### Benefits of Docker:

* **Simplified Development**: Developers can focus on writing code without dealing with environment inconsistencies.
    
* **Faster Deployment**: The same container image used in development can be deployed to production, reducing deployment issues.
    
* **Scalability**: Docker makes it easy to scale applications, especially in a microservices architecture, by quickly adding or removing containers.
    

**Components of Docker:**

![Nikki Siapno on LinkedIn: The Main Components of Docker Explained. Building  and deploying… | 24 comments](https://media.licdn.com/dms/image/v2/D5622AQHnDJ5txsONVQ/feedshare-shrink_800/feedshare-shrink_800/0/1699354998791?e=2147483647&v=beta&t=mwJ-IU394lyQk_7cQ7Q9B4gq92LgH4QYrI0jNZx8exI align="left")

### **Docker Compose**

**Docker Compose** is a tool that allows you to define and manage multi-container Docker applications. Instead of running containers one by one, Compose uses a YAML file (`docker-compose.yml`) to specify how your services (containers) should run together. You can use a single command to start or stop multiple containers, making it easier to manage applications with several components, such as databases, web servers, and backends.

### **Comparison: Virtual Machines (VMs) vs. Containers**

| **Aspect** | **Virtual Machines (VMs)** | **Containers** |
| --- | --- | --- |

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Isolation</strong></p></td><td colspan="1" rowspan="1"><p>Provides strong isolation with separate OS instances.</p></td><td colspan="1" rowspan="1"><p>Provides lightweight isolation by sharing the host OS kernel.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Resource Overhead</strong></p></td><td colspan="1" rowspan="1"><p>Requires additional resources for each OS, including memory and CPU.</p></td><td colspan="1" rowspan="1"><p>More efficient with minimal overhead, sharing the host OS.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Startup Time</strong></p></td><td colspan="1" rowspan="1"><p>Slower to start due to booting the full OS.</p></td><td colspan="1" rowspan="1"><p>Fast to start as they use the existing OS kernel.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Storage Requirements</strong></p></td><td colspan="1" rowspan="1"><p>Requires separate disk space for each VM's OS and data.</p></td><td colspan="1" rowspan="1"><p>Uses less disk space as containers share the host OS.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Portability</strong></p></td><td colspan="1" rowspan="1"><p>Less portable as VMs depend on the hypervisor and specific OS.</p></td><td colspan="1" rowspan="1"><p>Highly portable across different environments with Docker.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Performance</strong></p></td><td colspan="1" rowspan="1"><p>Potentially lower performance due to virtualization overhead.</p></td><td colspan="1" rowspan="1"><p>Generally better performance due to less overhead.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Management Complexity</strong></p></td><td colspan="1" rowspan="1"><p>More complex to manage due to separate OS and VMs.</p></td><td colspan="1" rowspan="1"><p>Simpler management with container orchestration tools like Kubernetes.</p></td></tr></tbody></table>

<table><tbody><tr><td colspan="1" rowspan="1"><p><strong>Use Cases</strong></p></td><td colspan="1" rowspan="1"><p>Suitable for running multiple OS environments or legacy applications.</p></td><td colspan="1" rowspan="1"><p>Ideal for microservices, scalable applications, and CI/CD pipelines.</p></td></tr></tbody></table>

### Docker: Ensuring Consistency Across Environments.

In DevOps, Docker plays a crucial role in solving environment consistency issues. For instance, if a developer's code works on their machine but fails during testing due to differences in system configurations or package managers, Docker ensures that the application runs the same way everywhere. By containerizing the application with all its dependencies, Docker eliminates the "works on my machine" problem, providing a reliable and reproducible environment for development, testing, and production.

![How to run your DevOps using Docker containers | by Bishoy Labib |  Microservices for .NET Developers | Medium](https://miro.medium.com/v2/resize:fit:750/1*ptq5TVMy6ur_pz5YegPl7A.png align="left")

### Key Takeaways

🔹 **Docker's Evolution**: From traditional servers and virtual machines to containers, Docker represents a major leap in efficiency and resource management. Containers have revolutionized how we deploy and manage applications.

🔹 **Efficiency and Portability**: Docker containers are lighter, faster, and more resource-efficient than VMs. They enable consistent application behavior across various environments and simplify scaling.

🔹 **Understanding Docker**: Docker packages applications into isolated containers, ensuring that they run consistently regardless of the underlying infrastructure. This isolation minimizes conflicts and enhances security.

🔹 **Docker Architecture**: Familiarize yourself with Docker’s core components—Docker Client, Docker Daemon, Docker Engine, Docker Registries, Images, and Containers—to effectively manage and deploy applications.

🔹 **Docker Compose**: Simplify the management of multi-container applications using Docker Compose. It allows you to define and orchestrate complex setups with ease.

🔹 **Role in DevOps**: Docker plays a critical role in DevOps by addressing environment consistency issues, enabling seamless integration into CI/CD pipelines, and facilitating reliable and reproducible application deployment.

🚀 Embrace Docker to streamline your development workflow and enhance application deployment efficiency. Stay tuned for more insights and advanced topics in Docker and DevOps!
