I'm having trouble connecting to the mysql database from my php/laravel container. I'm getting a connection refused all the time. The connection works just fine with Mysql Workbench though.
My docker-compose looks like this:
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "8080:80"
volumes:
- ./src:/var/www
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
- mysql
networks:
- laravel
mysql:
image: mysql:5.7.22
container_name: mysql
restart: unless-stopped
tty: true
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: project_db
MYSQL_USER: dev
MYSQL_PASSWORD: dev
MYSQL_ROOT_PASSWORD: dev
SERVICE_TAGS: dev
networks:
- laravel
php:
build:
context: .
dockerfile: Dockerfile
container_name: php_node
volumes:
- ./src:/var/www
ports:
- "9000:9000"
networks:
- laravel
And the relevant part of my.env looks like this:
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=project_db
DB_USERNAME=root
DB_PASSWORD=dev
I have tried removing everything and building the image again, but to no avail.
Can you guys spot my error?
Thanks in advance
On Docker for windows you can use host.docker.internal to access the host IP.
I answered this in the comments but I thought I'd duplicate it as a proper answer to help anyone who finds this in the future and save them reading all the comments
For extracting docker container names and their IPs use below command in your terminal:
docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)
It will show you something like:
/nginx - 172.18.0.4
/php - 172.18.0.3
/mysql - 172.18.0.2
Then use mysql container ip in your.env file
DB_HOST=172.18.0.2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.