Docker & Gitlab CI/CD deploy laravel app returns SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed in pipeline
At first, excuse me for the long thread. I'm trying to be as understandable as possible so here we go.
I just have a real hard time understanding Gitlab CI CD and it frustrates me. I’ve been trying to understand it and get it to work for the longest time but I just can’t get my head around it or get it to work.
I have a extremely simple Laravel installation with Tailwind, Vue etc. It doesn’t have any logic yet, its just frontend html for now. I am trying to set up a pipeline for this future application in which I can test and deploy my app every time I merge/push to the main branch.
I installed Laravel envoy and that I understand perfectly fine. Its a script that uses blade syntax in which I can connect to my server(DigitalOcean droplet) and run script right on there using SSH.
But there has to be a space in between my local environment and my webserver in which I can test my app before it goes live and that is where Docker comes in to play and also the place where I lose all the understanding I have of pipelines.
In this guide on Gitlab they go through how to setup CI CD with a laravel app and they also show you how you can “easily” test your app on a Docker image before you can deploy it live.
I get that docker is in essence just another server apart from your local environment and live environment in which you are free to do what ever wish to do with your application, right?
In the gitlab-ci.yml file I use a mysql image which is needed for the laravel app. In the same file under the unit_test section I am trying to migrate the database to what I presume is the Docker image and not my webserver. Therefore I need MySQL credentials to the docker image and not my webserver. But I don’t know what these credentials are or where I can get/change them. I don’t know the host, username or password to access Mysql from the image. It is driving me nuts.
Gitlab-ci.yml
image: registry.gitlab.com/rainieren/invoice_app:latest
services:
- mysql:5.7
variables:
MYSQL_DATABASE: invoice
MYSQL_ROOT_PASSWORD: ''
DB_HOST: mysql
DB_USERNAME: root
unit_test:
script:
# Install app dependencies
- composer install
# Set up .env
- cp .env.example .env
# Generate an environment key
- php artisan key:generate
# Run migrations
- php artisan migrate
# Run tests
- vendor/bin/phpunit
deploy_production:
script:
# Add the private SSH key to the build environment
- '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 *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
# Run Envoy
- ~/.composer/vendor/bin/envoy run deploy
environment:
name: production
url: http://invoiceapp.rainierlaan.nl
when: manual
only:
- main
Dockerfile
# Set the base image for subsequent instructions
FROM php:8.0
# Update packages
RUN apt-get update
# Install PHP and composer dependencies
RUN apt-get install -qq git curl libmcrypt-dev libjpeg-dev libpng-dev libfreetype6-dev libbz2-dev libzip-dev zip
# Clear out the local repository of retrieved package files
RUN apt-get clean
#Install mcrypt
RUN pecl install mcrypt-1.0.4
# Enable mcrypt
RUN docker-php-ext-enable mcrypt
# Install needed extensions
# Here you can install any other extension that you need during the test and deployment process
RUN docker-php-ext-install pdo_mysql zip
# Install Composer
RUN curl --silent --show-error "https://getcomposer.org/installer" | php -- --install-dir=/usr/local/bin --filename=composer
# Install Laravel Envoy
RUN composer global require "laravel/envoy=~1.0"
I have tried changing the host from mysql to localhost to 127 to db and they all gave me different errors.
Error I got when changing the host from mysql to localhost
SQLSTATE[HY000] [2002] No such file or directory
Error I got when changing the host from localhost to db
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Error I got when changing the host from db to 127.0.0.1
SQLSTATE[HY000] [2002] Connection refused
I also tried to change the login credentials to root and no password and a user and password I made on my server and both gave me errors as-well
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
At first I thought those credentials were to access mysql on my webserver. I SSH’d into my webserver and created a new user and gave it all the permissions. Then I put those credentials into the gitlab-ci.yml file but that still didn’t work. Then I though maybe it tried to access MySQL on the image but I don't know how to do that.
I have ran my pipeline for 7-8 times now, all of which failed on the exact same point, Trying to migrate the database. My question is, how can I get past this? There must be a more efficient way to test these credentials without running the pipeline a hunder times before right?
I also tried following this guide on MySQL https://hub.docker.com/_/mysql where it says I can connect to MySQL but then it says Unable to find image 'mysql:latest' locally.
Please can anyone help me with this issue.
Please or to participate in this conversation.