跳转至

Docker 笔记

常用命令

# 构建镜像
docker build -t myapp:latest .

# 运行容器
docker run -d --name myapp -p 8080:80 myapp:latest

# 查看日志
docker logs -f myapp

# 进入容器
docker exec -it myapp sh

# 清理
docker system prune -f

Docker Compose

services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    restart: unless-stopped

小技巧

  • 用 --cache-from 加速重复构建
  • .dockerignore 排除 .git、node_modules 等
  • 多阶段构建减小镜像体积