Skip to content

docker-compose容器编排

python编写, 用于批量管理多个容器.

举例:一个项目 = nginx转发 + nuxt服务 + mongo数据库

安装

linux通用脚本

shell
# release发布: https://github.com/docker/compose/releases
sudo curl -L "https://github.com/docker/compose/releases/download/v2.29.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

权限配置:sudo chmod +x /usr/local/bin/docker-compose

yaml/yml编写规则

  • 一个定义服务、网络和卷的YAML文件. Compose文件的默认路径是 ./docker-compose.yml【扩展名可以是:.yml 或 .yaml】

一个yaml文件一般可分为3层

依次是:版本服务(可以多个)、其他配置(网络/卷挂载/全局规则)

shell
# docker版本号
version: "3.9"
# 服务
services:
  web:
    build: .
    # 依赖redis, 让docker-compose优先启动redis
    depends_on:
      - redis
  redis:
    # 依赖镜像
    image: redis
   # 暴露端口
    ports:
      - "3306"
    # 定义全局挂载卷
    volumes:
      - db-data:/var/lib/postgresql/data

实战

一键部署wordpress,参考文档

  1. vim docker-compose.yaml 填入以下配置信息
  1. docker-compose up -d 【-d为后台运行】
yaml
version: '3.3'
services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       WORDPRESS_DB_NAME: wordpress
volumes:
    db_data: {}