Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dommer's avatar

Laravel Sail bridge - file_get_contents: Connection refused

My application uses swagger which then calls file_get_contents at localhost:8080/openapi

But php cannot connect to this address:

file_get_contents(localhost:8080/openapi): Failed to open stream: Connection refused

my docker-compose.yml

version: "3"
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.1
            dockerfile: Dockerfile
            args:
                WWWGROUP: "${WWWGROUP}"
        image: sail-8.1/app
        extra_hosts:
            - "host.docker.internal:host-gateway"
        ports:
            - "${APP_PORT:-80}:80"
        environment:
            WWWUSER: "${WWWUSER}"
            LARAVEL_SAIL: 1
            XDEBUG_MODE: "${SAIL_XDEBUG_MODE:-off}"
            XDEBUG_CONFIG: "${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}"
        volumes:
            - ".:/var/www/html"
        networks:
            - sail
        depends_on:
            - mysql
            - redis
            - meilisearch
    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"
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
    redis:
        image: "redis:alpine"
        ports:
            - "${FORWARD_REDIS_PORT:-6379}:6379"
        volumes:
            - "sail-redis:/data"
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
    meilisearch:
        image: "getmeili/meilisearch:latest"
        ports:
            - "${FORWARD_MEILISEARCH_PORT:-7700}:7700"
        volumes:
            - "sail-meilisearch:/data.ms"
        networks:
            - sail
        healthcheck:
            test:
                [
                    "CMD",
                    "wget",
                    "--no-verbose",
                    "--spider",
                    "http://localhost:7700/health",
                ]
            retries: 3
            timeout: 5s
    mailhog:
        image: "mailhog/mailhog:latest"
        ports:
            - "${FORWARD_MAILHOG_PORT:-1025}:1025"
            - "${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025"
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
    sail-mysql:
        driver: local
    sail-redis:
        driver: local
    sail-meilisearch:
        driver: local

I also tried to use host.docker.internal/openapi or laravel.test/openapi neither works. Can you advise me on how to set up a docker network correctly so that I can call my application from my application?

When i call in sail shell curl "localhost/openapi" or curl "laravel.test/openapi" it works

Thank you guys

0 likes
10 replies
Sinnbeck's avatar

Is swagger running inside the laravel.test container as well ? Did you try just localhost/openapi

dommer's avatar

@Sinnbeck Yes, it runs in the same container. When i use localhost/openapi i get

file_get_contents(localhost/openapi): Failed to open stream: HTTP request failed!
dommer's avatar

@Sinnbeck Both return the same result Failed to open stream: HTTP request failed!

dommer's avatar

@Sinnbeck All these requests failed.

file_get_contents(localhost:80/openapi): Failed to open stream: HTTP request failed!
file_get_contents(localhost/openapi): Failed to open stream: HTTP request failed!
file_get_contents(127.0.0.1/openapi): Failed to open stream: HTTP request failed!
file_get_contents(0.0.0.0/openapi): Failed to open stream: HTTP request failed!
file_get_contents(laravel.test:80/openapi): Failed to open stream: HTTP request failed!
file_get_contents(laravel.test/openapi): Failed to open stream: HTTP request failed!

btw i use http:// before localhost. But I'm a new user and I can't use links in post.

In the sail up console i noticed

api-laravel.test-1  | [Thu Feb 17 12:46:42 2022] 172.26.0.1:55688 Closed without sending a request; it was probably just an unused speculative preconnection
api-laravel.test-1  | [Thu Feb 17 12:46:42 2022] 172.26.0.1:55688 Closing

Can it help in any way?

Sinnbeck's avatar

@dommer I dont use sail myself (I prefer lando), so I am not quite sure why its failing like that. Can you get any urls on localhost at all? I just added a test route without auth to test with file_get_contents('http://localhost/faketestroute');

It might be a problem with php artisan serve not allowing it to connect with itself.

Edit: Ok I tested by running php artisan serve locally and connecting to itself.. It fails.

Sinnbeck's avatar

Maybe consider giving Lando a shot. It seems to work there. I will be releasing a small guide tonight or tomorrow if needed

dommer's avatar

I installed laravel octane with swoole and there it works

Please or to participate in this conversation.