Search code examples
phplaraveldockerlaravel-sail

Docker is running properly but is not accessible with browser on 0.0.0.0:8000


I have a Laravel-based application and I created a docker-compose file using laravel/sail:

services:
    app:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.2/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
            - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            IGNITION_LOCAL_SITES_PATH: '${PWD}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql/mysql-server:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}: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
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local

I'm trying to connect the app container using browser. Since $APP_PORT has seted on 8000, I expect to see laravel first page in hopefully http://0.0.0.0:8000 or http://127.0.0.1:8000, but I give:

This site can’t be reached

in Chrome, same thing in Firefox.

Here's the result of docker container ls:

CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS                    PORTS                                                                                        NAMES
663fc79a5338   sail-8.2/app             "start-container"        23 minutes ago   Up 23 minutes             0.0.0.0:5173->5173/tcp, :::5173->5173/tcp, 8000/tcp, 0.0.0.0:8000->80/tcp, :::8000->80/tcp   app_name-app-1
7311792e085f   mysql/mysql-server:8.0   "/entrypoint.sh mysq…"   23 minutes ago   Up 23 minutes (healthy)   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060-33061/tcp                                   app_name-mysql-1

I ran Docker before and for example 0.0.0.0:8088 works properly in another project (which not using laravel-sail).

It's also notable that I checked Stackoverflow and docker community and none of them could solve my problem, my guess is.

It might be related to ssl/tls related issues maybe(?)

Note:

OS: Ubuntu 20.04.4 LTS

Docker version: 20.10.17

I tried a different port (8088), and it wasn't helpful.

I served Laravel using php artisan serve --host=0.0.0.0 and it works fine.

I tried curl 0.0.0.0:80 in the app container and it works fine (Laravel home page).

I tried curl 0.0.0.0:80 in my machine (out of Docker) and it gave me:

curl: (56) Recv failure: Connection reset by peer

I also checked my chrome proxy setting and it seems fine (Disabled).

I disabled my firewall (ufw disable) and nothing changed.

Update

app docker logs:

 sudo docker logs app_name-app-1
usermod: UID '0' already exists
2024-01-05 22:33:56,718 INFO Set uid to user 0 succeeded
2024-01-05 22:33:56,722 INFO supervisord started with pid 1
2024-01-05 22:33:57,727 INFO spawned: 'php' with pid 10
[Fri Jan  5 22:33:58 2024] PHP 8.2.10 Development Server (http://localhost:80) started
2024-01-05 22:33:59,005 INFO success: php entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2024-01-05 22:43:16,632 INFO reaped unknown pid 27 (terminated by SIGKILL)
2024-01-05 22:43:16,633 INFO reaped unknown pid 26 (terminated by SIGKILL)

Solution

  • The key message in your log is PHP 8.2.10 Development Server (http://localhost:80) started that shows that your app is binding to localhost.

    That means that it'll only accept connections from localhost. In a container, localhost is the container itself. So it won't accept connections from outside the container.

    You need to have it bind to 0.0.0.0 to get it to accept connections from outside the container. You haven't shown your Dockerfile, so I can't see what command you use to start your app. In your post, you mention adding the option --host=0.0.0.0 to your php serve command and it's something like that you need to do.