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.
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.
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
Dockerfile: A blueprint outlining the steps required to achieve a desired result.
Docker Image: The final image created from the blueprint, which aids in creating Docker containers.
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
docker --version
: Check the version of Docker or verify if Docker is installed.docker pull <image_name>:latest
: Download images.docker run <image_name>:<image_id>
: Convert an image into a container.docker ps
: List running containers.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.
- For example:
If a container is exited, use
docker run -dit <image_name>:<image_id>
for interactive mode.docker ps -a
: List all containers (running and stopped).docker images
: View all Docker images available on the system.docker kill <container_id>
: Kill a running container.docker rm <container_id>
: Remove a container.
CLI Instructions
Create an EC2 instance and connect via SSH.
Run
sudo apt update
.Install Docker with
sudo apt install
docker.io
.Check Docker version with
docker --version
.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
Pull the latest MySQL image:
docker pull mysql:latest
.Check all Docker images:
docker images
.Create a Docker container with MySQL using:
docker run -d -e MYSQL_ROOT_PASSWORD=test@123 mysql:latest
Check running containers:
docker ps
.Enter the created MySQL container:
docker exec -it <container_id> bash
Log into MySQL:
mysql -u root -p
Running Nginx through Docker
Remove any existing Nginx server:
sudo apt remove nginx
.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.
- The
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
Initially, create a blueprint with comments (#) indicating the requirements.
Set a Base Image (OS) using:
FROM openjdk:17-jdk-alpine
Create a working directory:
WORKDIR /app
Copy the source code to the destination directory:
COPY src/Main.java /app/Main.java
Compile the Java application:
RUN javac Main.java
Define the command to run the Java application:
CMD ["java", "Main"]
Save the Dockerfile.
Build the Docker image:
docker build -t java-app .
Create and run the container:
docker run java-app:latest
The output will display the date.