Day 18: Mastering Docker

Adrian Rubico

|

Dec 11, 2024

10:43 PM GMT+8

Containers have revolutionized how we develop, deploy, and manage applications by offering lightweight, portable environments that encapsulate software and its dependencies. DevOps practices are core to Docker's success as the industry leader in containerization.

What is Container?

  • Lightweight and standalone: Containers package code, runtime, libraries, and system tools to run applications.
  • Efficient: They share the host OS's kernel, using fewer resources than virtual machines.
  • Consistent: Ensure applications run the same across development, testing, and production.
  • Focus on applications: Simplify building, deploying, and managing applications without environment-specific issues.

Virtual Machine vs Container

Below is a comprehensive list of Python libraries frequently used in DevOps for various tasks such as automation, configuration management, testing, and cloud integration:

FeatureDescriptionContainer
DefinitionA virtualized hardware environment that emulates a physical machineA standardized software unit that packages code and its dependencies, allowing it to run consistently across different environments
Operating SystemEach VM has its own OSContainers share the host OS kernel
Resource AllocationVMs have dedicated hardware resourcesContainers share the host's hardware resources
Boot TimeSlower boot time due to OS loadingFaster boot time as they don't need to boot an OS
PortabilityLess portable as they require specific hardware and OS configurationsHighly portable, can run on any system with a compatible container runtime
IsolationStrong isolation, each VM is independentLess isolation, containers rely on the host OS for security
Resource OverheadHigher resource overhead due to the full OSLower resource overhead as they share the host OS
Use CasesIdeal for legacy applications, complex software stacks, and hardware-specific requirementsWell-suited for microservices architecture, cloud-native applications, and rapid deployment

What is Docker?

Docker is an open-source platform designed to simplify application development, deployment, and management using containerization technology. It allows developers to package applications and their dependencies into a lightweight, portable container that can run consistently across different environments.

Docker Engine

The Docker Engine is the core component of Docker, responsible for building, running, and managing containers. It acts as a lightweight runtime and ensures containers operate efficiently by abstracting the underlying operating system.

Docker Engine consists of three main components:

  • Docker Daemon (dockerd): A background service that manages Docker objects such as images, containers, and networks. It handles container lifecycle operations like starting, stopping, and removing containers.
  • Docker CLI: The command-line interface through which users interact with the Docker Engine. It simplifies tasks such as building images, managing containers, and pushing or pulling images from Docker Hub.
  • REST API: Provides programmatic access to Docker functionality, allowing integration with other tools and automation of container operations.

By combining these components, Docker Engine offers a powerful and user-friendly way to manage containerized applications, ensuring scalability, portability, and efficiency.

Docker Containers

Docker containers are lightweight, portable environments that run on the Docker Engine. They package applications and their dependencies together, ensuring they work consistently across any system.

  • Standard: Docker containers adhere to open standards, ensuring compatibility and interoperability across different platforms and tools. This consistency is critical for seamless development and deployment.
  • Lightweight: Unlike virtual machines, containers share the host operating system kernel, eliminating the need for a full OS inside each container. This design significantly reduces resource usage and start-up time.
  • Secure: Containers run in isolated environments, separating applications from the host system and from each other. This isolation minimizes risks and ensures better control over security policies.

Docker Installation

Docker can be installed on various operating systems, with the most common platforms being Linux and Windows. Below are the installation steps for both:

Docker Installation on Linux (Ubuntu 22.04)

  1. Update the system and Set up Docker's apt repository
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

  1. Install the Docker packages
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

  1. Verify Installation
docker --version
systemctl status docker

Docker Installation on Windows

  1. Download Docker Desktop for Windows
    Visit Docker Windows installer and download Docker Desktop for Windows.
  2. Install Docker Desktop
    Run the installer and follow the on-screen instructions. Ensure that WSL 2 (Windows Subsystem for Linux) and Hyper-V are enabled during installation.
  3. Configure WSL 2
    Docker Desktop requires WSL 2 to run on Windows. You can enable it via PowerShell:
wsl --install

  1. Launch Docker Desktop
    Once installed, launch Docker Desktop. It may take a few moments to start.
docker --version

Final Thoughts

In this blog, we explored Docker's essential components, including the differences between virtual machines and containers, the core functionality of Docker Engine, and the lightweight, secure nature of Docker containers. We also walked through the installation process for both Linux and Windows, equipping you to start using Docker in your projects.

In the next blog, we will take things further by mastering Docker commands and diving deeper into core Docker concepts, empowering you to manage containers and images effectively. Stay tuned!

Discussion