Docker Basics Summary

1. Application Deployment Methods

There are 3 typical ways to deploy a web application or install a system application:

1.1 Physical Machine

You need to install:

1.2 Virtual Machine

You need to install:

1.3 Container (Recommended)

You need to install:

Key Advantage: No kernel operating system is required because containers share the host operating system's kernel.

2. Disadvantages of Each Deployment Method

Physical Machines vs VMs/Containers

Virtual Machines vs Containers

Containers - General Advantages

✓ Faster
✓ More portable
✓ More efficient (CPU, RAM, disk)
✓ Highly scalable

Only problem: Isolation - Virtual machines provide the best isolation among applications, but containers provide better isolation than physical machines.

3. Container Engines

What is a Container Engine?

A container engine (also known as container runtime) is a software component responsible for creating, managing, and running containers on a host system.

Docker Engine

The most popular container engine:

Other Container Engines

4. Basic Docker Concepts

Key Terminology

Key Relationships

Dockerfile --[build]--> Image --[run]--> Container (instance)

Docker vs Virtual Machines

5. Docker Compose

Tool for defining and running multi-container Docker applications.

Main Features

  1. Define Your Application Services: In a docker-compose.yml file (YAML format), you specify the services that make up your application. Each service typically corresponds to a container
  2. Environment Variables and Overrides: Use environment variables and override settings using a separate .env file or environment variables passed directly to docker-compose
  3. Start Your Application: Use docker-compose up command to:
    • Create necessary networks and volumes
    • Pull Docker images (if not already available locally)
    • Start containers in the correct order based on dependencies
  4. Manage Your Application: Docker Compose provides commands for managing your multi-container application

Use Cases

Especially useful for applications with multiple interconnected services:

6. Installing Docker Engine on Debian Linux

# Update repositories
sudo apt-get -y update

# Install dependencies
sudo apt-get -y install apt-transport-https ca-certificates curl gnupg2 gpg lsb-release

# Add Docker GPG key
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker.gpg

# Add Docker repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker.gpg] https://download.docker.com/linux/debian trixie stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update and install Docker
sudo apt-get -y update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin

Post-Installation

# Check Docker service status
sudo systemctl status docker
# Verify the service is started and enabled

# Add your user to the docker group
sudo gpasswd -a $USER docker

# Log out and log in, then verify membership
id
# Should show docker group (984)

# Check Docker version
docker --version

7. Essential Docker Commands

Image Management

Pulling an Image

docker pull <image_name:version>

# Examples:
docker pull photon:3.0
docker pull mysql:8.0

List Images

docker images
# Shows all downloaded images with repository, tag, image ID, created date, and size

Remove an Image

docker rmi <image_id>
# or
docker rmi <image_name:tag>

# Example:
docker rmi mysql:8.0

Search for Images

docker search <image_name>

# Example:
docker search nginx

Container Management

Run a Container

docker run <image_name>

# Run in detached mode (background)
docker run -d <image_name>

# Run with name
docker run --name <container_name> <image_name>

# Run with port mapping
docker run -p <host_port>:<container_port> <image_name>

# Example: Run nginx with port mapping
docker run -d -p 8080:80 --name my-nginx nginx

List Containers

# List running containers
docker ps

# List all containers (including stopped)
docker ps -a

# List only container IDs
docker ps -q

Stop a Container

docker stop <container_id>
# or
docker stop <container_name>

Start a Container

docker start <container_id>
# or
docker start <container_name>

Restart a Container

docker restart <container_id>

Remove a Container

# Must be stopped first
docker rm <container_id>

# Force remove (even if running)
docker rm -f <container_id>

Execute Commands in Running Container

docker exec <container_id> <command>

# Interactive shell
docker exec -it <container_id> /bin/bash
# or
docker exec -it <container_id> sh

View Container Logs

docker logs <container_id>

# Follow log output
docker logs -f <container_id>

# Show last N lines
docker logs --tail 100 <container_id>

Inspect Container

docker inspect <container_id>
# Returns detailed information in JSON format

System Management

View Docker System Information

docker info

View Docker Disk Usage

docker system df

Clean Up Unused Resources

# Remove all stopped containers
docker container prune

# Remove all unused images
docker image prune

# Remove all unused volumes
docker volume prune

# Remove all unused networks
docker network prune

# Clean up everything (use with caution!)
docker system prune

Volume Management

Create a Volume

docker volume create <volume_name>

List Volumes

docker volume ls

Remove a Volume

docker volume rm <volume_name>

Inspect a Volume

docker volume inspect <volume_name>

Network Management

List Networks

docker network ls

Create a Network

docker network create <network_name>

Remove a Network

docker network rm <network_name>

Inspect a Network

docker network inspect <network_name>

Building Images

Build from Dockerfile

docker build -t <image_name:tag> <path_to_dockerfile_directory>

# Example:
docker build -t myapp:1.0 .

Build with custom Dockerfile name

docker build -f <dockerfile_name> -t <image_name:tag> .

Docker Compose Commands

Start Services

docker-compose up

# Start in detached mode
docker-compose up -d

Stop Services

docker-compose down

# Stop and remove volumes
docker-compose down -v

View Logs

docker-compose logs

# Follow logs
docker-compose logs -f

List Services

docker-compose ps

Execute Command in Service

docker-compose exec <service_name> <command>

Quick Reference Table

Task Command
Pull image docker pull <image>
List images docker images
Remove image docker rmi <image>
Run container docker run <image>
List running containers docker ps
List all containers docker ps -a
Stop container docker stop <container>
Remove container docker rm <container>
View logs docker logs <container>
Execute command docker exec -it <container> <cmd>
Build image docker build -t <name> .

Note: This document provides an overview of fundamental Docker concepts and commands as presented in https://www.collados.org/asix1/sm1/tasks/sm1act11/sm1act11.html