title: SQLSTATE[HY000] [2002] Connection refused on laravel and docker setup
body: <p>I am trying to set up a docker environment for laravel development. I am facing issues with the migrate command. I am pasting her the compose file and env file. I think my settings are correct because if I do a normal DB operation from a controller then the DB connection works fine. It just gives the error only with the migrate command.
  I also have artisan in my compose file because I wanted to keep everything in the container.</p>
  <pre><code>
  networks:
    laravel:
  
  services:
    nginx:
      image: nginx:stable-alpine
      container_name: nginx
      ports:
        - &quot;8080:80&quot;
      volumes:
        - ./src:/var/www/html
        - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
      depends_on:
        - php
        - mysql
      networks:
        - laravel
  
    mysql:
      image: mysql:5.7.29
      container_name: mysql
      restart: unless-stopped
      tty: true
      ports:
        - &quot;3306:3306&quot;
      environment:
        MYSQL_DATABASE: homestead
        MYSQL_USER: homestead
        MYSQL_PASSWORD: secret
        MYSQL_ROOT_PASSWORD: secret
        SERVICE_TAGS: dev
        SERVICE_NAME: mysql
      volumes:
        - ./mysql:/var/lib/mysql
      networks:
        - laravel
  
    php:
      build:
        context: .
        dockerfile: Dockerfile
      container_name: php
      volumes:
        - ./src:/var/www/html
        - ./src/php/ini:/usr/local/etc/php
      ports:
        - &quot;9000:9000&quot;
      networks:
        - laravel
  
    artisan:
      build:
        context: .
        dockerfile: Dockerfile
      container_name: artisan
      volumes:
        - ./src:/var/www/html
      depends_on:
        - mysql
      working_dir: /var/www/html
      entrypoint: ['php', '/var/www/html/artisan']
      networks:
        - laravel
  </code></pre>
  <p>my env file looks like this</p>
  <pre><code>DB_CONNECTION=mysql
  DB_HOST=mysql
  DB_PORT=3306
  DB_DATABASE=homestead
  DB_USERNAME=root
  DB_PASSWORD=secret
  </code></pre>
  <p>My docker file looks like</p>
  <pre><code>FROM php:7.3-fpm-alpine
  WORKDIR /var/www/html
  RUN docker-php-ext-install mysqli pdo pdo_mysql &amp;&amp; docker-php-ext-enable mysqli
  </code></pre>
  <p>When I run</p>
  <pre><code>docker-compose run --rm artisan  migrate
  </code></pre>
  <p>then I get</p>
  <pre><code>SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = homestead and table_name = migrations and table_type = 'BASE TABLE')
  </code></pre>
  <p>Any idea what might be going wrong here?</p>
  <p>I have one more thing to share so I am editing my question.</p>
  <p>I executed this command.</p>
  <p><code>docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)</code></p>
  <p>to find the IP address of all the containers.</p>
  <p>I used the IP address in the DB_HOST as follows</p>
  <pre><code>DB_CONNECTION=mysql
  DB_HOST=172.19.0.2
  DB_PORT=3306
  DB_DATABASE=homestead
  DB_USERNAME=root
  DB_PASSWORD=secret
  </code></pre>
  <p>and it worked but this can not be a permanent solution.</p>
  <p>Somehow docker is not able to map the container-name to the Internal IP.</p>
  <p>I have no idea about the root cause.</p>