Docker Usage and Configuration
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
- 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.
- 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.
- 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.
- Docker Hub:
- A public image repository, similar to code hosting platforms (like GitHub). You can download or push images to Docker Hub.
- 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.
- 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
Image Related
Pull Image:
docker pull <image-name>:<tag>
Example:
docker pull python:3.9
View Local Images:
docker images
Delete Image:
docker rmi <image-ID>
Container Related
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
View Running Containers:
docker ps
View All Containers (Including Stopped):
docker ps -a
Stop Container:
docker stop <container-ID>
Delete Container:
docker rm <container-ID>
Other Commands
Enter Container:
docker exec -it <container-ID> /bin/bash
Example:
docker exec -it my-container /bin/bash
View Container Logs:
docker logs <container-ID>
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
Write Dockerfile: Ensure
Dockerfile
is correct, defining application build and run steps.Build Image
docker build -t simon_says:latest .
Run Container
docker run -d -p 8000:8000 simon_says:latest
Test Application: Access http://localhost:8000 via browser or command line.
3.2 Using Docker Compose
Write
docker-compose.yml
: Ensure service and port configurations are correct.Start Services
docker-compose up -d
Test Application: Access http://localhost:8000.
3.3 Publish Image to Docker Hub
Login to Docker Hub
docker login
Tag Image
docker tag simon_says:latest yourusername/simon_says:latest
Push to Docker Hub
docker push yourusername/simon_says:latest
3.4 Remote Deployment
Pull Image on Remote Server
docker pull yourusername/simon_says:latest
Run Image
- Direct Run:
docker run -d -p 8000:8000 yourusername/simon_says:latest
Using Docker Compose:
version: '3.8' services: simon_says: image: yourusername/simon_says:latest ports: - "8000:8000"
docker-compose up -d