Cloud News
Trends
Insight
and more
Docker, one of the popular terms among tech-enthusiasts!
​
Docker is a technology suite that enables development and IT operations teams to build, ship, and run distributed applications anywhere.
​
The term “docker” originates from the shipping industry, where standardized containers streamlined global cargo transport. Docker containers package software and its dependencies, ensuring consistent operation across diverse computing environments.
​
The power of Docker lies in its ability to solve the age-old problem of “it works on my machine” by encapsulating an application’s entire runtime environment. This includes the code, system tools, libraries, and settings.
​
Docker Concepts
​
Docker Images: Think of a Docker image as a comprehensive blueprint for an application. It’s a self-contained package that includes all the essential elements needed to run a piece of software. This encompasses:
-
The application code itself
-
A runtime environment (e.g., Java Runtime, Python interpreter)
-
System libraries and dependencies
-
Configuration files
-
Any other necessary tools or resources
​
Images are designed to be lightweight and portable, allowing for easy distribution and deployment across different systems. They serve as the foundation for creating containers.
​
Docker Containers: A Docker container is essentially a live, running instance of a Docker image. When you start a container, you’re bringing the blueprint (image) to life. Key characteristics of containers include:
-
Isolation: Each container operates in its own environment, separate from other containers and the host system. This isolation ensures consistency and security.
-
Executability: Containers are ready-to-run instances of applications.
-
Consistency: The application will behave the same way in a container, regardless of where that container is deployed.
-
Efficiency: Containers share the host system’s kernel, making them more lightweight than traditional virtual machines.
​
The relationship between images and containers is similar to that of a class and an object in programming. The image defines the structure and contents, while the container is a specific instance of that image in action.
​
Containers Vs VM
​​
​
​
​
​
​
​
​
​
​
​
​
​
​​
​
​
​
​
Containers differ from virtual machines (VMs) in a fundamental way. While containers run directly on the host’s Linux system and share its kernel, VMs run a complete guest operating system on top of the host. This means containers are more lightweight and efficient, using only the resources needed for the application itself. In contrast, VMs require more overhead to run the entire guest operating system.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Docker Architecture
​
Docker Engine: The Core of the System
​
The Docker Engine is the heartbeat of the Docker ecosystem, functioning as a client-server application with three main components:
​
1. Docker Daemon (dockerd):
-
A long-running background process that manages Docker objects like containers, images, networks, and volumes.
-
Listens for API requests and handles container operations, image management, and networking.
-
​
2. Docker Client (CLI):
-
A command-line interface that allows users to interact with Docker.
-
Example command: docker pull nginx fetches the Nginx image from a Docker registry.
​
3. REST API:
-
Facilitates communication between the Docker client and the daemon.
-
It can be used for remote management or integration with other tools.
-
Communication can occur via UNIX sockets or a network interface.
​
The Docker Daemon and Client can run on the same system or operate remotely, ensuring flexibility in system design.
​
Platform-Specific Considerations
​
-
Linux: The Docker host runs the daemon directly, and the client can be accessed via the terminal.
-
Windows/macOS: Docker Toolbox provides the necessary Docker environment. This includes:
-
Docker Client: CLI to interact with Docker.
-
Compose: For defining and running multi-container applications.
-
Kitematic: A graphical user interface (GUI) for managing Docker containers.
-
Machine: For managing Docker hosts on remote servers.
-
VirtualBox: A virtual environment that enables Docker to run on non-Linux platforms.
​
Docker Images
​
They are read-only templates that contain everything needed to run an application, such as the code, runtime, libraries, and dependencies. Docker images are created using Dockerfiles, which provide a set of instructions to build the image.
​
​
​
​
​
​
​
​​
​
​​​
Docker Containers: Running Instances
Docker Containers are lightweight, isolated environments that run instances of Docker images. They encapsulate applications and their dependencies, ensuring consistency across different environments.
​
​
​
​
Docker Registry: Image Repository
Docker Registry serves as a storage location for Docker images. Docker Hub is the default public registry, but private registries can be used to store proprietary images.
​
​
​
​​
​
​
​
​
​
​
Docker Compose: Multi-Container Applications
Docker Compose allows the definition and management of multi-container applications. Compose uses a YAML file to specify the services that make up the application.
​
​
​
​
​​
​
​
​
​
​
​
​
​
To run the services
​
​
​
​
​
​
Docker Volumes: Persistent Data Storage
Docker Volumes provide a method to persist data generated by containers and share data between them. Volumes are independent of the container lifecycle, ensuring data continuity even if a container is removed.
​
​
​
​
​
​​
​
​
​
​
​
Docker Networking: Container Communication
​
Docker provides networking capabilities that allow containers to communicate with each other securely. Multiple networking drivers are available, such as bridge, host, and overlay networks.
​
Docker’s Underlying Technologies
​
Docker leverages several key technologies to provide its robust containerization capabilities:
​
Go Programming Language Docker is primarily written in Go, a language known for its efficiency and strong support for concurrent operations. This choice contributes to Docker’s performance and scalability.
​
Linux Kernel Features Docker harnesses two crucial Linux kernel features:
​
1. Namespaces Docker utilizes namespaces to create isolated workspaces called containers. When a container is launched, Docker generates a set of namespaces for it, ensuring isolation. Each aspect of a container operates within its own namespace, with access restricted to that specific namespace.
​
2. Control Groups (cgroups) It allows Docker to manage and limit resource allocation (CPU, memory, disk I/O, network, etc.) for container processes. This feature enables Docker to efficiently share available hardware resources among containers and enforce resource constraints when necessary.
​
3. Union File Systems (UnionFS) UnionFS are layered file systems that Docker Engine uses as building blocks for containers. These file systems operate by creating layers, which contributes to the lightweight and fast nature of Docker containers.
​
4. Container Format Docker Engine combines namespaces, cgroups, and UnionFS into a wrapper known as a container format. The default container format used by Docker is libcontainer.
​
Some common and very useful Docker commands:
​​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
Usage is basically syntax which changes accordingly
This technological foundation allows Docker to provide isolated, efficient, and portable containerized environments for applications.
​
FROM python:3.8 # Use Python 3.8 as the base image COPY app.py /app/app.py # Copy the Python script into the container
WORKDIR /app # Set the working directory to /app CMD [“python”, “app.py”] # Run the Python script
To build the image:
docker build -t my-python-app .
docker run -d my-python-app
docker tag my-python-app myusername/my-python-app:v1.0 # Tag the image
docker push myusername/my-python-app:v1.0 # Push to Docker Hub
version: ‘3’
services:
web:
image: node:14
ports: - “8080:8080”
redis:
image: “redis:alpine”
docker-compose up
docker volume create mydata # Create a volume
docker run -v mydata:/app/data my-app # Mount the volume to
/app/data inside the container