Docker / WSL / Symfony / MySQL – SQLSTATE[HY000] [2002] Connection refused

I’m using WSL 2 and trying to connect to my MySQL database though my docker app container (Symfony 4.4). The other posts about this subject did not helped me to find a solution, that is why I asking you now. Don’t hesitate to ask me to add other files which can help us to get more informations about this problem.

All of the containers are ostensibly running fine (details here).

Then I go inside my app container with docker exec -it app-backend-fairfair bash and run composer install with success. It works fine (details here).

Finally I run php bin/console doctrine:database:create but it won’t work even if I have an full database access though Adminer on the localhost:8081, I still get the SQLSTATE[HY000] [2002] Connection refused error. The full error message here.


Files used

Here is my docker .env file :

SERVER_PORT=8080
ADMINER_PORT=8081

# name and group of your user
HOST_USER=shamzic

# Path to store data in your machine
MYSQL_VOLUME=/var/docker-volume/fairfair/database

# Password for root user
MYSQL_ROOT_PASSWORD=root

Inside the .env symfony container file :

...

DATABASE_URL=mysql://root:[email protected]:3306/main
...

The docker-compose.yaml file :

version: '3.2'

services:
    app-backend-fairfair:
        build:
            context: .
            dockerfile: app.dockerfile
            args:
                USER: ${HOST_USER}
        volumes:
            - '../../:/usr/src/backend-fairfair'
        networks:
            - app-backend-fairfair
            - db
        restart: on-failure
        container_name: app-backend-fairfair
    server-backend-fairfair:
        build:
            context: .
            dockerfile: server.dockerfile
        networks:
            - app-backend-fairfair
        ports:
            - ${SERVER_PORT}:80
        restart: on-failure
        depends_on:
            - app-backend-fairfair
        container_name: server-backend-fairfair
    db:
        image: mysql:8.0
        build:
            context: .
            dockerfile: database.dockerfile
        volumes:
            - ${MYSQL_VOLUME}:/var/lib/mysql
        ports:
            - 3306:3306
        environment:
            MYSQL_ROOT_HOST: '%'
            MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
        networks:
            - db
        restart: on-failure
        container_name: db
    adminer:
        build:
            context: .
            dockerfile: adminer.dockerfile
        ports:
            - ${ADMINER_PORT}:8080
        restart: on-failure
        networks:
            - db
        depends_on:
            - db
        container_name: adminer
networks:
    app-backend-fairfair:
        name: app-backend-fairfair
    db:
        name: db

I’m conviced that this can be fixed by few changes… Maybe inside the docker-compose.yaml file ?

Thank you by advance for your help !

Source: Docker Questions

LEAVE A COMMENT