# Lecture 04: Docker Fundamentals

### Why Docker?

Docker emerged to solve the common problem of applications running smoothly on a developer's machine but failing to execute on a client’s machine. This issue often arises when the client’s environment lacks the necessary tools, libraries, languages, or their required versions.

Before Docker, VirtualBox (a hypervisor) was used to address this issue. However, Docker introduced advanced technologies that revolutionized the tech industry by optimizing resource usage.

While hypervisors utilize double the resources, Docker minimizes this problem by allowing containerization.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729780725443/c5ba0f61-0015-47a4-b558-ed802f235318.png align="center")

---

# Docker

Docker is a containerization tool that enables the creation of a virtualization environment to run any application on any operating system.

### Containerization

To minimize costs and avoid resource duplication, Docker employs containerization, which allows shared resources, leading to reduced costs.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729780750264/87bb6a4e-c4e1-470c-b007-807bf4bd75c8.png align="center")

1. **Docker Engine**: Comprises three components to facilitate resource and application containerization:
    
    * **Docker Daemon**: A background process that runs on the system.
        
    * **Docker CLI**: Provides a command-line interface to control the Docker Daemon.
        
    * **Container D**: A CNCF (Cloud Native Computing Foundation) project that creates a virtual environment to run applications; it is an open-source tool. For example, if your application requires a Linux environment, Container D creates that environment.
        
2. Docker containers do not include an operating system kernel; they utilize the kernel of the local machine to run applications through the Docker Engine, meaning applications run directly on the system's kernel.
    

### Difference between Virtualization and Containerization

| **Virtualization** | **Containerization** |
| --- | --- |
| Uses VirtualBox | Uses Docker |
| Allocates dedicated resources | Allocates shared resources |
| Higher cost | Lower cost |
| Utilizes a hypervisor to translate operating system ISO images to run in a virtual environment | Uses Docker Engine for containerization |
| Comes with an operating system kernel | Does not include an operating system kernel |
| Larger ISO image size (includes kernel) | Smaller Docker image size (uses local system kernel) |

---

# Key Concepts in Docker

1. **Dockerfile**: A blueprint outlining the steps required to achieve a desired result.
    
2. **Docker Image**: The final image created from the blueprint, which aids in creating Docker containers.
    
3. **Docker Container**: A storage entity for images that can run any operating system.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729780786810/373d9bcb-c5d6-4f8f-bbfa-d020b2254da0.png align="center")
    

---

## Create a Docker Hub Account

You can create a Docker Hub account [here](https://hub.docker.com/).

### Docker Desktop Installation

Use this link to download Docker Desktop according to your operating system: [Docker Desktop](https://www.docker.com/products/docker-desktop/).

After downloading, log in with the account you created on Docker Hub.

### Basic Docker Commands

1. `docker --version`: Check the version of Docker or verify if Docker is installed.
    
2. `docker pull <image_name>:latest`: Download images.
    
3. `docker run <image_name>:<image_id>`: Convert an image into a container.
    
4. `docker ps`: List running containers.
    
5. `docker run -d <image_name>:<image_id>`: Run a container in detached mode (background).
    
    * For example: `docker run -d ubuntu:latest` creates an Ubuntu EC2 server.
        
6. If a container is exited, use `docker run -dit <image_name>:<image_id>` for interactive mode.
    
7. `docker ps -a`: List all containers (running and stopped).
    
8. `docker images`: View all Docker images available on the system.
    
9. `docker kill <container_id>`: Kill a running container.
    
10. `docker rm <container_id>`: Remove a container.
    

---

# CLI Instructions

1. Create an EC2 instance and connect via SSH.
    
2. Run `sudo apt update`.
    
3. Install Docker with `sudo apt install` [`docker.io`](http://docker.io).
    
4. Check Docker version with `docker --version`.
    
5. Add the Ubuntu user to the Docker group with:
    
    ```bash
    sudo usermod -aG docker ubuntu && newgrp docker
    ```
    
    * This step is crucial to access Docker CLI commands and avoid permission denied errors.
        

---

# Installing MySQL on EC2

1. Pull the latest MySQL image: `docker pull mysql:latest`.
    
2. Check all Docker images: `docker images`.
    
3. Create a Docker container with MySQL using:
    
    ```bash
    docker run -d -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
    ```
    
4. Check running containers: `docker ps`.
    
5. Enter the created MySQL container:
    
    ```bash
    docker exec -it <container_id> bash
    ```
    
6. Log into MySQL:
    
    ```bash
    mysql -u root -p
    ```
    

---

# Running Nginx through Docker

1. Remove any existing Nginx server: `sudo apt remove nginx`.
    
2. Run Nginx in Docker:
    
    ```bash
    docker run -d -p 80:80 nginx:latest
    ```
    
    * The `-p 80:80` flag maps Docker's port 80 to the host's port 80 to access the Nginx webpage.
        
3. Visit your server's public IP in a browser (e.g., `http://<your_public_ip>:80`).
    

---

# Creating a Dockerfile for a Java Application

Learn to create a Dockerfile for a simple Java application [here](https://github.com/Amitabh-DevOps/simple-java-docker).

### Dockerfile for Running the Java Application

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729780856855/d95c1e88-0d23-4ddd-a805-95c423b53bb2.png align="center")

1. Initially, create a blueprint with comments (#) indicating the requirements.
    
2. Set a **Base Image** (OS) using:
    
    ```dockerfile
    FROM openjdk:17-jdk-alpine
    ```
    
3. Create a working directory:
    
    ```dockerfile
    WORKDIR /app
    ```
    
4. Copy the source code to the destination directory:
    
    ```dockerfile
    COPY src/Main.java /app/Main.java
    ```
    
5. Compile the Java application:
    
    ```dockerfile
    RUN javac Main.java
    ```
    
6. Define the command to run the Java application:
    
    ```dockerfile
    CMD ["java", "Main"]
    ```
    
7. Save the Dockerfile.
    
8. Build the Docker image:
    
    ```bash
    docker build -t java-app .
    ```
    
9. Create and run the container:
    
    ```bash
    docker run java-app:latest
    ```
    
10. The output will display the date.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729780892538/d0a44b6a-2c5d-4018-a2eb-43968c209413.png align="center")
