When I try to run the command php artisan migrate
, an error occurs:
SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = docker and table_name = migrations and table_type = ‘BASE TABLE’)
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql mysqli
docker-compose.yml:
version: '3'
networks:
laravel:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
volumes:
- ./src:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
ports:
- "8088:80"
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:8.0
container_name: db
ports:
- "3307:3306"
expose:
- "3306"
volumes:
- ./cache/mysql:/var/lib/mysql
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: docker
MYSQL_USER: root
MYSQL_PASSWORD: root
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
depends_on:
- nginx
- php
volumes:
- ./src:/var/www/html
working_dir: /var/www/html
networks:
- laravel
.env:
DB_CONNECTION=mysql
DB_HOST="db"
DB_PORT=3306
DB_DATABASE=docker
DB_USERNAME=root
DB_PASSWORD=root
I try too:
DB_HOST=127.0.0.1
DB_HOST=localhost
thank you very good
2
Answers
In docker-compose.yml
In MySQL Dockerfile
please chnage the ports in docker-compose.yml File , it works for me
ports:
– 8889:3306
—-My-> docker-compose.yml File —
version: ‘3.8’
services:
Web Server Service
nginx:
image: nginx:alpine
container_name: nginx
ports:
– "8080:80"
volumes:
– ./src:/var/www
– ./nginx/conf.d/:/etc/nginx/conf.d/
depends_on:
– app
– db
#Database Service
db:
image : mysql
container_name : mysql
restart: unless-stopped
tty: true
ports:
– 8889:3306
volumes:
environment:
app:
container_name: app
build:
context: ./php
dockerfile: Dockerfile
volumes:
– ./src:/var/www
ports:
– "9000:9000"
working_dir: /var/www
phpMyAdmin:
# phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: phpmyadmin
ports:
– "3400:80"
depends_on:
– db
links:
– db
#pgadmin:
#image: dpage/pgadmin4
#container_name: pgAdmin
#ports:
# – "5050:80"
#depends_on:
# – db
#environment:
# PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: password