Translated by GPT4o from my original chinese page.

Come try one-click foolproof distribution and become a Docker expert! In software development, whenever you encounter CI/CD concepts, Docker is indispensable.

Docker Basic Concepts

1. Docker Introduction

Docker is an open-source containerization technology that can package applications and all their dependencies into a portable container. Through Docker, you can ensure application consistency across different environments (development, testing, production), solving the “it works on my machine” problem.

2. Docker Basic Concepts

  1. Image:
    • Similar to an application template, containing all the environments needed to run the application (such as operating system, runtime, libraries, application code, etc.).
    • Images are immutable, similar to virtual machine snapshots.
  2. Container:
    • An instantiated running environment based on an image. Each container is an independent sandbox containing the application and all dependencies.
    • Containers are lightweight, start quickly, and use fewer resources.
  3. Dockerfile:
    • A script file used to define how to build images (including operating system, dependency installation, program code, etc.).
    • Similar to virtual machine configuration files.
  4. Docker Hub:
    • A public image repository, similar to code hosting platforms (like GitHub). You can download or push images to Docker Hub.
  5. Network:
    • Docker provides container intercommunication functionality, allowing containers to connect through networks.
    • The default network mode is bridge, where all containers connect to a virtual bridge.
  6. Volume:
    • Used for data persistence, saving container internal data to the host machine’s file system, preventing data loss after container deletion.

3. Docker Basic Operations

  1. Pull Image:

    docker pull <image-name>:<tag>

    Example:

    docker pull python:3.9
  2. View Local Images:

    docker images
  3. Delete Image:

    docker rmi <image-ID>
  1. Run Container:

    docker run -d -p 8000:8000 <image-name>

    Parameter explanation:

    • -d: Run container in background.

    • -p: Map container port to host port (e.g., 8000:8000).

    • Example:

      docker run -d -p 8080:80 nginx
  2. View Running Containers:

    docker ps
  3. View All Containers (Including Stopped):

    docker ps -a
  4. Stop Container:

    docker stop <container-ID>
  5. Delete Container:

    docker rm <container-ID>

Other Commands

  1. Enter Container:

    docker exec -it <container-ID> /bin/bash

    Example:

    docker exec -it my-container /bin/bash
  2. View Container Logs:

    docker logs <container-ID>
  3. Clean Unused Images and Containers:

    docker system prune -a

Docker Deployment

1. Dockerfile Purpose

What is Dockerfile?

Dockerfile is a text file used to define how to build a Docker image. It contains information about the base image, dependency installation, file copying, and running commands.


Dockerfile Basic Structure

Here’s a standard Dockerfile example:

# Use official base image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy project dependency files
COPY requirements.txt .

# Install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy application code
COPY app/ ./app

# Expose application port
EXPOSE 8000

# Run application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]

Key Instructions in Dockerfile

  • FROM: Specify base image (e.g., python:3.11-slim).
  • WORKDIR: Set working directory in container.
  • COPY: Copy local files to image.
  • RUN: Execute commands during image build (e.g., install dependencies).
  • EXPOSE: Declare ports container will use.
  • CMD: Specify command to run when container starts.

2. Docker Compose Purpose

What is Docker Compose?

Docker Compose is a tool for defining and managing multi-container applications. Through the docker-compose.yml file, you can start a group of related containers with simple commands.


Docker Compose File Example

Here’s an example docker-compose.yml file for deploying an application with a single service:

version: '3.8'

services:
  simon_says:
    build: .
    container_name: simon_says_app
    ports:
      - "8000:8000"
    environment:
      - PYTHONUNBUFFERED=1

Key Parts Explained:

  • services: Define a group of container services.
  • build: Specify build context (current directory .).
  • container_name: Specify container name.
  • ports: Define host and container port mapping (e.g., 8000:8000).
  • environment: Configure environment variables.

3. Docker Publishing and Deployment Process

3.1 Local Build and Testing

  1. Write Dockerfile: Ensure Dockerfile is correct, defining application build and run steps.

  2. Build Image

    docker build -t simon_says:latest .
  3. Run Container

    docker run -d -p 8000:8000 simon_says:latest
  4. Test Application: Access http://localhost:8000 via browser or command line.


3.2 Using Docker Compose

  1. Write docker-compose.yml: Ensure service and port configurations are correct.

  2. Start Services

    docker-compose up -d
  3. Test Application: Access http://localhost:8000.


3.3 Publish Image to Docker Hub

  1. Login to Docker Hub

    docker login
  2. Tag Image

    docker tag simon_says:latest yourusername/simon_says:latest
  3. Push to Docker Hub

    docker push yourusername/simon_says:latest

3.4 Remote Deployment

  1. Pull Image on Remote Server

    docker pull yourusername/simon_says:latest
  2. Run Image

    1. Direct Run:
    docker run -d -p 8000:8000 yourusername/simon_says:latest
    1. Using Docker Compose:

      version: '3.8'
      
      services:
        simon_says:
          image: yourusername/simon_says:latest
          ports:
            - "8000:8000"
      docker-compose up -d