Lecture 04: Docker Fundamentals

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.


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.

  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

VirtualizationContainerization
Uses VirtualBoxUses Docker
Allocates dedicated resourcesAllocates shared resources
Higher costLower cost
Utilizes a hypervisor to translate operating system ISO images to run in a virtual environmentUses Docker Engine for containerization
Comes with an operating system kernelDoes 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.


Create a Docker Hub Account

You can create a Docker Hub account here.

Docker Desktop Installation

Use this link to download Docker Desktop according to your operating system: 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.

  4. Check Docker version with docker --version.

  5. Add the Ubuntu user to the Docker group with:

     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:

     docker run -d -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
    
  4. Check running containers: docker ps.

  5. Enter the created MySQL container:

     docker exec -it <container_id> bash
    
  6. Log into MySQL:

     mysql -u root -p
    

Running Nginx through Docker

  1. Remove any existing Nginx server: sudo apt remove nginx.

  2. Run Nginx in Docker:

     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.

Dockerfile for Running the Java Application

  1. Initially, create a blueprint with comments (#) indicating the requirements.

  2. Set a Base Image (OS) using:

     FROM openjdk:17-jdk-alpine
    
  3. Create a working directory:

     WORKDIR /app
    
  4. Copy the source code to the destination directory:

     COPY src/Main.java /app/Main.java
    
  5. Compile the Java application:

     RUN javac Main.java
    
  6. Define the command to run the Java application:

     CMD ["java", "Main"]
    
  7. Save the Dockerfile.

  8. Build the Docker image:

     docker build -t java-app .
    
  9. Create and run the container:

     docker run java-app:latest
    
  10. The output will display the date.