Docker Basics
Introduction to Docker
Docker is a powerful container technology that revolutionizes software deployment and development. As a containerization platform, Docker enables developers to package applications with all their dependencies, ensuring consistent and efficient software delivery across different computing environments.
Core Concepts of Containerization
Containers are lightweight, standalone, executable packages that include everything needed to run an application: code, runtime, system tools, libraries, and settings. Unlike traditional virtual machines, containers share the host system's kernel, making them more resource-efficient.
graph TD
A[Application Code] --> B[Docker Container]
C[Dependencies] --> B
D[System Libraries] --> B
E[Runtime Environment] --> B
Docker Architecture
Component |
Description |
Function |
Docker Daemon |
Background service |
Manages Docker objects |
Docker Client |
Command-line interface |
Sends commands to Docker daemon |
Docker Registry |
Storage for Docker images |
Allows image sharing and distribution |
Installation on Ubuntu 22.04
## Update package index
sudo apt update
## Install dependencies
sudo apt install apt-transport-https ca-certificates curl software-properties-common
## Add Docker's official GPG key
curl -fsSL | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
## Set up stable repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
## Install Docker Engine
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io
Basic Docker Commands
## Pull an image
docker pull ubuntu:latest
## List images
docker images
## Run a container
docker run -it ubuntu:latest /bin/bash
## List running containers
docker ps
## Stop a container
docker stop container_id
Dockerfile Example
## Use official Ubuntu base image
FROM ubuntu:22.04
## Set working directory
WORKDIR /app
## Install Python
RUN apt-get update && apt-get install -y python3
## Copy application files
COPY . /app
## Define command to run
CMD ["python3", "app.py"]