0

I have the following docker-compose file to build a LAMP stack:

version: "3.1"
services:

  mariadb:
    image: mariadb:latest
    container_name: mariadb
    working_dir: /application
    volumes:
      - .:/application
    environment:
      - MYSQL_ROOT_PASSWORD=test
      - MYSQL_DATABASE=test
      - MYSQL_USER=test
      - MYSQL_PASSWORD=test
    ports:
      - "8052:3306"

  webserver:
    image: nginx:latest
    container_name: nginx
    working_dir: /application
    volumes:
      - .:/application
      - ./dev/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "8050:80"

  php-fpm:
    build: dev/php-fpm
    container_name: php-fpm
    working_dir: /application
    volumes:
      - .:/application
      - ./dev/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini

This gets my stack up and running then in my .env file I have:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=8052
DB_DATABASE=test
DB_USERNAME=test
DB_PASSWORD=test

When Laravel tries to connect I get the following error message: SQLSTATE[HY000] [2002] No such file or directory

After some research I found that changing DB_HOST to 127.0.0.1 changes the way PHP tries to connect (TCP instead of a socket). After trying that I get a different error: SQLSTATE[HY000] [2002] Connection refused

The thing is php artisan migrate works and connects fine. I can also connect to the DB from the CLI and a GUI tool.

Is there something wrong in my docker-compose yaml causing this?

Docker file:

FROM php:fpm
WORKDIR "/application"

ARG DEBIAN_FRONTEND=noninteractiv

# Install selected extensions and other stuff
RUN apt-get update \
    && apt install ca-certificates apt-transport-https \
    && apt install wget \
    && docker-php-ext-install pdo_mysql && docker-php-ext-enable pdo_mysql \
    && wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add - \
    && echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list \
    && apt-get -y --no-install-recommends install php7.3-memcached php7.3-mysql php7.3-redis php7.3-xdebug \
    && apt-get clean; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

Nginx conf:

server {
    listen 80 default;

    client_max_body_size 108M;

    access_log /var/log/nginx/application.access.log;


    root /application/public;
    index index.php;

    if (!-e $request_filename) {
        rewrite ^.*$ /index.php last;
    }

    location ~ \.php$ {
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        fastcgi_param APP_ENV "dev";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
    }

}

6
  • 1
    DB_HOST=mariadb Commented Dec 17, 2018 at 12:08
  • Internal traffic would still use the standard ports (or rather the ports defined in the image / Dockerfile) wouldn't it? Not the host / container mapping? Commented Dec 17, 2018 at 12:10
  • @u_mulder that didn't seem to make a difference I still get SQLSTATE[HY000] [2002] Connection refused (and configs/cache where cleared) Commented Dec 17, 2018 at 12:15
  • 1
    @twigg In addition to u_mulder's change, change DB_PORT=8052 to DB_PORT=3306. You may need to add link directives, but I'm not sure if those are still valid in v3. Commented Dec 17, 2018 at 12:17
  • 2
    Found it. docs.docker.com/compose/networking By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name. Commented Dec 17, 2018 at 12:38

1 Answer 1

1

If you find yourself unable to run php artisan commands which require database connection after adding "DB_HOST=mariadb"

such as:

  • php artisan migrate
  • php artisan db:seed

Solution:

Add the following to your hosts file: 127.0.0.1 [name_of_container]

Name of container in your case is: mariadb

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

1 Comment

Can you explain that further? Why should one modify the hosts file manually, if Docker already provides everything? Additionally, which file should be edited?

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.