Docker Documentation

Containerization - Docker packages applications and their dependencies into containers, ensuring consistent deployment across any environment.

What is Docker?

Docker is a platform for developing, shipping, and running applications using containerization technology. Containers allow you to package an application with all its dependencies into a single unit, making it easy to run consistently across different environments.

Key Concepts

🐳 Docker Image

An image is a read-only template with instructions for creating a Docker container. Think of it as a blueprint for your application and its environment.

📦 Container

A container is a runnable instance of an image. It's isolated from other containers and uses the host OS kernel.

🏗️ Dockerfile

A text file containing instructions to build a Docker image. It defines what's included in the container, commands to run, and environment variables.

📝 Docker Compose

A tool for defining and running multi-container Docker applications. You use a YAML file to configure services.

Installation

Linux (Ubuntu/Debian)

# Update package index
sudo apt-get update

# Install prerequisites
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

# Add Docker's official GPG key
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set up repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Install Docker Engine
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

macOS

# Using Homebrew
brew install --cask docker

# Or download from: https://www.docker.com/products/docker-desktop

Verify Installation

# Check Docker version
docker --version

# Run test container
docker run hello-world

Common Docker Commands

Container Management

# List running containers
docker ps

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

# Run a container
docker run -d --name myapp nginx:latest

# Stop a container
docker stop myapp

# Start a stopped container
docker start myapp

# Remove a container
docker rm myapp

# View container logs
docker logs myapp

# Follow logs in real-time
docker logs -f myapp

Image Management

# List images
docker images

# Pull an image from Docker Hub
docker pull nginx:latest

# Build an image from Dockerfile
docker build -t myapp:latest .

# Remove an image
docker rmi myapp:latest

# Search for images on Docker Hub
docker search nginx

Container Inspection

# Inspect container details
docker inspect myapp

# Execute a command in running container
docker exec -it myapp sh

# View container statistics
docker stats myapp

# View resource usage
docker top myapp

Quick Start Example

Let's create a simple Node.js application in Docker:

1. Create a simple Node.js app

// app.js
const http = require('http');
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from Docker!\n');
});
server.listen(3000, () => {
  console.log('Server running on port 3000');
});

2. Create a Dockerfile

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

3. Build and run

# Build the image
docker build -t my-node-app .

# Run the container
docker run -d -p 3000:3000 --name myapp my-node-app

# Test it
curl http://localhost:3000

Next Steps

Continue learning with our detailed guides: