[英]Docker Github workflow with database dependency: No such container
我試圖弄清楚使用數據庫依賴項(實際上是 MySQL)的 Github 工作流發生了什么,但我找不到任何解釋或解決方案。
這是我的工作流程 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
此工作流的目的是運行單元測試。 但是在運行單元測試之前,需要先執行一些遷移來創建測試表。 我正在使用 Phalcon 框架,但我認為沒關系。 實際上發生了什么,我一直收到“連接被拒絕”,而我確信 MySQL 容器已啟動並准備好使用,並且 MySQL 容器的 IP 地址不正確,但我的下一個容器無法訪問容器“產品單元測試”。
我要做的是在執行單元測試之前在容器內本地安裝 MySQL 服務器,但我認為這不是最佳實踐。 我需要一個單獨的 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
因為您將mysql -uroot -e...
傳遞到容器的入口點,所以它沒有啟動守護程序。
將環境變量MYSQL_USER
、 MYSQL_PASSWORD
等傳遞給容器是正確的方法。
--default-authentication-plugin=mysql_native_password
作為運行參數將確保它以正確的方式創建。
我在 github 服務中看不到錯誤,但是日志似乎沒有停止啟動。
在花了很多時間試圖找到解決方案之后,我想出了這個:
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
...
首先,我獲得了 MySQL 服務 ID,然后我獲得了 Github 創建的網絡的網關地址。 這樣,我確定 MySQL 容器主機和端口是什么。
可以幫助搜索相同問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.