一键导入
docker
Docker containerization - images, containers, volumes, networking, docker-compose, best practices
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Docker containerization - images, containers, volumes, networking, docker-compose, best practices
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
TypeScript development guidelines - strict config, functional programming, best practices
Vitest testing framework - unit tests, mocking, best practices
React Router setup using @aminnairi/react-router - project initialization and routing implementation
Material-UI (MUI) v7 React component library - components, theming, customization, best practices
| name | docker |
| description | Docker containerization - images, containers, volumes, networking, docker-compose, best practices |
| metadata | {"language":"bash","audience":"developers"} |
Docker is a platform for developing, shipping, and running applications in containers. Use this skill when working with Docker or Docker Compose.
# Ubuntu/Debian
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
# Add user to docker group
sudo usermod -aG docker $USER
# Standalone (recommended)
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Verify installation:
docker --version
docker-compose --version
nginx:latest)# List images
docker images
docker image ls
# Pull an image
docker pull nginx:latest
# Remove an image
docker rmi nginx:latest
# Build an image from Dockerfile
docker build -t myapp:latest .
# Tag an image
docker tag myapp:latest registry.io/myapp:latest
# Push to registry
docker push registry.io/myapp:latest
Docker contexts allow you to manage connections to multiple Docker hosts from a single Docker CLI. Useful for switching between local Docker, remote servers, or Kubernetes clusters.
# List all contexts
docker context ls
# Show current context
docker context show
# Use a specific context
docker context use my-remote-host
# Create a new context
docker context create my-remote-host \
--docker "host=ssh://user@remote-server:22"
# Create context from existing Docker configuration
docker context create my-context --docker "host=tcp://localhost:2375"
# Inspect a context
docker context inspect my-remote-host
# Remove a context
docker context rm my-remote-host
Connect to a remote Docker host via SSH:
# Create SSH context
docker context create production \
--docker "host=ssh://user@production-server"
# Use the context
docker context use production
docker ps
docker run -d nginx:latest
# List Kubernetes contexts (requires kubectl)
kubectl config get-contexts
# Create Docker context from Kubernetes context
docker context create k8s-prod \
--kubernetes "context-name=production"
# Switch to Kubernetes context
docker context use k8s-prod
# Use a specific context with compose
docker --context my-remote-host compose up -d
# Or set context in compose file (Docker Compose v2)
COMPOSE_DOCKER_CLI_HOST=1 docker compose up -d
local-, prod-, dev-docker context inspect to verifydefault or local for local development# Example context setup
docker context create local # Local Docker (default)
docker context create dev-server --docker "host=ssh://dev@dev-server"
docker context create prod-server --docker "host=ssh://prod@prod-server"
# Quick switch
docker context use dev-server
latest in production# Build stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Production stage
FROM node:20-alpine AS production
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]
FROM python:3.11-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --user -r requirements.txt
FROM python:3.11-slim
WORKDIR /app
COPY --from=builder /root/.local /root/.local
COPY . .
ENV PATH=/root/.local/bin:$PATH
EXPOSE 8000
CMD ["python", "main.py"]
node_modules
.git
.gitignore
.env
.env.*
README.md
dist
build
coverage
*.log
.DS_Store
# Run in background (detached)
docker run -d myapp:latest
# Run with port mapping
docker run -d -p 8080:3000 myapp:latest
# Run with volume mount
docker run -d -v /host/path:/container/path myapp:latest
# Run with environment variables
docker run -d -e NODE_ENV=production -e API_KEY=xxx myapp:latest
# Run with name
docker run -d --name my-container myapp:latest
# Interactive mode
docker run -it ubuntu:latest /bin/bash
# Remove container after exit
docker run --rm myapp:latest
# List running containers
docker ps
# List all containers
docker ps -a
# Stop a container
docker stop my-container
# Start a stopped container
docker start my-container
# Restart a container
docker restart my-container
# Remove a container
docker rm my-container
# Remove all stopped containers
docker container prune
# View logs
docker logs -f my-container
# Execute command in running container
docker exec -it my-container sh
# Inspect container details
docker inspect my-container
# View resource usage
docker stats my-container
# Create a volume
docker volume create my-data
# List volumes
docker volume ls
# Inspect volume
docker volume inspect my-data
# Remove volume
docker volume rm my-data
# Mount host directory
docker run -v /host/path:/container/path myapp:latest
# Mount read-only
docker run -v /host/path:/container/path:ro myapp:latest
VOLUME /app/data
# Create a network
docker network create my-network
# List networks
docker network ls
# Connect container to network
docker network connect my-network my-container
# Disconnect container from network
docker network disconnect my-network my-container
EXPOSE 3000 8080
version: '3.8'
services:
app:
build: .
ports:
- "3000:3000"
environment:
- NODE_ENV=production
volumes:
- ./data:/app/data
depends_on:
- db
restart: unless-stopped
db:
image: postgres:15-alpine
environment:
POSTGRES_DB: myapp
POSTGRES_USER: user
POSTGRES_PASSWORD: password
volumes:
- db-data:/var/lib/postgresql/data
restart: unless-stopped
volumes:
db-data:
# Start services
docker-compose up -d
# Start with build
docker-compose up -d --build
# Stop services
docker-compose down
# Stop and remove volumes
docker-compose down -v
# View logs
docker-compose logs -f
# List services
docker-compose ps
# Execute command in service
docker-compose exec app sh
# Scale a service
docker-compose up -d --scale app=3
services:
app:
build: .
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
services:
app:
build: .
debug:
build: .
profiles:
- debug
command: ["sleep", "infinity"]
Run with profile: docker-compose --profile debug up
services:
app:
build:
context: .
target: development
volumes:
- .:/app
- /app/node_modules
environment:
- NODE_ENV=development
command: npm run dev
For Node.js with nodemon:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "dev"]
USER directivedocker scout or trivylatest--read-only flag--cap-drop and --cap-add# Security scanning
docker scout cves myapp:latest
# Run securely
docker run \
--read-only \
--cap-drop ALL \
--user 1000:1000 \
myapp:latest
# Interactive shell
docker run -it --rm myapp:latest sh
# Inspect with dive (if installed)
dive myapp:latest
# Check layers
docker history myapp:latest
# Debug running container
docker exec sh
# Copy files from -it my-container container
docker cp my-container:/app/logs ./logs
services:
app:
build: ./app
expose:
- "3000"
nginx:
image: nginx:alpine
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- app
services:
db:
image: postgres:15-alpine
volumes:
- db-data:/var/lib/postgresql/data
backup:
image: postgres:15-alpine
volumes:
- ./backups:/backups
command: >
sh -c "while true; do
pg_dump -h db -U postgres mydb > /backups/backup-$$(date +%Y%m%d-%H%M%S).sql;
sleep 86400;
done"
depends_on:
- db
docker system prune -a to clean up unused resources--force-rm when building to remove intermediate containers-f flag to force operations--no-cache for clean buildsdocker-compose config to validate compose files