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
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
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