Cant run migrations using dockerised Laravel projects connnecting to seperate MySQL DB

  docker, laravel, laravel-8, mysql

So i have setup a docker container to run each of my laravel project, as well as a docker container with a MySQL database, this is because I have a few projects that run off the same database. I am able to successfully connect to the database through the app when pulling the apps up on a browser, but when I run any migrate command I get the following error:

    Creating laravel_artisan_run ... done

   IlluminateDatabaseQueryException 

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

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

      +33 vendor frames 
  34  artisan:37
      IlluminateFoundationConsoleKernel::handle(Object(SymfonyComponentConsoleInputArgvInput), Object(SymfonyComponentConsoleOutputConsoleOutput))
ERROR: 1

If I go to my application in a browser I can run my project with no issues, it only seems to be migrations. I don’t understand why I am able to connect to the database, but am unable to connect when running a migration?

I have the following config for my MySQL container:

version: '3'

networks:
  laravel:
  app-shared:
    driver: bridge

services:

  mysql:
    image: mysql:latest
    container_name: medlab_mysql
    restart: unless-stopped
    tty: true
    ports:
      - 33061:3306
    environment:
      MYSQL_DATABASE: 5i
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel
      - app-shared

And the following for my laravel project container:

version: '3'

networks:
  laravel:
  medlab-docker-mysql_app-shared:
    external: true

services:
  site:
    build:
      context: .
      dockerfile: nginx.dockerfile
      args:
        - NGINXUSER=${NGINXUSER:-www-data}
        - NGINXGROUP=${NGINXGROUP:-www-data}
    container_name: nginx-6i
    ports:
      - 8088:80
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - php
    networks:
      - laravel
      - medlab-docker-mysql_app-shared

  php:
    build:
      context: .
      dockerfile: php.dockerfile
      args:
        - PHPUSER=${PHPUSER:-www-data}
        - PHPGROUP=${PHPGROUP:-www-data}
    container_name: php-6i
    volumes:
      - ./src:/var/www/html:delegated
    networks:
      - laravel
      - medlab-docker-mysql_app-shared

  composer:
    build:
      context: .
      dockerfile: composer.dockerfile
      args:
        - PHPUSER=${PHPUSER:-www-data}
        - PHPGROUP=${PHPGROUP:-www-data}
    container_name: composer-6i
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    user: ${PHPUSER:-www-data}
    entrypoint: ['composer', '--ignore-platform-reqs']
    networks:
      - laravel

  npm:
    image: node:16
    container_name: npm-6i
    volumes:
      - ./src:/var/www/html
    ports:
      - 3000:3000
      - 3001:3001
    working_dir: /var/www/html
    entrypoint: ['npm']
    networks:
      - laravel

  artisan:
    build:
      context: .
      dockerfile: php.dockerfile
      args:
        - PHPUSER=${PHPUSER:-www-data}
        - PHPGROUP=${PHPGROUP:-www-data}
    container_name: artisan-6i
    volumes:
      - ./src:/var/www/html:delegated
    working_dir: /var/www/html
    entrypoint: ['php', '/var/www/html/artisan']
    networks:
      - laravel

And my .env file containers the following database config

DB_CONNECTION=mysql
DB_HOST=medlab_mysql
DB_PORT=3306
DB_DATABASE=5i
DB_USERNAME=homestead
DB_PASSWORD=secret

Source: Laravel

Leave a Reply