Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection refused when running Laravel artisan command with Docker

I'm running Laravel 5.4 in Docker. This is my docker-compose.yml file:

version: '2'

services:
  app:
    container_name: laravel_app
    image: webdevops/php-apache-dev:ubuntu-16.04
    links:
      - mysql
    depends_on:
      - mysql
    ports:
      - 8888:80
    volumes:
      - .:/app
    environment:
      docker: 'true'
      WEB_DOCUMENT_ROOT: '/app/public'
      WEB_NO_CACHE_PATTERN: '\.(.*)$$'
      working_dir: '/app'
  mysql:
    image: mariadb:latest
    ports:
      - 8889:80
    environment:
      MYSQL_ROOT_PASSWORD: 'dev'
      MYSQL_DATABASE: 'dev'
      MYSQL_USER: 'dev'
      MYSQL_PASSWORD: 'dev'

This is the relevant part of my .env file:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=8889
DB_DATABASE=dev
DB_USERNAME=dev
DB_PASSWORD=dev

I am able to see the Laravel welcome page - that side of things works. But when I run php artisan migrate I get this error:

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = dev and table_name = migrations)

I have tried fiddling with the host and port parameters in the .env file.

like image 782
GluePear Avatar asked May 09 '18 13:05

GluePear


2 Answers

this worked for me: put the name of my mysql container instead of 127.0.0.1

NAME CONTAINERS

project-db --> container mysql
project-app --> container laravel

DB_CONNECTION=mysql
DB_HOST=project-db
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=root
like image 170
Diego Santa Cruz Mendezú Avatar answered Sep 16 '22 16:09

Diego Santa Cruz Mendezú


First edit your docker-compose.yml.

mysql:
    image: mariadb:latest
    ports:
      - 8889:3306

After that set the correct DB port in .env.

Your DB port is wrong. You are trying to connect the exposed port inside the docker. In this case you should use DB_PORT=3306 in your .env.

like image 39
Peter Kota Avatar answered Sep 18 '22 16:09

Peter Kota