简体   繁体   English

具有数据库依赖性的 Docker Github 工作流:没有这样的容器

[英]Docker Github workflow with database dependency: No such container

I am trying to figure out whats going on with Github workflows using a database dependency (MySQL actually), but I can't find any explanation or solution for my situation.我试图弄清楚使用数据库依赖项(实际上是 MySQL)的 Github 工作流发生了什么,但我找不到任何解释或解决方案。

This is my workflow yaml file:这是我的工作流程 yaml 文件:

name: docker

on:
    push:
        # publish image as master=dev or on new tag
        # except on document and ci changes
        branches:
            - main
        tags:
            - '*'
        paths-ignore:
            - '**.md'
            - '.github/workflows/*yml'

    # always run tests on merge
    # except on document and ci changes
    pull_request:
        paths-ignore:
            - '**.md'
            - '.github/workflows/*yml'

jobs:
    unit_test:
        runs-on: ubuntu-latest
        if: github.event_name == 'pull_request'
        services:
            mysql:
                image: mysql:8
                ports:
                    - 3306
                env:
                    MYSQL_USER: phalcon
                    MYSQL_PASSWORD: secret
                    MYSQL_DATABASE: shop_products_test
                    MYSQL_ROOT_PASSWORD: root
                options: --health-cmd="mysqladmin ping" --health-interval=5s --health-timeout=2s --health-retries=3
        steps:
            - name: Create docker network
              run: docker network create marketplace-network
            - name: Check out Site Repository 📄
              uses: actions/checkout@v2
            - name: Create .env file
              run: cp .env.example .env
            - name: Replace environment variables
              run: |
                  sed -i 's/MYSQL_HOST.*/MYSQL_HOST=0.0.0.0/g' .env
                  sed -i 's/MYSQL_PORT.*/MYSQL_PORT=${{ job.services.mysql.ports[3306] }}/g' .env
            - name: Build docker image
              run: docker build -t marketplace_shop_products .
            - name: Running unit test
              run: docker-compose up products-unit-test

The purpose of this workflow is to run the unit test.此工作流的目的是运行单元测试。 But before running the unit test, there are some migrations need to be executed before to create test tables.但是在运行单元测试之前,需要先执行一些迁移来创建测试表。 I am using Phalcon framework, but I think it doesn't matter.我正在使用 Phalcon 框架,但我认为没关系。 What happens actually, I keep getting "Connection Refused" while I am sure that MySQL container is up and ready to be used and the IP address for MySQL container is correct, but somehow, it's not available or can't be reached by my next container "products-unit-test".实际上发生了什么,我一直收到“连接被拒绝”,而我确信 MySQL 容器已启动并准备好使用,并且 MySQL 容器的 IP 地址不正确,但我的下一个容器无法访问容器“产品单元测试”。

What I am about to do is to install MySQL server locally inside the container before executing the unit test, but I don't think it's the best practice.我要做的是在执行单元测试之前在容器内本地安装 MySQL 服务器,但我认为这不是最佳实践。 I need a separate MySQL container that unit test container connect to it in order to run migrations and do the test.我需要一个单独的 MySQL 容器,单元测试容器连接到它,以便运行迁移并进行测试。

products-unit-test_1  | Copying php extensions to container ...
products-unit-test_1  | Run migrations ...
products-unit-test_1  | 
products-unit-test_1  | Phalcon DevTools (3.4.11)
products-unit-test_1  | 
products-unit-test_1  | Running migrations:
products-unit-test_1  | ERROR: SQLSTATE[HY000] [2002] Connection refused
products-unit-test_1  | 
products-unit-test_1  | PHPUnit 7.5.20 by Sebastian Bergmann and contributors.
products-unit-test_1  | 
products-unit-test_1  | Runtime:       PHP 7.3.28 with Xdebug 2.9.1
products-unit-test_1  | Configuration: /src/tests/phpunit.xml
products-unit-test_1  | 
products-unit-test_1  | ....                                                                4 / 4 (100%)
products-unit-test_1  | 
products-unit-test_1  | Time: 230 ms, Memory: 4.00 MB
products-unit-test_1  | 
products-unit-test_1  | OK (4 tests, 4 assertions)
shop_products_products-unit-test_1 exited with code 0

Because you are passing mysql -uroot -e... to the entry point of the container it isn't starting the daemon.因为您将mysql -uroot -e...传递到容器的入口点,所以它没有启动守护程序。

Passing the env variable MYSQL_USER , MYSQL_PASSWORD etc to the container is the correct way.将环境变量MYSQL_USERMYSQL_PASSWORD等传递给容器是正确的方法。

--default-authentication-plugin=mysql_native_password as a run argument will ensure its created the right way. --default-authentication-plugin=mysql_native_password作为运行参数将确保它以正确的方式创建。

I can't see the error in the github services however the logs don't seem to sover the startup.我在 github 服务中看不到错误,但是日志似乎没有停止启动。

After spending many hours trying to find a solution, I came up with this one:在花了很多时间试图找到解决方案之后,我想出了这个:

jobs:
    unit_test:
        runs-on: ubuntu-latest
        if: github.event_name == 'pull_request'
        services:
            mysql:
                image: mysql:8
                ports:
                    - 3306
                options: --health-cmd="mysqladmin ping"
                         --health-interval=5s
                         --health-timeout=2s
                         --health-retries=3
        steps:
            - name: Get MySQL service ID
              id: mysql-service
              run: echo "::set-output name=container-id::$(docker ps | grep -i mysql | awk '{print $1}')"
            - name: Get Github network gateway address
              id: github-network
              run: echo "::set-output name=gateway-address::$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.Gateway}}{{end}}' ${{ steps.mysql-service.outputs.container-id }})"
            - name: Check out Site Repository 📄
              uses: actions/checkout@v2
            - name: Create .env file
              run: cp .env.example .env
            - name: Replace environment variables
              run: |
                  sed -i 's/MYSQL_HOST.*/MYSQL_HOST=${{ steps.github-network.outputs.gateway-address }}/g' .env
                  sed -i 's/MYSQL_PORT.*/MYSQL_PORT=${{ job.services.mysql.ports[3306] }}/g' .env
            ...

First thing, I get the MySQL service ID, then I get the gateway address for the created network by Github.首先,我获得了 MySQL 服务 ID,然后我获得了 Github 创建的网络的网关地址。 This way, I am sure what is the MySQL container host and port.这样,我确定 MySQL 容器主机和端口是什么。

May help someone who searches for same problem.可以帮助搜索相同问题的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM