Ed T.
Ed T.

Reputation: 325

Cannot run artisan migrate with docker-compose setup

I've got following setup and cannot get my head around why my docker-composer run --rm artisan migrate is struggling to connect to database with the following error message:

Error:

 Illuminate\Database\QueryException 

  SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'172.28.0.4' (using password: 
YES) (SQL: select * from information_schema.tables where table_schema = homestead and tabl
e_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

Laravel version: v7.3.0

Docker compose setup:

version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8080:80"
    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:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: homestead
      MYSQL_USER: homestead
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    networks:
      - laravel

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    ports:
      - "8183:80"
    depends_on:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: "3306"
      PMA_ARBITRARY: 1
      MYSQL_PASSWORD: secret
    restart: unless-stopped
    networks:
      - laravel

  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html
    ports:
      - "9000:9000"
    networks:
      - laravel

  composer:
    image: composer:latest
    container_name: composer
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    depends_on:
      - php
    networks:
      - laravel

  npm:
    image: node:13.7
    container_name: npm
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
    entrypoint: ['npm']

  artisan:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: artisan
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
    working_dir: /var/www/html
    entrypoint: ['/var/www/html/artisan']
    networks:
      - laravel

MySQL configuration in .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secure

All of the configurations seem correct to me, as I can view the database through phpmyadmin.

What I've tried so far, is rebuilding image, and flushing cache, as suggested on other stackoverflow questions, but that didn't seem to work for me either.

Any tips on where I've gone wrong, would be much appreciated.

Upvotes: 0

Views: 656

Answers (1)

Shizzen83
Shizzen83

Reputation: 3529

You set MySQL password as secret while it is secure in your .env file

Upvotes: 1

Related Questions