Google Laravel artisan 的 Cloud Build 失败,涉及数据库连接

Google Cloud Build failed for Laravel artisan, involving database connection

我正在尝试将源代码打包为图像并通过 Google Cloud Build

进行构建

这是我的 docker 文件:

FROM php
RUN cd ~ && curl -sS https://getcomposer.org/installer -o composer-setup.php
RUN cd ~ && php composer-setup.php --install-dir=/usr/local/bin --filename=composer
ADD ./mysource /app
WORKDIR /app
RUN cd /app && composer update --ignore-platform-reqs && composer install --ignore-platform-reqs
RUN cd /app && composer require laravel/horizon && php artisan vendor:publish --provider="Laravel\Horizon\HorizonServiceProvider"

原来构建失败并且日志显示: (最新日志在顶部)

Step #1: 
ERROR: build step 1 "gcr.io/cloud-builders/docker" failed: exit status 1
Finished Step #1
Step #1: The command '/bin/sh -c cd /app && composer update --ignore-platform-reqs && composer install --ignore-platform-reqs' returned a non-zero code: 1
Step #1: Script @php artisan package:discover handling the post-autoload-dump event returned with error code 1
Step #1: SQLSTATE[HY000] [2002] Connection refused 
Step #1: In PDOConnection.php line 46:
Step #1: SQLSTATE[HY000] [2002] Connection refused 
Step #1: In PDOConnection.php line 50:
Step #1: chema.tables where table_schema = forge and table_name = explorers) 
Step #1: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_s 
Step #1: 
Step #1: In Connection.php line 664:
Step #1: 
Step #1: > @php artisan package:discover
Step #1: > Illuminate\Foundation\ComposerScripts::postAutoloadDump
Step #1: Generating optimized autoload files

我正在寻找类似 services in bitbucket pipeline, which u could use during build process . I also referenced the issue raised here 的内容,但这些回复并没有解决我的问题。

感谢任何想法

根据 https://github.com/GoogleCloudPlatform/community/issues/351#issuecomment-370056462

Unfortunately, during the build phase, the CloudSQL connection is not available.

只想post更新我的解决方案。 我原来添加了一个 MySQL 容器 创建一个特定的网络并在所有其他步骤中指定了名称(容器) --network <network_name> ,所以容器在以下步骤中可以通过 mysql 容器的名称直接连接。

为了安全起见,我添加了一个容器作为缓冲区,以确保 mysql 容器有足够的时间来构建和启动。

steps:
- name: 'gcr.io/cloud-builders/docker'
  args: ['network','create','gcb_network']
- name: 'gcr.io/cloud-builders/docker'
  args: ['run','--name=mysql','-p','3306:3306','--network','gcb_network','--env','MYSQL_USER=<user>','--env','MYSQL_PASSWORD=<pwd>','--env','MYSQL_ROOT_PASSWORD=<user>','--env','MYSQL_DATABASE=<dbname>','-d','mysql/mysql-server:5.7']
- name: 'ubuntu'
  args: ['sleep','10']
  id: 'MYSQLDeferrer'
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/<projectname>/<img_name>:latest','--network','gcb_network','-f','<path_to_docker_file>','.']
  waitFor:
    - 'MYSQLDeferrer'
  env:
  - 'DB_HOST='
  - 'DB_USERNAME='
  - 'DB_PASSWORD='
images: ['gcr.io/<projectname>/<img_name>']

根据您的设置,您may/may不需要环境。