使用 URL 而不是服务名称来连接具有 docker 的服务

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

我有以下 docker-compose 文件:

# 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

login 服务被 companies 服务用于 OAuth 流程 (Laravel passport/socialite)。

我遇到的问题是,由于某些环境变量要求 login 服务名称在内部 docker 网络中发送 post 请求,导致重定向中断,但是浏览器重定向需要完整 url (https://login.test)。

有问题的 .env 变量是:

LARAVEL_PASSPORT_HOST=login

有没有办法在内部 docker 网络中使用完整的 url https://login.test

查看使用方法network aliases

有关您的上下文的快速示例:

    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

现在 sail 网络中的所有机器都可以使用服务名称或别名 login.test 访问 login 服务。想加多少就加多少。