1
votes

I try to set up Laravel project in Docker. My docker-compose.yml:

version: "2"
services:
  web:
    build:
      context: .
      dockerfile: Dockerfile
    environment:
      - MYSQL_DATABASE=identy
      - MYSQL_USER=root
      - MYSQL_PASSWORD=654321
      - MYSQL_HOST=db
    ports:
      - "8080:80"
    volumes:
      - .:/var/www
    depends_on:
      - db
    networks:
      - identy-network

  db:
    image: mysql:8.0
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=654321
      - MYSQL_USER=root
      - MYSQL_PASSWORD=654321
      - MYSQL_DATABASE=identy
    volumes:
      - "mysql_data:/var/lib/mysql"
      - ./data/schema.sql:/docker-entrypoint-initdb.d/schema.sql
    networks:
      - identy-network

volumes:
  mysql_data:
    driver: local

networks:
  identy-network:

My Dockerfile:

FROM php:7.4-apache

RUN apt-get update \
    && apt-get install -y vim git zlib1g-dev mariadb-client libzip-dev \
    && docker-php-ext-install zip mysqli pdo_mysql \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_host=host.docker.internal' >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo 'xdebug.remote_port=9000' >>  /usr/local/etc/php/conf.d/xdebug.ini \
    && a2enmod rewrite \
    && sed -i 's!/var/www/html!/var/www/public!g' /etc/apache2/sites-available/000-default.conf \
    && mv /var/www/html /var/www/public \
    && curl -sS https://getcomposer.org/installer \
    | php -- --install-dir=/usr/local/bin --filename=composer \
    && echo "AllowEncodedSlashes On" >> /etc/apache2/apache2.conf

WORKDIR /var/www

.env:

...
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=identy
DB_USERNAME=root
DB_PASSWORD=654321
...

When I run docker exec identy-api_web_1 php artisan migrate I get error: ** Illuminate\Database\QueryException

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

I have no idea why it doesn't work.

3

3 Answers

0
votes

I think, You don't have permission to access the database from remote host on the mysql server. You need to allow access remotely to root from any host on the mysql server.

GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.144.3' IDENTIFIED BY 'PASSWORD' WITH GRANT OPTION;
0
votes

I think you should remove the volumes data and try again.

0
votes

You don't need to define MYSQL_USER=root. Try this one (it creates identy db as well):

db:
  image: mysql:8.0
  ports:
    - "3306:3306"
  environment:
    - MYSQL_ROOT_PASSWORD=654321
  networks:
    - identy-network
  volumes:
    - "mysql_data:/var/lib/mysql"
  entrypoint: sh -c "
    echo 'CREATE DATABASE IF NOT EXISTS identy;' > /docker-entrypoint-initdb.d/init.sql;
    /usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci"