GitHub Actions:如何在最新版本的Windows和macOS上运行docker-compose?

5

我有一个GitHub Action的工作流程用于PR,其中包含一个任务执行.NET Core虚拟测试,需要同时使用PostgreSQL和SQL Server实例。

任务定义:

run-test:
  needs: [lint-commit, lint-code]
  strategy:
    matrix:
      os: [ubuntu-latest]
      dotnet: [3.1.201]
    fail-fast: false
  runs-on: ${{ matrix.os }}
  steps:
    - uses: actions/checkout@v2
    - name: Build the docker-compose stack
      run: docker-compose --file "./MessingUp.Tests/docker-compose.yml" up --detach
    - name: Setup .NET Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: ${{ matrix.dotnet }}
    - name: Run Tests
      run: dotnet test

还有docker compose文件,./MessingUp.Tests/docker-compose.yml:

version: "3.7"

services:

  sqlserver:
    image: mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04
    restart: unless-stopped
    environment:
      - MSSQL_PID=Express
      - ACCEPT_EULA=Y
      - SA_PASSWORD=MyPassword!
    ports:
      - 1433:1433

  postgres:
    image: postgres:12.3-alpine
    restart: unless-stopped
    environment:
      - POSTGRES_HOST_AUTH_METHOD=trust
      - POSTGRES_DB=postgres
      - POSTGRES_USER=root
      - POSTGRES_PASSWORD=root
    command:
      - postgres
      - -c
      - max_prepared_transactions=100
    ports:
      - 5432:5432
    # Needed because the postgres container does not provide a health check
    healthcheck:
      test: pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
      interval: 1m30s
      timeout: 10s
      retries: 3
      start_period: 40s

简而言之,上面的代码可行,我唯一的问题是如果我不仅使用ubuntu-latest,我想知道如何使用docker-compose

os: [windows-latest, macos-latest, ubuntu-latest]

在非 ubuntu-latest 操作系统中,它并不能真正起作用: :
 Build the docker-compose stack15s
##[error]Process completed with exit code 1.
Run docker-compose --file "./MessingUp.Tests/docker-compose.yml" up --detach
Creating network "messinguptests_default" with the default driver
Pulling sqlserver (mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04)...
2019-CU3-ubuntu-18.04: Pulling from mssql/server
image operating system "linux" cannot be used on this platform
##[error]Process completed with exit code 1.

macos-latest:

 Build the docker-compose stack0s
##[error]Process completed with exit code 127.
Run docker-compose --file "./MessingUp.Tests/docker-compose.yml" up --detach
/Users/runner/runners/2.169.1/work/_temp/ccb3e6a1-fc89-4099-9bf2-2f4ca4bb1fb8.sh: line 1: docker-compose: command not found
##[error]Process completed with exit code 127.

使用ubuntu-latest时的输出:

Build the docker-compose stack43s

Run docker-compose --file "./MessingUp.Tests/docker-compose.yml" up --detach
Creating network "messinguptests_default" with the default driver
Pulling sqlserver (mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04)...
2019-CU3-ubuntu-18.04: Pulling from mssql/server
Digest: sha256:e064843673f08f22192c044ffa6a594b0670a3eb3f9ff7568dd7a65a698fc4d6
Status: Downloaded newer image for mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04
Pulling postgres (postgres:12.3-alpine)...
12.3-alpine: Pulling from library/postgres
Digest: sha256:bd975ce4ddb0cf271a3c0132afaa16894ccdbadd0a1e81aa5d1b12727bb43779
Status: Downloaded newer image for postgres:12.3-alpine
Creating messinguptests_sqlserver_1 ... 
Creating messinguptests_postgres_1  ... 

Creating messinguptests_sqlserver_1 ... done

Creating messinguptests_postgres_1  ... done
1个回答

2
我看到两个不同的问题导致在windows-latestmac-os上执行docker-compose命令失败。 windows-latest使用Windows容器作为Docker后端引擎,而您正在尝试使用与Windows后端不兼容的基于Linux的镜像(mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04)。这就是为什么会出现错误消息:
Pulling sqlserver (mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04)...
2019-CU3-ubuntu-18.04: Pulling from mssql/server
image operating system "linux" cannot be used on this platform
如果您访问专门用于所使用镜像的DockerHub页面,您可以阅读到以下内容:

Microsoft SQL Server官方镜像,适用于Docker Engine的Linux

您应该使用与Windows容器后端兼容的不同镜像才能在windows-latest上运行容器。目前,有两个与Windows容器后端兼容的官方镜像: 上述DockerHub页面都提到:

Microsoft SQL Server Express适用于Windows容器

考虑到这一点,您应该在windows-latest runner上使用其中之一来使docker-compose命令正常工作。
您可以在此处了解有关Docker中的SQLServer的更多信息
至于macos-latest runner,在这种情况下错误消息略有不同:
/Users/runner/runners/2.169.1/work/_temp/ccb3e6a1-fc89-4099-9bf2-2f4ca4bb1fb8.sh: line 1: docker-compose: command not found

没有安装docker-compose二进制文件,因此找不到它。如果您查看GitHub Actions虚拟环境页面,可以探索每个运行程序包含的软件。 ubuntu-latestwindows-latest都已经包含了docker-compose命令,但是macos-latest没有包含它。这是由于一些许可问题。您必须使用brew自己安装该命令:

[...]
runs-on: macOS-latest
steps:
  - name: Install docker
    run: |
      brew install docker docker-machine docker-compose
      sudo docker –version
[...]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接