Docker build with laravel - SQLSTATE[HY000] [2002]

I’m getting error with my docker image after building the docker containers.

on docker-compose up -d command, the application is connected to a database.

but when I tried to run docker build -t to make an image and run it, I got 500 internal error php_network_getaddresses: getaddrinfo failed:

dockerfile

FROM webdevops/php-nginx:7.4-alpine

WORKDIR /app

# Install Laravel framework system requirements (https://laravel.com/docs/8.x/deployment#optimizing-configuration-loading)
RUN apk add oniguruma-dev postgresql-dev libxml2-dev
RUN docker-php-ext-install \
        bcmath \
        ctype \
        fileinfo \
        json \
        mbstring \
        pdo_mysql \
        pdo_pgsql \
        tokenizer \
        xml

# Copy Composer binary from the Composer official Docker image
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

ENV WEB_DOCUMENT_ROOT /app/public
ENV APP_ENV production

# Copy existing application directory contents to the working directory
COPY . .
RUN composer install

RUN composer install --no-interaction --optimize-autoloader --no-dev

# Optimizing Configuration loading
RUN php artisan config:clear
# Optimizing Route loading
RUN php artisan cache:clear

RUN php artisan view:cache

RUN chown -R application:application .

docker-compose.yml

version: "3.8"

services:
 #php service
 php:
  build: 
   context: .
   dockerfile: Dockerfile
  container_name: dtltrcy_phpv1
  volumes:
   - .:/var/www/html
   - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
  networks:
   - laravel

 #nginx service web server
 nginx:
  image: nginx:stable-alpine
  container_name: dtltrcy_nginxv1
  ports: 
   - "8080:80"
  volumes:
   - .:/var/www/html
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  depends_on:
   - php
   - mysql
  networks:
    - dataliteracyapp

 #mysql service
 mysql:
  image: mysql:8.0.21
  container_name: dtltrcy_mysqlv1
  restart: unless-stopped
  tty: true
  ports:
   - "3306:3306"
  volumes:
   - ./mysql:/var/lib/mysql
   - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  environment:
   MYSQL_DATABASE: database
   MYSQL_USER: ${DB_USERNAME}
   MYSQL_PASSWORD: ${DB_PASSWORD}
   MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
   MYSQL_ROOT_HOST: '%'
   SERVICE_TAGS: dev
   SERVICE_NAME: mysql
  networks:
   - laravel

 phpmyadmin:
  image: phpmyadmin/phpmyadmin
  restart: always
  container_name: dtltrcy_phpmyadminv1
  depends_on:
   - mysql
  ports:
   - "8081:80"
  environment:
   PMA_HOST: mysql
   MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
  networks:
   - laravel

#docker networks
networks:
 laravel:

.env

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root 
DB_PASSWORD=secret

tried to change .env file with these.

DB_HOST=127.0.0.1 ERROR → SQLSTATE[HY000] [2002] Connection refused

DB_HOST=localhost ERROR → SQLSTATE[HY000] [2002] No such file or directory

DB_HOST=172.19.0.3 ERROR → SQLSTATE[HY000] [2002] Operation time out

Container IP using docker inspect -f ‘{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ container_id

Today I had the same problem.
In case anyone has this problem, consider checking your IP address with ifconfig or ip addr or ipconfig.
Provide this address and include it in the .env file (DB_HOST).

It worked for me.

When using Docker compose, each service gets a local Docker DNS entry with its service name (unless configured differently), so the service mysql will be reachable at mysql, no need to mess with IPs that might change.