Docker Compose 基础

Updated on with 0 views and 0 comments

Docker Compose 基础

概述: 用一个 YAML 文件,一键启动 / 停止 / 管理多个容器

image.png

一、 核心命令

# 查看版本
docker compose -v
docker compose version

# 查看服务
docker compose ps

# 后台启动所有服务
docker compose up -d

# 停止所有服务的容器
docker compose stop

# 删除所有服务的容器、网络
docker compose down

# 查看日志
docker compose logs -f

# 重启服务
docker compose restart

# 重建镜像后重新启动(改了 Dockerfile 用这个)
docker compose up -d --build

# 只启动某个服务
docker compose up -d nginx

# 进入容器
docker compose exec 服务名 bash

# 拉取最新镜像
docker compose pull

# 查看服务运行的进程
docker compose top

# 验证 docker-compose.yml 语法是否正确
docker compose config -q

# 停止并删除容器、网络、镜像
docker compose down --rmi all

# 停止并删除容器、网络、卷(数据会删!谨慎)
docker compose down -v

二、 docker-compose.yml

  1. image —— 镜像
  2. container_name —— 容器名
  3. ports —— 端口映射
  4. volumes —— 挂载
  5. environment —— 环境变量
  6. networks —— 网络
  7. restart —— 自启动
  8. depends_on —— 依赖
  9. build —— 构建
  10. command —— 启动命令
  11. entrypoint —— 入口
  12. working_dir —— 工作目录
  13. user —— 指定用户
  14. privileged —— 最高权限
  15. hostname —— 主机名
  16. healthcheck —— 健康检查
  17. volumes —— 定义数据卷
  18. networks —— 定义网络
  19. configs —— 配置
  20. secrets —— 密钥

三、 例子

1. LNMP

version: '3'

services:
  nginx:
    image: nginx:alpine
    container_name: web-nginx
    networks:
      - lnmp-net
    ports:
      - "8080:80"
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ./nginx/html:/usr/share/nginx/html
    restart: always

  mysql:
    image: mysql:8.0
    container_name: data-mysql
    networks:
      - lnmp-net
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: lnmp
    restart: always

  php:
    image: php:fpm
    container_name: php-fpm
    networks:
      - lnmp-net
    volumes:
      - ./nginx/html:/var/www/html
    restart: always
    depends_on:
      - mysql

volumes:
  mysql-data:

networks:
  lnmp-net:
    driver: bridge

2. secerts 用法

secrets:
  名字:
    file: 密码文件

服务里:
  secrets:
    - 名字

环境变量:
  XXX_PASSWORD_FILE: /run/secrets/名字

3. healthcheck 用法

healthcheck:
  test: curl -f http://localhost || exit 1
  interval: 5s     # 每5秒查一次
  timeout: 2s      # 超时2秒算失败
  retries: 3       # 重试3次
  start_period: 10s # 启动后10秒再开始查

Nginx / Apache / Web 服务

test: curl -f http://localhost || exit 1

MySQL

test: mysql -uroot -p123456 -e "SELECT 1" || exit 1

Redis

test: redis-cli ping || exit 1

PHP-FPM

test: php-fpm -t || exit 1

4. build 用法

services:
  web:
    build: .   # 表示在当前目录找 Dockerfile 构建

指定 Dockerfile 文件名

build:
  context: .        # 构建目录
  dockerfile: Dockerfile.php   # 指定文件

构建时传参数(ARG)

build:
  context: .
  args:
    - VERSION=8.0
    - NAME=test

5. working_dir 用法

php:
  image: php:fpm
  working_dir: /var/www/html   # 一进容器就在这里

6. entrypoint 用法

entrypoint = 主程序(发动机)

command = 命令参数(油门)

固定程序用 entrypoint

临时命令用 command

entrypoint: nginx -g "daemon off;"
或
entrypoint: ["nginx", "-g", "daemon off;"]

7. command 用法

command = 容器启动后执行什么命令

覆盖镜像默认命令

想让容器干嘛就写啥

command: php -S 0.0.0.0:8000
或
command: ["php", "-S", "0.0.0.0:8000"]

8. configs 用法

# 顶层定义:声明有哪些配置
configs:
  nginx_conf:       	# 配置名(自定义)
    file: ./nginx.conf  # 宿主机文件路径

services:
  nginx:
    image: nginx
    configs:
      - source: nginx_conf   # 对应顶层config名
        target: /etc/nginx/nginx.conf  # 容器内路径
        uid: "101"           # 可选:文件所有者UID
        gid: "101"           # 可选:文件所有者GID
        mode: 0444           # 可选:权限(默认只读0444){insert\_element\_2\_}

9. user 用法

安全:不让容器用 root 乱跑命令

权限最小化:避免黑客拿到高权限

解决宿主机文件权限问题(最常用!)

符合生产规范

# 用户名
user: nginx

# UID
user: 101

# UID:GID
user: "101:101"

10. privileged 用法

给容器开启最高权限(特权模式),让容器几乎拥有和宿主机一样的权限。

默认容器是受限的,加了 privileged: true 后:

  • 可以读写宿主机设备 /dev/*
  • 可以挂载硬盘、操作内核模块
  • 可以用一些需要高权限的程序(如 Docker in Docker)

场景:

  • Docker in Docker(容器里跑 Docker)
  • 需要操作宿主机设备:U 盘、显卡、串口、 /dev 下设备
  • 需要挂载磁盘、格式化、修改分区
  • 运行需要特殊内核权限的程序(如数据库、网络抓包工具)
services:
  test:
    image: ubuntu
    privileged: true
    command: sleep infinity

四、 Docker Compose 安装

需要已装 Docker Engine

docker --version

二进制安装

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

docker-compose --version

官方源安装

# 1. 安装 yum-utils(提供 yum-config-manager)
sudo yum install -y yum-utils

# 2. 添加 Docker 官方 YUM 源
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo3

# 阿里云 Docker 源(国内首选)
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# yum 安装
sudo yum install -y docker-compose-plugin

标题:Docker Compose 基础
作者:zhongts
地址:http://zhongts.cc:8080/articles/2026/04/06/1775458994874.html