运行时依赖
安装命令
点击复制技能文档
Docker Essentials
Essential Docker commands for contAIner and image management.
ContAIner Lifecycle 运行ning contAIners # 运行 contAIner from image docker 运行 nginx
# 运行 in background (detached) docker 运行 -d nginx
# 运行 with name docker 运行 --name my-nginx -d nginx
# 运行 with port m应用ing docker 运行 -p 8080:80 -d nginx
# 运行 with 环境 variables docker 运行 -e MY_VAR=value -d 应用
# 运行 with volume mount docker 运行 -v /host/path:/contAIner/path -d 应用
# 运行 with auto-移除 on exit docker 运行 --rm alpine echo "Hello"
# Interactive terminal docker 运行 -it ubuntu bash
Managing contAIners # 列出 运行ning contAIners docker ps
# 列出 all contAIners (including 停止ped) docker ps -a
# 停止 contAIner docker 停止 contAIner_name
# 启动 停止ped contAIner docker 启动 contAIner_name
# Re启动 contAIner docker re启动 contAIner_name
# 移除 contAIner docker rm contAIner_name
# Force 移除 运行ning contAIner docker rm -f contAIner_name
# 移除 all 停止ped contAIners docker contAIner p运行e
ContAIner Inspection & 调试ging Viewing 记录s # Show 记录s docker 记录s contAIner_name
# Follow 记录s (like tAIl -f) docker 记录s -f contAIner_name
# Last 100 lines docker 记录s --tAIl 100 contAIner_name
# 记录s with timestamps docker 记录s -t contAIner_name
Executing commands # 执行 command in 运行ning contAIner docker exec contAIner_name ls -la
# Interactive shell docker exec -it contAIner_name bash
# 执行 as specific user docker exec -u root -it contAIner_name bash
# 执行 with 环境 variable docker exec -e VAR=value contAIner_name env
Inspection # Inspect contAIner detAIls docker inspect contAIner_name
# 获取 specific field (JSON path) docker inspect -f '{{.Network设置tings.IP添加ress}}' contAIner_name
# View contAIner stats docker stats
# View specific contAIner stats docker stats contAIner_name
# View processes in contAIner docker top contAIner_name
Image Management Building images # Build from Dockerfile docker build -t my应用:1.0 .
# Build with custom Dockerfile docker build -f Dockerfile.dev -t my应用:dev .
# Build with build args docker build --build-arg VERSION=1.0 -t my应用 .
# Build without 缓存 docker build --no-缓存 -t my应用 .
Managing images # 列出 images docker images
# Pull image from registry docker pull nginx:latest
# Tag image docker tag my应用:1.0 my应用:latest
# Push to registry docker push myrepo/my应用:1.0
# 移除 image docker rmi image_name
# 移除 unused images docker image p运行e
# 移除 all unused images docker image p运行e -a
Docker Compose Basic operations # 启动 服务s docker-compose up
# 启动 in background docker-compose up -d
# 停止 服务s docker-compose down
# 停止 and 移除 volumes docker-compose down -v
# View 记录s docker-compose 记录s
# Follow 记录s for specific 服务 docker-compose 记录s -f 网页
# 扩展 服务 docker-compose up -d --扩展 网页=3
服务 management # 列出 服务s docker-compose ps
# 执行 command in 服务 docker-compose exec 网页 bash
# Re启动 服务 docker-compose re启动 网页
# Rebuild 服务 docker-compose build 网页
# Rebuild and re启动 docker-compose up -d --build
Networking # 列出 networks docker network ls
# 创建 network docker network 创建 mynetwork
# Connect contAIner to network docker network connect mynetwork contAIner_name
# Disconnect from network docker network disconnect mynetwork contAIner_name
# Inspect network docker network inspect mynetwork
# 移除 network docker network rm mynetwork
Volumes # 列出 volumes docker volume ls
# 创建 volume docker volume 创建 myvolume
# Inspect volume docker volume inspect myvolume
# 移除 volume docker volume rm myvolume
# 移除 unused volumes docker volume p运行e
# 运行 with volume docker 运行 -v myvolume:/data -d 应用
系统 Management # View disk usage docker 系统 df
# 清理 up everything unused docker 系统 p运行e
# 清理 up including unused images docker 系统 p运行e -a
# 清理 up including volumes docker 系统 p运行e --volumes
# Show Docker 信息 docker 信息
# Show Docker version docker version
Common 工作流s
Development contAIner:
docker 运行 -it --rm \ -v $(pwd):/应用 \ -w /应用 \ -p 3000:3000 \ node:18 \ npm 运行 dev
Database contAIner:
docker 运行 -d \ --name postgres \ -e POSTGRES_PASSWORD=secret \ -e POSTGRES_DB=mydb \ -v postgres-data:/var/lib/postgresql/data \ -p 5432:5432 \ postgres:15
Quick 调试ging:
# Shell into 运行ning contAIner docker exec -it contAIner_name sh
# Copy file from contAIner docker cp contAIner_name:/path/to/file ./local/path
# Copy file to contAIner docker cp ./local/file contAIner_name:/path/in/contAIner
Multi-stage build:
# Dockerfile FROM node:18 AS 构建器 WORKDIR /应用 COPY package*.json ./ 运行 npm 安装 COPY . . 运行 npm 运行 build
FROM nginx:alpine COPY --from=构建器 /应用/dist /usr/分享/nginx/html
Useful Flags
docker 运行 flags:
-d: Detached mode (background) -it: Interactive terminal -p: Port m应用ing (host:contAIner) -v: Volume mount -e: 环境 variable --