Search code examples
laraveldockerdocker-composelaravel-passportlaravel-sail

Using a URL instead of service name to connect services with docker compose


I have the following docker-compose file:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    login:
        build:
            context: ${LOGIN}/vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '${LOGIN}:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    companies:
        build:
            context: ${COMPANIES}/vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '${COMPANIES}:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
          test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s
    webserver:
        image: 'nginx:alpine'
        volumes:
            - '${NGINX_CONFIG}:/etc/nginx/conf.d'
            - '${NGINX_CONFIG}/certs/:/etc/nginx/ssl'
        ports:
            - 80:80
            - 443:443
        networks:
            - sail
        depends_on:
            - login
            - companies
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local

The login service is used for an OAuth flow (Laravel passport/socialite) by the companies service.

The issue I've come across is that redirects break due to certain env variables requiring the login service name to send post requests in the internal docker network, but a full url (https://login.test) is required for browser redirects.

The .env variable in question is:

LARAVEL_PASSPORT_HOST=login

Is there a way to use the full url https://login.test in the internal docker network?


Solution

  • See how to use network aliases

    For a quick example in your context:

        login:
            build:
                context: ${LOGIN}/vendor/laravel/sail/runtimes/8.0
                dockerfile: Dockerfile
                args:
                    WWWGROUP: '${WWWGROUP}'
            image: sail-8.0/app
            environment:
                WWWUSER: '${WWWUSER}'
                LARAVEL_SAIL: 1
                XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
                XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            volumes:
                - '${LOGIN}:/var/www/html'
            networks:
                sail:
                  aliases:
                    - login.test
            depends_on:
                - mysql
    

    Now all machines in the sail network will be able to reach the login service by using either its service name or the alias login.test. Add as many as you wish.