2

I've built a docker-compose file that creates 2 services (PHP and MariaDB). Somehow I cannot connect to the database from the PHP service: Within the PHP service, a Laravel-app is running.

The error message (redirect is a table within the database):

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from redirects)

All env-variables should be correct.

Here is my docker-compose.yml:

version: "3.7"
services:
  faaren_backend:
    image: php:alpine
    container_name: FAA-Backend
    volumes:
      - "./:/faaren_backend"
    working_dir: /faaren_backend
    command: "php artisan serve --host=0.0.0.0 --port=8000"
    ports:
      - 8000:8000
    build:
      context: docker/php
      dockerfile: dev.Dockerfile

  faaren_database:
    image: mariadb
    container_name: FAA-Database
    ports:
    - "3306:3306"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: ${DB_DATABASE}
    volumes:
    - "faa-db-data:/var/lib/mysql/data"

volumes:
  faa-db-data: {}

My dev.Dockerfile

FROM php:7.3-fpm

RUN apt-get update
RUN apt-get update && apt-get install -y libzip-dev \
         && docker-php-ext-install zip

RUN apt-get update && apt-get install -y libmagickwand-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN pecl install imagick
RUN docker-php-ext-enable imagick

RUN apt-get install $PHPIZE_DEPS && \
    pecl install xdebug && docker-php-ext-enable xdebug && \
    docker-php-ext-install pdo_mysql pcntl

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

VOLUME /faaren_backend

And finally my .env-file:

DB_CONNECTION=mysql
DB_HOST=faaren_backend
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=

Mostly I've followed this tutorial: https://medium.com/swlh/laravel-with-docker-compose-de2190569084

1
  • Did you try taking the ports value for the db out of quotes? Commented Dec 1, 2019 at 12:50

2 Answers 2

2

You need change your docker database host:

DB_CONNECTION=mysql
DB_HOST=faaren_database <-- edit
DB_PORT=3306
DB_DATABASE=faaren_db
DB_USERNAME=root
DB_PASSWORD=

And in docker-compose.yml faaren_backend section add:

depends_on:
  - faaren_database

Hope it's help.

Sign up to request clarification or add additional context in comments.

Comments

1

Looks like you deal with a race condition. You need to use depends_on directive to control startup order and use wait script to make sure that service ready to accept connections:

version: "3.7"
services:
  faaren_backend:
    image: php:alpine
    container_name: FAA-Backend
    volumes:
      - "./:/faaren_backend"
    working_dir: /faaren_backend
    command: entrypoint.sh
    ports:
      - 8000:8000
    depends_on:
      - faaren_database
    build:
      context: docker/php
      dockerfile: dev.Dockerfile

  faaren_database:
    image: mariadb
    container_name: FAA-Database
    ports:
      - "3306:3306"
    environment:
      MYSQL_ALLOW_EMPTY_PASSWORD: 1
      MYSQL_DATABASE: ${DB_DATABASE}
    volumes:
      - "faa-db-data:/var/lib/mysql/data"

volumes:
  faa-db-data: {}

There are a bunch of ways to check service availability, sometimes wait-for-it can be really handy. In the example below used netcat(you need to install it into the container).

Also, it'a good idea to put a script into a separated file:

entrypoint.sh

#!/usr/bin/env bash
until nc -w 1 -z faaren_database 3306; do
  >&2 echo "Mysql is unavailable - sleeping"
  sleep 1
done
sleep 10
>&2 echo "Mysql is up - executing command"

php artisan serve --host=0.0.0.0 --port=8000

UPDATE: Specify correct DB_HOST how Dmitry suggested as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.