Search code examples
phpmysqllaraveldocker-composelaravel-sail

Laravel Sail - Migration are working but can't register new user


I've set up a new laravel project and here's the step I followed :

  • install laravel on docker using

     curl -s "https://laravel.build/example-app" | bash
    
  • changing mysql port as 3306 is already used in my machine. Files have changed

.env file

DB_CONNECTION=mysql
DB_HOST=0.0.0.0
DB_PORT=4306
DB_DATABASE=example_app
DB_USERNAME=sail
DB_PASSWORD=password

docker-compose file (mysql container section)

mysql:
    image: 'mysql/mysql-server:8.0'
    ports:
        - '${FORWARD_DB_PORT:-4306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ROOT_HOST: "%"
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 1
    volumes:
        - 'sail-mysql:/var/lib/mysql'
        - './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
    networks:
        - sail
    healthcheck:
        test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
        retries: 3
        timeout: 5s
  • build and run container

    ./vendor/bin/sail up

  • run 1st migration

Everything works fine at this point.

Then I install breeze package and run the migration

composer require laravel/breeze && php artisan migrate

Everything is still working at this point.

Then when I try to register a new user entering the adress localhost/register, I get the following error

lluminate \ Database \ QueryException
SQLSTATE[HY000] [2002] Connection refused

select count(*) as aggregate from `users` where `email` = [email protected]

Am I missing something in the .env or the docker-compose.yml ?

Thanks a lot


Solution

  • I found a fix by changing the environment variable with the Gateway IP

    Use this command to find this IP

    docker network inspect bridge
    

    you should have a result like the following :

    [
        {
            "Name": "bridge",
            "Id": "17da7215587ae714f631d213fed173550b69d38a5824c1998e1c3cd4b2e954b",
            "Created": "2022-10-26T19:26:38.585884296Z",
            "Scope": "local",
            "Driver": "bridge",
            "EnableIPv6": false,
            "IPAM": {
                "Driver": "default",
                "Options": null,
                "Config": [
                    {
                        "Subnet": "172.17.0.0/16",
                        "Gateway": "172.17.0.1"
                    }
                ]
            },
            "Internal": false,
            "Attachable": false,
            "Ingress": false,
            "ConfigFrom": {
                "Network": ""
            },
            "ConfigOnly": false,
            "Containers": {},
            "Options": {
                "com.docker.network.bridge.default_bridge": "true",
                "com.docker.network.bridge.enable_icc": "true",
                "com.docker.network.bridge.enable_ip_masquerade": "true",
                "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
                "com.docker.network.bridge.name": "docker0",
                "com.docker.network.driver.mtu": "1500"
            },
            "Labels": {}
        }
    ]
    

    use the Gateway entry in your .env file