最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Docker 核心指南:掌握容器管理 - Openclaw Skills
时间:2026-08-18 16:36:01 编辑:袖梨 来源:一聚教程网
什么是 Docker 核心指南?
Docker 核心指南是一项旨在简化容器化开发和运维的技术资源。它提供了一种结构化的方法来管理整个容器生命周期,从使用 Dockerfile 进行初始镜像构建,到使用 Docker Compose 进行复杂的镜像服务编排。对于需要维护一致环境并高效调试服务的 Openclaw Skills 用户来说,这项技能尤为重要。
该技能涵盖了容器技术的核心支柱,包括网络、通过数据卷实现的持久化数据存储以及系统范围的资源管理。通过整合这些工作流,开发者可以确保其应用程序在 Openclaw Skills 生态系统中具有可移植性、可扩展性且易于维护。
下载入口:https://github.com/openclaw/skills/tree/main/skills/arnarsson/docker-essentials安装与下载
1. ClawHub CLI
从源直接安装技能的最快方式。
npx clawhub@latest install docker-essentials
2. 手动安装
将技能文件夹复制到以下位置之一
全局模式~/.openclaw/skills/
工作区
<project>/skills/
优先级:工作区 > 本地 > 内置
3. 提示词安装
将此提示词复制到 OpenClaw 即可自动安装。
请帮我使用 Clawhub 安装 docker-essentials。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。
Docker 核心指南 应用场景
- 使用特定的语言运行时和依赖项创建隔离的开发环境。
- 通过流式传输容器日志和执行交互式 shell 来排查应用程序错误。
- 管理本地数据库实例,并使用持久卷挂载以确保数据一致性。
- 自动清理悬空镜像和未使用的网络,以回收系统磁盘空间。
- 使用 Docker Compose 部署多服务架构,进行本地集成测试。
- 开发者触发与容器相关的任务,例如构建新镜像或启动服务。
- 该技能参考适当的 Docker CLI 命令,应用最佳标志,如用于清理的 --rm 或用于交互的 -it。
- 通过构建、标记和推送到仓库来管理镜像层,以促进部署。
- 应用网络和卷配置,确保服务正确通信且数据在容器重启后依然存在。
- 调用系统级维护命令,保持宿主环境清洁且高效,以便进行其他 Openclaw Skills 操作。
Docker 核心指南 配置指南
要使用此技能,请确保系统中已安装并运行 Docker Engine。docker 和 docker-compose 二进制文件必须在系统的 PATH 中可访问。
# 检查 Docker 是否已安装
docker --version
# 验证 Docker Compose 安装
docker-compose --version
# 通过运行 hello-world 容器测试权限
docker run --rm hello-world
Docker 核心指南 数据架构与分类体系
该技能将 Docker 对象组织为几个关键分类,以便在管理期间提供清晰度:
| 组件 | 管理的数据 | 目的 |
|---|---|---|
| 容器 | ID、状态、端口 | 运行时执行和进程隔离 |
| 镜像 | 标签、层、大小 | 容器实例的版本化蓝图 |
| 数据卷 | 挂载点、驱动程序 | 容器生命周期之外的持久数据存储 |
| 网络 | 子网、网关 | 内部和外部通信通道 |
| Compose | 服务、依赖项 | 多容器应用程序定义 |
name: docker-essentials
description: Essential Docker commands and workflows for container management, image operations, and debugging.
homepage: https://docs.docker.com/
metadata: {"clawdbot":{"emoji":"??","requires":{"bins":["docker"]}}}
Docker Essentials
Essential Docker commands for container and image management.
Container Lifecycle
Running containers
# 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
Managing containers
# 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
Container Inspection & Debugging
Viewing logs
# 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
Executing commands
# 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
Inspection
# 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
Image Management
Building images
# 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 .
Managing images
# 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
Basic operations
# 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
Service management
# 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
Networking
# 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
Volumes
# 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
System Management
# 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
Common Workflows
Development container:
docker run -it --rm r
-v $(pwd):/app r
-w /app r
-p 3000:3000 r
node:18 r
npm run dev
Database container:
docker run -d r
--name postgres r
-e POSTGRES_PASSWORD=secret r
-e POSTGRES_DB=mydb r
-v postgres-data:/var/lib/postgresql/data r
-p 5432:5432 r
postgres:15
Quick debugging:
# 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
Multi-stage build:
# 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
Useful Flags
docker run flags:
-d: Detached mode (background)-it: Interactive terminal-p: Port mapping (host:container)-v: Volume mount-e: Environment variable--name: Container name--rm: Auto-remove on exit--network: Connect to network
docker exec flags:
-it: Interactive terminal-u: User-w: Working directory
Tips
- Use
.dockerignoreto exclude files from build context - Combine
RUNcommands in Dockerfile to reduce layers - Use multi-stage builds to reduce image size
- Always tag your images with versions
- Use
--rmfor one-off containers - Use
docker-composefor multi-container apps - Clean up regularly with
docker system prune
Documentation
Official docs: https://docs.docker.com/ Dockerfile reference: https://docs.docker.com/engine/reference/builder/ Compose file reference: https://docs.docker.com/compose/compose-file/