Pipelines, PHPUnit, Docker Compose and Laravel

Ethan Glover June 2, 2021

I'm trying to run PHPUnit tests on BitBucket Pipelines and I'm getting some issues with connecting to the database. I can run everything as normal by downloading the repo into a new directory, clearing docker images and pruning, and simply running everything Pipelines does.

I can't figure out why, if Pipelines is running a docker container, it can't connect to the database. It installs composer packages (which includes a key generation) but fails on the step for migrate:fresh.

I have this for docker-compose-bitbucket.yml

version: "3.7"
services:
app:
build:
args:
user: pcsAdmin
uid: 1000
context: ./
dockerfile: Dockerfile
image: pcs
container_name: pcs-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- pcs
db:
image: mysql:5.7
container_name: pcs-db
restart: unless-stopped
environment:
MYSQL_DATABASE: "${DB_DATABASE:-pcs}"
MYSQL_ROOT_PASSWORD: "${DB_PASSWORD:-root}"
MYSQL_PASSWORD: "${DB_PASSWORD:-root}"
MYSQL_USER: "${DB_USERNAME:-pcsAdmin}"
SERVICE_TAGS: dev
SERVICE_NAME: mysql
ports:
- 33306:3306
networks:
- pcs
nginx:
image: nginx:1.17-alpine
container_name: pcs-nginx
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker-compose/nginx:/etc/nginx/conf.d
networks:
- pcs
networks:
pcs:
driver: bridge


In bitbucket-pipelines.yml

image: php:8-fpm
pipelines:
default:
- step:
image: docker/compose:latest
services:
- docker
caches:
- docker
script:
- docker-compose -f docker-compose-bitbucket.yml build
- docker-compose -f docker-compose-bitbucket.yml up -d
- docker-compose exec -T app mv .env.pipelines .env
- docker-compose exec -T app composer install
- docker-compose exec -T app php artisan migrate:fresh --seed
- docker-compose exec -T app ./vendor/bin/phpunit
- docker-compose down 

.env.pipelines

APP_ENV=local
APP_KEY=base64:LcfzQg8uGaNiyaoPmn1LOpciqIIHrTTBq/vUdOKt4FY=
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=pcs
DB_USERNAME=pcsAdmin
DB_PASSWORD=root 

 Dockerfile

FROM php:8-fpm

# Arguments defined in docker-compose.yml
ARG user
ARG uid

# Install system dependencies
RUN apt-get update && apt-get install -y \
git \
curl \
libpng-dev \
libonig-dev \
libxml2-dev \
zip \
unzip \
vim

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Install XDebug
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.mode=remote" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.mode=coverage,develop" >> /usr/local/etc/php/conf.d/xdebug.ini

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user

# Set working directory
WORKDIR /var/www

USER $user

 

Screen Shot 2021-06-02 at 10.27.11 AM.png

1 answer

1 accepted

1 vote
Answer accepted
Ethan Glover June 2, 2021

I stopped caching docker, and added a prune before any other command.

docker system prune -a -f

I also added a sleep 5 after the up command. Which seems to do the trick.

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events