skip to Main Content

I started a new Laravel 9.x + JsonApi project on docker-compose with 2 containters, ‘web’ and ‘mysql’. Artisan migrations, seeding and db:show works fine. But when I’m trying to load a page from browser or Postman, it returns DB error

`SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for mariadb failed: Temporary failure in name resolution

select * from redirects where redirects.id = 1 limit 1 `

The weird part is that my DB host is mysql and ‘mariadb’ string is never mentioned in my code (except some vendor files), nor in docker files, nor in .env file. I also checked container environment variables – nothing there.

Artisan scripts work fine.

Here are my settings, if it helps. Mariadb was never mentioned, it was mysql from the very beginning.

docker-compose.yml

version: "3.4"
services:
  mysql:
    image: mysql
    volumes: 
      - mysql_db:/var/lib/mysql
    ports:
      - 3306:3306
    environment:
      - MYSQL_ROOT_PASSWORD=***
      - MYSQL_DATABASE=url_shortener
  web:
    build:
      dockerfile: Dockerfile.web.dev
      context: .
    #image: php:alpine
    volumes:
      - "./:/app"
      - "/app/src/vendor"
      - web_root_home:/root
    working_dir: /app/src/
    command: "php artisan serve --host=0.0.0.0 --port=7999"
    ports:
      - 8000:7999
    depends_on:
      - mysql
volumes:
  mysql_db:
  web_root_home:

Dockerfile.web.dev

#FROM php
FROM bitnami/laravel
WORKDIR /app/src/
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN apt update
RUN apt install -y git build-essential vim mc mlocate
RUN pecl install xdebug
COPY ./src /app/src
COPY ./src/composer.json ./
COPY ./src/.env ./
RUN composer install
RUN php artisan key:generate

CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=8000"]

.env (DB part)

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=url_shortener
DB_USERNAME=root
DB_PASSWORD=***

app/config/database.php wasn’t changed

artisan db:show

root@a3c873713ee1:/app/src# php artisan db:show

  MySQL 8 ...................................................................
  Database .................................................... url_shortener
  Host ................................................................ mysql
  Port ................................................................. 3306
  Username ............................................................. root
  URL .......................................................................
  Open Connections ........................................................ 3
  Tables .................................................................. 6
  Total Size ........................................................ 0.09MiB

  Table .......................................................... Size (MiB)
  redirects ............................................................ 0.02
  failed_jobs .......................................................... 0.02
  migrations ........................................................... 0.02
  personal_access_tokens ............................................... 0.02
  password_resets ...................................................... 0.02
  users ................................................................ 0.02

migrations and seeding works fine, I can connect to mysql from the host laptop. All seeds data are there

I tried to clear config cache, didn’t help

root@a3c873713ee1:/app/src# php artisan config:clear

   INFO  Configuration cache cleared successfully.

I also reloaded containers, no luck as well

Stack trace isn’t useful error screenshot

I’m out of ideas what to try and where it takes this mariadb host at all.

2

Answers


  1. Chosen as BEST ANSWER

    I could solve the problem by defining the DATABASE_URL=mysql://root:PASSWORD@mysql/url_shortener in my .env file. I still have no idea where it got mariaDB host from.


  2. For me, this was a problem with bitnami’s laravel docker image. At build time they set certain environment variables which are then read (where, I wasn’t able to find, maybe it interacts with a bug in another library).

    Solution for me was setting DB_HOST, DB_CONNECTION, DB_DATABASE, DB_USERNAME and DB_PASSWORD to my "default" connection in docker-compose.yml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search