Can not connect to DB from docker container (winter cms, laravel)

149 Views Asked by At

I have a winter cms project and docker compose setup as following -

version: '3'
services:
  web:
    image: nginx:latest
    container_name: 12k_nginx_dev
    ports:
      - 8080:80
    environment:
      - DB_TYPE=mysql
      - DB_HOST=db #DB_HOST should match the service name of the database container
      - DB_DATABASE=12k_winter
      - DB_USERNAME=root
      - DB_PASSWORD=root
      - PHP_MEMORY_LIMIT=-1
    volumes:
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./app:/var/www/html
  php:
    build: ./docker/php/
    container_name: 12k_php_dev
    depends_on:
      - db
    volumes:
      - ./app:/var/www/html
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
    links:
      - db
    environment:
      XDEBUG_CONFIG: "remote_host=host.docker.internal remote_enable=1"
      PHP_IDE_CONFIG: "serverName=Docker"
  db:
    image: mariadb:latest
    command: --max_allowed_packet=32505856
    environment:
      MYSQL_ROOT_PASSWORD: 'root'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'root123'
      MYSQL_DATABASE: '12k_winter'
    volumes:
      - $HOME/sites/12knots_winter_new/mysql/data:/var/lib/mysql
    ports:
      - 3306:3306
#volumes:
#    mysqldata: {}

And .env :

DB_CONNECTION=mysql
DB_HOST=db
#DB_HOST="172.18.0.1"
DB_PORT=3306
DB_DATABASE="12k_winter"
DB_USERNAME="root"
DB_PASSWORD="root123"

When I try to run php artisan winter:up from container, I get such error

enter image description here

The thing is that I have a project with exact same setup, and it works. Other docker projects work without such error.

I am currently debugging this but without success (

I tried to change env DB_HOST to be IP address but it didn't help. Rebuilding the container doesn't give any results.

I also found that I can not login to mysql from DB container, I get this error - ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

but at the same time in another project instance with same setup (but other DB volumes) I can connect to mysql

3

There are 3 best solutions below

0
Eugene B. On BEST ANSWER

Finally I found a solution, I've added "networks" to docker-compose, and it looks like this now -

version: '3'
    services:
      web:
        image: nginx:latest
        networks:
          - backend
        container_name: 12k_nginx_dev
        ports:
          - 8080:80
        environment:
          - DB_TYPE=mysql
          - DB_HOST=db #DB_HOST should match the service name of the database container
          - DB_DATABASE=12k_winter
          - DB_USERNAME=root
          - DB_PASSWORD=root
          - PHP_MEMORY_LIMIT=-1
        volumes:
          - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
          - ./app:/var/www/html
      php:
        build: ./docker/php/
        networks:
          - backend
        container_name: 12k_php_dev
        depends_on:
          - db
        volumes:
          - ./app:/var/www/html
          - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
          - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
        links:
          - db
        environment:
          XDEBUG_CONFIG: "remote_host=host.docker.internal remote_enable=1"
          PHP_IDE_CONFIG: "serverName=Docker"
      db:
        image: mariadb:latest
        networks:
          - backend
        container_name: db
        command: --max_allowed_packet=32505856
        environment:
          MYSQL_ROOT_PASSWORD: 'root'
          MYSQL_USER: 'root'
          MYSQL_PASSWORD: 'password'
          MYSQL_DATABASE: '12k_winter'
        volumes:
          - $HOME/sites/12knots_winter_new/mysql/data:/var/lib/mysql
        ports:
          - 3306:3306
    networks:
      backend:
        # Use a custom driver which takes special options
        driver: bridge
    
    
    in the .env file DB_HOST = db
2
Rahmat Waisi On

First choose a name for your database service in the docker-compose file:

  db:
    image: mariadb:latest
    container_name: WHATEVER

Then make sure that the container and services are up and running then just change the host to the database container name in your docker-compose file which is WHATEVER herea nd it will work properly.

DB_CONNECTION=mysql
DB_HOST=WHATEVER
DB_PORT=3306
DB_DATABASE="database_name"
DB_USERNAME="username"
DB_PASSWORD="password"

Also, I recommend you define another connection in your config/database.php file and name it mariadb cause your image is using the latest version on MariaDB, and change the .env as well.

DB_CONNECTION=mysql

0
Henning On

It doesn't make sense to set MYSQL_ROOT_PASSWORD to 'root' and then use MYSQL_USER with 'root' and MYSQL_PASSWORD as 'root123'....

If you want to use the database as root (not recommended), just set MYSQL_ROOT_PASSWORD and MYSQL_DATABASE for the database and DB_DATABASE, DB_USERNAME, DB_PASSWORD for the web container.

Otherwise just leave MYSQL_ROOT_PASSWORD out and set the other variables to the same values for both containers.