GitLab CI with docker and laravel cannot connect to database
I have Gitlab CI set-up but it keeps failing when trying to run the database migrations:
SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: select * from information_schema.tables where table_schema = test_db and table_name = migrations)
My gitlab.yml file is:
before_script:
- bash .gitlab-ci.sh
- export APP_ENV=testing
services:
- mysql:8.0
variables:
MYSQL_DATABASE: test_db
MYSQL_ROOT_PASSWORD: testdb
DB_HOST: mysql
DB_USERNAME: root
DOCKER_DRIVER: overlay
cache:
paths:
- vendor/
- node_modules/
stages:
- test
- deploy
phpunit:php7.1:mysql8.0:
stage: test
image: php:7.1
services:
- mysql:8.0
script:
- echo "Running PHP Unit - php 7.1 mysql 8.0"
- php vendor/bin/phpunit --colors
deploy_production:
stage: deploy
script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
- eval $(ssh-agent -s)
- ssh-add <(echo "$SSH_PRIVATE_KEY")
- mkdir -p ~/.ssh
- '[[ -f /.dockerenv ]] && echo -e "Host *ntStrictHostKeyChecking nonn" > ~/.ssh/config'
- ~/.composer/vendor/bin/envoy run deploy
environment:
name: production
url: http://167.99.202.64
when: manual
only:
- master
My before bash script is as follows:
#!/bin/bash
# Install dependencies only for Docker.
[[ ! -e /.dockerinit ]] && [[ ! -e /.dockerenv ]] && exit 0
set -xe
# Update packages and install composer and PHP dependencies.
apt-get update -yqq
apt-get install git libcurl4-gnutls-dev libicu-dev libmcrypt-dev libvpx-dev libjpeg-dev libpng-dev libxpm-dev zlib1g-dev libfreetype6-dev libxml2-dev libexpat1-dev libbz2-dev libgmp3-dev libldap2-dev unixodbc-dev libpq-dev libsqlite3-dev libaspell-dev libsnmp-dev libpcre3-dev libtidy-dev -yqq
# Compile PHP, include these extensions.
docker-php-ext-install mbstring mcrypt pdo_mysql curl json intl gd xml zip bz2 opcache
# Install Composer and project dependencies
echo 'Installing composer'
curl -sS https://getcomposer.org/installer | php
php composer.phar install --no-plugins --no-scripts --dev
# Copy over testing configuration.
cp .env.testing .env
# Generate an application key. Re-cache
php artisan key:generate
php artisan config:cache
# Run database migrations
php artisan migrate
Finally my env.testing file:
APP_ENV=testing
APP_DEBUG=true
APP_KEY=key
DB_HOST=mysql
DB_DATABASE=test_db
DB_USERNAME=root
DB_PASSWORD=testdb
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
MAIL_DRIVER=log
I’m guessing Laravel inside the PHP7.1 container can’t see the MySQL container looking at the error message? Is declaring it as a service in the gitlab.yml not enough?
from Laravel Questions and Answers https://laravelquestions.com/php/gitlab-ci-with-docker-and-laravel-cannot-connect-to-database/
via Lzo Media
No comments:
Post a Comment