关于 docker-essentials
docker-essentials 是用于管理 Docker 容器、镜像、Compose 服务、网络和卷的命令参考技能,面向需要常用运维与调试命令的开发者,并提供开发及数据库容器示例。
主要功能
- 01运行、停止和检查容器
- 02查看日志并在容器中执行命令
- 03构建、拉取和标记镜像
- 04管理 Compose 服务
- 05操作网络和卷
- 06清理 Docker 系统资源
适用场景
- 01启动挂载源代码的开发容器
- 02运行带持久化存储的数据库容器
- 03通过日志和 Shell 调试容器
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sundial-org/ awesome-openclaw-skills --skill docker-essentials安装后在 Claude Code 中输入/docker-essentials确认已加载。
详细介绍
Docker 基础知识
用于容器和镜像管理的常用 Docker 命令。
容器生命周期
运行容器
# Run container from image
docker run nginx
# Run in background (detached)
docker run -d nginx
# Run with name
docker run --name my-nginx -d nginx
# Run with port mapping
docker run -p 8080:80 -d nginx
# Run with environment variables
docker run -e MY_VAR=value -d app
# Run with volume mount
docker run -v /host/path:/container/path -d app
# Run with auto-remove on exit
docker run --rm alpine echo "Hello"
# Interactive terminal
docker run -it ubuntu bash
管理容器
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Stop container
docker stop container_name
# Start stopped container
docker start container_name
# Restart container
docker restart container_name
# Remove container
docker rm container_name
# Force remove running container
docker rm -f container_name
# Remove all stopped containers
docker container prune
容器检查与调试
查看日志
# Show logs
docker logs container_name
# Follow logs (like tail -f)
docker logs -f container_name
# Last 100 lines
docker logs --tail 100 container_name
# Logs with timestamps
docker logs -t container_name
执行命令
# Execute command in running container
docker exec container_name ls -la
# Interactive shell
docker exec -it container_name bash
# Execute as specific user
docker exec -u root -it container_name bash
# Execute with environment variable
docker exec -e VAR=value container_name env
检查
# Inspect container details
docker inspect container_name
# Get specific field (JSON path)
docker inspect -f '{{.NetworkSettings.IPAddress}}' container_name
# View container stats
docker stats
# View specific container stats
docker stats container_name
# View processes in container
docker top container_name
镜像管理
构建镜像
# Build from Dockerfile
docker build -t myapp:1.0 .
# Build with custom Dockerfile
docker build -f Dockerfile.dev -t myapp:dev .
# Build with build args
docker build --build-arg VERSION=1.0 -t myapp .
# Build without cache
docker build --no-cache -t myapp .
管理镜像
# List images
docker images
# Pull image from registry
docker pull nginx:latest
# Tag image
docker tag myapp:1.0 myapp:latest
# Push to registry
docker push myrepo/myapp:1.0
# Remove image
docker rmi image_name
# Remove unused images
docker image prune
# Remove all unused images
docker image prune -a
Docker Compose
基本操作
# Start services
docker-compose up
# Start in background
docker-compose up -d
# Stop services
docker-compose down
# Stop and remove volumes
docker-compose down -v
# View logs
docker-compose logs
# Follow logs for specific service
docker-compose logs -f web
# Scale service
docker-compose up -d --scale web=3
服务管理
# List services
docker-compose ps
# Execute command in service
docker-compose exec web bash
# Restart service
docker-compose restart web
# Rebuild service
docker-compose build web
# Rebuild and restart
docker-compose up -d --build
网络
# List networks
docker network ls
# Create network
docker network create 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
# Remove network
docker network rm mynetwork
卷
# List volumes
docker volume ls
# Create volume
docker volume create myvolume
# Inspect volume
docker volume inspect myvolume
# Remove volume
docker volume rm myvolume
# Remove unused volumes
docker volume prune
# Run with volume
docker run -v myvolume:/data -d app
系统管理
# View disk usage
docker system df
# Clean up everything unused
docker system prune
# Clean up including unused images
docker system prune -a
# Clean up including volumes
docker system prune --volumes
# Show Docker info
docker info
# Show Docker version
docker version
常见工作流
开发容器:
docker run -it --rm \
-v $(pwd):/app \
-w /app \
-p 3000:3000 \
node:18 \
npm run dev
数据库容器:
docker run -d \
--name postgres \
-e POSTGRES_PASSWORD=secret \
-e POSTGRES_DB=mydb \
-v postgres-data:/var/lib/postgresql/data \
-p 5432:5432 \
postgres:15
快速调试:
# Shell into running 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
多阶段构建:
# Dockerfile
FROM node:18 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
常用标志
docker run 标志:
-d:分离模式(后台运行)-it:交互式终端-p:端口映射(主机:容器)-v:卷挂载-e:环境变量--name:容器名称--rm:退出时自动删除--network:连接到网络
docker exec 标志:
-it:交互式终端-u:用户-w:工作目录
提示
- 使用
.dockerignore从构建上下文中排除文件 - 在 Dockerfile 中合并
RUN命令以减少层数 - 使用多阶段构建来减小镜像体积
- 始终为镜像标记版本
- 对于一次性容器,使用
--rm - 对于多容器应用,使用
docker-compose - 定期使用
docker system prune清理
文档
官方文档:https://docs.docker.com/ Dockerfile 参考:https://docs.docker.com/engine/reference/builder/ Compose 文件参考:https://docs.docker.com/compose/compose-file/
评论
支持 Markdown