Docker Beginner Tutorial: From Installation to Containerized Web Deployment

Docker has become the standard technology for modern software deployment. It packages applications and their dependencies into standardized containers, ensuring consistent operation across any environment.

1. Docker Core Concepts

1.1 What is a Container

Traditional Deployment:
Application → Dependencies → OS → Server
(Environment differences cause "it works on my machine" problems)

Container Deployment:
Docker Container (App + Dependencies) → Docker Engine → OS
(Standardized, consistent environment)

1.2 Key Terminology

Term Description Analogy
Image Read-only template of app and dependencies Installation disc
Container Running instance of an image Running program
Dockerfile Instructions for building an image Recipe
Docker Hub Image registry App store
Docker Compose Multi-container management tool One-click launch

2. Installing Docker

# macOS
brew install docker --cask
# Or visit https://www.docker.com/products/docker-desktop

# Ubuntu
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER  # Run docker without sudo

# Verify installation
docker --version
docker run hello-world

3. Your First Docker Application

3.1 Create a Simple Web Application

# Dockerfile
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "server.js"]
// server.js
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello Docker!');
});

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

3.2 Build and Run

# Build image
docker build -t my-web-app .

# Run container
docker run -d -p 3000:3000 --name my-app my-web-app

# List running containers
docker ps

# View logs
docker logs my-app

# Stop container
docker stop my-app

# Remove container
docker rm my-app

4. Docker Compose: Multi-Service Management

4.1 What is Docker Compose

Docker Compose defines and runs multi-container applications using a single YAML file, ideal for scenarios involving web apps + databases + cache.

4.2 docker-compose.yml Example

version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DB_HOST=db
      - DB_USER=app
      - DB_PASSWORD=secret
    depends_on:
      - db
    volumes:
      - .:/app
      - /app/node_modules

  db:
    image: mysql:8.0
    environment:
      - MYSQL_DATABASE=app
      - MYSQL_USER=app
      - MYSQL_PASSWORD=secret
      - MYSQL_ROOT_PASSWORD=rootsecret
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - "3306:3306"

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

volumes:
  db_data:

4.3 Start Multi-Service

# Start all services
docker-compose up -d

# Check service status
docker-compose ps

# View logs
docker-compose logs -f web

# Stop all services
docker-compose down

5. Deploy to Server

5.1 Deploy with Docker to VPS

# 1. Install Docker on the server

# 2. Transfer project files
scp -r ./my-app user@server:/var/www/

# 3. Run on the server
ssh user@server
cd /var/www/my-app
docker-compose up -d --build

5.2 Optimized Deployment with Docker

# Multi-stage build (reduce image size)
# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Run (only include files needed for runtime)
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./

EXPOSE 3000
CMD ["node", "dist/server.js"]

6. Common Docker Commands

# Image management
docker images                    # List local images
docker pull nginx                # Pull an image
docker rmi <image_id>            # Remove an image

# Container management
docker run -d nginx              # Run in background
docker exec -it <id> /bin/bash   # Enter a container
docker logs -f <id>              # View logs
docker cp <id>:/path /local      # Copy files

# Cleanup
docker system prune              # Clean unused containers and images
docker volume prune              # Clean unused volumes

7. Best Practices

1. Image optimization:
   - Use Alpine base images
   - Multi-stage builds
   - Combine RUN commands to reduce layers

2. Security:
   - Do not run containers as root
   - Regularly update base images
   - Use .dockerignore to exclude sensitive files

3. Data persistence:
   - Use volumes for stateful services like databases
   - Use bind mounts for configuration files

4. Logging:
   - Output container logs to stdout/stderr
   - Use Docker's log drivers for collection

8. Summary

Docker is an indispensable tool for modern application deployment. Mastering Docker's basic concepts and usage allows you to easily achieve "build once, run anywhere." Start with simple single-container web apps, gradually learn Docker Compose multi-service orchestration, and ultimately build a complete CI/CD pipeline.