'Docker compose: getaddrinfo for mariadb failed

I'm quite bad at creating docker-compose and I currently have a problem when I try to build a new project.

After the build I can't access my mariadb server within my php app container, I've got this error:

PDO::__construct(): php_network_getaddresses: getaddrinfo for mariadb failed: Name does not resolve

So I know something is misconfigured but I don't know what, yet. I tried many changes but nothing that worked.

Here is my current docker-compose.yml:

version: "3.8"

networks:
  # used by some services (php) to communicate with other docker-compose.yaml
  censored.com:
    external:
      name: censored.com

services:
  app:
    build:
      context: .
      target: symfony_php
      args:
        - secret=id=composerauth,src=${HOME}/.composer/auth.json
    restart: unless-stopped
    healthcheck:
      interval: 10s
      timeout: 3s
      retries: 3
      start_period: 30s
    environment:
      APP_ENV: dev
      HOST: www.censored.lan
    networks:
      - default
      - censored.com
    volumes:
      - ./:/srv/app:rw,cached
      - ./docker/php/conf.d/symfony.dev.ini:/usr/local/etc/php/conf.d/symfony.ini
      - ${HOME}/.composer/auth.json:/root/.composer/auth.json
      # If you develop on Linux, comment out the following volumes to just use bind-mounted project directory from host
      - ./var/cache:/srv/app/cache:rw
      - ./var/log:/srv/app/logs:rw
    depends_on:
      - mariadb

    extra_hosts:
      - www.censored.lan:127.0.0.1

  nginx:
    build:
        context: .
        target: symfony_nginx
        args:
          - secret=id=composerauth,src=${HOME}/.composer/auth.json
    restart: unless-stopped
    depends_on:
      - app
    environment:
      NGINX_DOMAIN: www.censored.lan
    ports:
      - 8001:80
    volumes:
      - ./docker/nginx/templates/dev.conf.template:/etc/nginx/templates/default.conf.template:ro
      - ./docker/nginx/rules/rules.dev.conf:/etc/nginx/rules.conf:ro
      - ./public:/srv/app/public:ro
      - ./src:/srv/app/src:ro

  mariadb:
    image: mariadb:10.7
    environment:
      MYSQL_ROOT_PASSWORD: changeme
      MYSQL_DATABASE: database
      MYSQL_USER: user
      MYSQL_PASSWORD: changeme
    networks:
      - default
      - censored.com
    ports:
      - '3307:3306'
    restart: on-failure
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data: {}

Can someone help me to fix the issue please?

Thanks !



Solution 1:[1]

The problem wasn't related to my docker-compose configuration file.

My app is a PHP Symfony app and it was doing a "cache:clear" after the initial "composer install" during the build. The "cache:clear" was triggering calls to the database which wasn't ready yet. To solve this I just had to set my mariadb version to my "DATABASE_URL" parameter in my Symfony app, to avoid useless database queries.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TEOL