title: Laravel Docker SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
body: <p>I have a laravel api project with react SPA front-end.</p>
  <p>Folder structure:</p>
  <pre><code>app
     ...
     Dockerfile
  api 
     ...
     Dockerfile
  docker-compose.yml
  </code></pre>
  <p>On laravel (api) <code>.env</code></p>
  <pre><code>DB_CONNECTION=mysql
  DB_HOST=db
  DB_PORT=3306
  DB_DATABASE=myapp
  DB_USERNAME=root
  DB_PASSWORD=secret
  </code></pre>
  <p>On api (laravel) <code>Dockerfile</code></p>
  <pre><code>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
  </code></pre>
  <p>On app (react) <code>Dockerfile</code></p>
  <pre><code>FROM node:alpine
  
  WORKDIR /myapp/app 
  COPY . .
  RUN npm install 
  EXPOSE 3000
  CMD npm start
  </code></pre>
  <p>On <code>docker-compose.yml</code></p>
  <pre><code>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
  </code></pre>
  <p>Then running docker using</p>
  <pre><code>docker-compose down
  docker system prune -a
  docker-compose up --build
  </code></pre>
  <p>This gives error on <code>php artisan migrate</code></p>
  <pre><code>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')
  </code></pre>
  <p><strong>Update:</strong></p>
  <p>I am getting these logs if I remove migrate &amp; seed artisan commands. So something to deal with sock?</p>
  <p><a href="https://i.stack.imgur.com/KG3Id.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KG3Id.jpg" alt="db log" /></a></p>