Ошибка Laravel Docker SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo: имя или служба неизвестны

#mysql #reactjs #laravel #docker #docker-compose

#mysql #reactjs #laravel #докер #docker-compose

Вопрос:

У меня есть проект API laravel с интерфейсом react SPA.

Структура папок:

 app
   ...
   Dockerfile
api 
   ...
   Dockerfile
docker-compose.yml
 

В laravel (api) .env

 DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=root
DB_PASSWORD=secret
 

В api (laravel) Dockerfile

 FROM php:7.4

RUN apt-get update -y amp;amp; apt-get install -y openssl zip unzip git
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-install pdo_mysql
WORKDIR /myapp/api
COPY . .
RUN composer install
RUN php artisan config:clear
RUN cp .env.example .env
RUN php artisan key:generate
RUN php artisan migrate
RUN php artisan db:seed
EXPOSE 8000
CMD php artisan serve --host=0.0.0.0
 

В приложении (реагирует) Dockerfile

 FROM node:alpine

WORKDIR /myapp/app 
COPY . .
RUN npm install 
EXPOSE 3000
CMD npm start
 

На docker-compose.yml

 version: '3.7'
# Services
services:
  # MySQL Database
  db:
    image: mysql:5.7.22
    container_name: db
    restart: always
    environment:
      MYSQL_DATABASE: 'myapp'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'secret'
      MYSQL_ROOT_PASSWORD: 'secret'
      MYSQL_ROOT_USER: root
    ports:
      - 3308:3306
    volumes:
      - ./db/:/var/lib/mysql/
    networks:
      - app
  # Laravel REST API
  api:
    build:
      context: ./api
      dockerfile: Dockerfile
    container_name: api
    environment:
      DB_HOST: db
      DB_DATABASE: 'myapp'
      DB_USERNAME: 'root'
      DB_PASSWORD: 'secret'
    depends_on:
      - db
    ports:
      - 8000:8000
    volumes:
      - ./api:/myapp/api
    networks:
      - app

  # React SPA
  app:
    build:
      context: ./app
      dockerfile: Dockerfile
    container_name: app
    tty: true
    ports:
      - 3000:3000
    volumes:
      - ./app:/myapp/app
    depends_on:
      - api
    networks:
      - app

# Networks
networks:
  app:
    driver: bridge
 

Затем запускаем docker с помощью

 docker-compose down
docker system prune -a
docker-compose up --build
 

Это выдает ошибку при php artisan migrate

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

Обновить:

Я получаю эти журналы, если удаляю команды migrate amp; seed artisan. Так что-то делать с sock?

журнал базы данных