4.云原生技术培训系列 Docker Compose安装

 

Compose 是用于定义和运行多容器 Docker 应用程序的工具。通过 Compose,您可以使用 YML 文件来配置应用程序需要的所有服务。然后,使用一个命令,就可以从 YML 文件配置中创建并启动所有服务。

version: '3.1'
services:
  nodebb:
    image: nibrev/nodebb:v1.17.1
    restart: unless-stopped
    environment:
      DATABASE: "redis"
      DB_NAME: "0"
      DB_HOST: "redis"
      DB_PORT: "6379"
    volumes:
      - ./data/nodebb:/data
    ports:
      - "4567:4567"

  redis:
    image: redis
    restart: unless-stopped
    volumes:
      - ./data/redis:/data

一、Compose 安装

Linux

Linux 上我们可以从 Github 上下载它的二进制包来使用,最新发行的版本地址:https://github.com/docker/compose/releases

运行以下命令以下载 Docker Compose 的当前稳定版本: sudo curl -L "http://************/docker-compose/v1.28.2/docker-compose-Linux-x86_64" -o /usr/local/bin/docker-compose

将可执行权限应用于二进制文件: sudo chmod +x /usr/local/bin/docker-compose

创建软链: sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

测试是否安装成功: docker-compose --version

macOS

Mac 的 Docker 桌面版和 Docker Toolbox 已经包括 Compose 和其他 Docker 应用程序,因此 Mac 用户不需要单独安装 Compose。Docker 安装说明可以参阅 MacOS Docker 安装。

windows PC

Windows 的 Docker 桌面版和 Docker Toolbox 已经包括 Compose 和其他 Docker 应用程序,因此 Windows 用户不需要单独安装 Compose。Docker 安装说明可以参阅 Windows Docker 安装。

二、常用操作

➜  backup docker-compose -h

Usage:  docker compose [OPTIONS] COMMAND

Docker Compose

Options:
      --ansi string                Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto")
      --env-file string            Specify an alternate environment file.
  -f, --file stringArray           Compose configuration files
      --profile stringArray        Specify a profile to enable
      --project-directory string   Specify an alternate working directory
                                   (default: the path of the Compose file)
  -p, --project-name string        Project name

Commands:
  build       Build or rebuild services
  convert     Converts the compose file to platform's canonical format
  cp          Copy files/folders between a service container and the local filesystem
  create      Creates containers for a service.
  down        Stop and remove containers, networks
  events      Receive real time events from containers.
  exec        Execute a command in a running container.
  images      List images used by the created containers
  kill        Force stop service containers.
  logs        View output from containers
  ls          List running compose projects
  pause       pause services
  port        Print the public port for a port binding.
  ps          List containers
  pull        Pull service images
  push        Push service images
  restart     Restart containers
  rm          Removes stopped service containers
  run         Run a one-off command on a service.
  start       Start services
  stop        Stop services
  top         Display the running processes
  unpause     unpause services
  up          Create and start containers

Run 'docker compose COMMAND --help' for more information on a command.
  • docker-compose up -d
  • docker-compose down
  • docker-compose logs -f
  • dokcer-compose restart

三、配置服务的实际启动顺序

depends_on

只定义了docker-compose的启动先后顺序,但实际上并没有等服务完全就绪后再启动。

比如说数据库服务先启动,初始化加启动需要花费1分钟。 而后端程序后启动,但启动时发现数据库还未就绪,直接失败。

wait-for-it.sh

https://github.com/vishnubob/wait-for-it

通过启动脚本,控制真正的启动顺序,等待数据库完全就绪后,再启动后台服务。

wait-for-it.sh host:port [-s] [-t timeout] [-- command args]
-h HOST | --host=HOST       Host or IP under test
-p PORT | --port=PORT       TCP port under test
                            Alternatively, you specify the host and port as host:port
-s | --strict               Only execute subcommand if the test succeeds
-q | --quiet                Don't output any status messages
-t TIMEOUT | --timeout=TIMEOUT
                            Timeout in seconds, zero for no timeout
-- COMMAND ARGS             Execute command with args after the test finishes

点击下载wait-for-it.sh

示例使用

version: '3'
services:
  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 1
  web:
    build: .
    depends_on:
      - db
    ports:
      - "9999:8080"
    command: ["wait-for-it.sh", "db:3306", "--", "catalina.sh", "run"]

四、练习-编辑guns的docker-compose.yaml文件

数据库+后端服务

注意启动顺序