i dockerized my laravel project (9.25.1) and my problem is my query in tinker totally works and php artisan migrate
works too but when i query in controller i get mysql error SQLSTATE[HY000] [2002] Connection refused
Dockerfile:
FROM php:8.1-fpm-alpine
RUN docker-php-ext-install pdo pdo_mysql sockets
RUN curl -sS https://getcomposer.org/installer | php -- \
--install-dir=/usr/local/bin --filename=composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
WORKDIR /app
COPY . .
RUN composer install
docker-composer.yml:
version: '3.8'
services:
main:
build:
context: .
dockerfile: Dockerfile
command: 'php artisan serve --host=0.0.0.0'
volumes:
- .:/app
ports:
- 8000:8000
db:
platform: linux/x86_64
image: mysql:8.0
environment:
MYSQL_DATABASE: main
MYSQL_USER: admin
MYSQL_ROOT: admin
MYSQL_PASSWORD: admin
MYSQL_ROOT_PASSWORD: root
volumes:
- ./storage/dbdata:/var/lib/mysql
ports:
- 4306:3306
.env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=4306
DB_DATABASE=main
DB_USERNAME=admin
DB_PASSWORD=admin
CodePudding user response:
Check this. If your application will run in a docker container, you need to use internal port. In your scenario it's 3306
. If you want to use tinker run it on docker container.
For example:
docker-compose exec main php artisan tinker
For other artisan commands:
docker-compose exec main php artisan migrate