skip to Main Content

I am having an issue with my Laravel in Docker.

Currently when I run php artisan migrate inside my container I get the error

SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name does not 
resolve (SQL: select * from information_schema.tables where table_schema = 
app_database and table_name = migrations and table_type = 'BASE TABLE')

However, I am able to connect to the mysql using Sequel Pro and I am able to see the database created app_database

My docker_compose.yml is as below:

version: '3'


services:
  nginx:
    build:
      context: .
      dockerfile: Dockerfile_nginx
    container_name: nginx_webserver
    restart: unless-stopped
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - database
    networks:
      - laravel

  database:
    image: mysql:8.0
    container_name: docker_database
    environment:
      - "MYSQL_DATABASE=app_database"
      - "MYSQL_USER=app_db_user"
      - "MYSQL_PASSWORD=app_db_password"
      - "MYSQL_ROOT_PASSWORD=password"
    volumes:
      - ./mysql/db_data:/var/lib/mysql
    ports:
      - "3306:3306"

  php:
    build:
      context: .
      dockerfile: Dockerfile_php
    container_name: my_app
    volumes:
      - ./src:/var/www
      - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
    ports:
      - "9000:9000"
    networks:
      - laravel
    depends_on:
      - database

networks:
  laravel:
    driver: bridge

The .env of my laravel app is

DB_CONNECTION=mysql
DB_HOST=docker_database
DB_PORT=3306
DB_DATABASE=app_database
DB_USERNAME=app_db_user
DB_PASSWORD=app_db_password

Can anybody share some insight?

I have tried everything online.

2

Answers


  1. It looks like you need to add network to database container

    networks:
      - laravel
    
    Login or Signup to reply.
  2. I got a similar error. In my case the mistake was using docker run which spun up a new container which was in it’s own isolated network instead of using docker compose run which will run the command on one of the containers spun up after docker compose up which will have access to the other containers in the network

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