首页 > 解决方案 > Symfony 4 应用程序与 Docker Compose 一起工作,但与 Docker Swarm 中断(没有登录,分析器损坏)

问题描述

我在本地使用Docker Compose :

它在本地运行良好,但是当部署到开发Docker Swarm集群时,我无法登录 Symfony 应用程序。Swarm 堆栈与本地堆栈相同,除了安装在自己的服务器(不是 Docker 容器)上的PostgreSQL 。

使用profiler,我几乎总是得到以下错误:

未找到令牌 在数据库中未找到
令牌“2df1bb”。

当我显示var/log/dev.log文件的内容时,我会收到有关我的登录尝试的这些行:

[2019-07-22 10:11:14] request.INFO: Matched route "app_login". {"route":"app_login","route_parameters":{"_route":"app_login","_controller":"App\\Controller\\SecurityController::login"},"request_uri":"http://dev.ip/public/login","method":"GET"} []
[2019-07-22 10:11:14] security.DEBUG: Checking for guard authentication credentials. {"firewall_key":"main","authenticators":1} []
[2019-07-22 10:11:14] security.DEBUG: Checking support on guard authenticator. {"firewall_key":"main","authenticator":"App\\Security\\LoginFormAuthenticator"} []
[2019-07-22 10:11:14] security.DEBUG: Guard authenticator does not support the request. {"firewall_key":"main","authenticator":"App\\Security\\LoginFormAuthenticator"} []
[2019-07-22 10:11:14] security.INFO: Populated the TokenStorage with an anonymous Token. [] []

我可能在这里发现唯一有用的是Guard authenticator does not support the request.消息,但我不知道从那里搜索什么。


更新:

这是我的docker-compose.dev.yml (删除了 redis 容器并更改了应用程序环境变量)

version: "3.7"

networks:
    web:
        driver: overlay

services:
    # Symfony + Nginx
    app:
        image: "registry.gitlab.com/my-image"
        deploy:
            replicas: 2
            restart_policy:
                condition: on-failure
        networks:
            - web
        ports:
          - 80:80
        environment:
            APP_ENV: dev
            DATABASE_URL: pgsql://user:pass@0.0.0.0/my-db
            MAILER_URL: gmail://user@gmail.com:pass@localhost

这是Dockerfile.dev用于在开发服务器上构建应用程序映像的方法:

# Base image
FROM php:7.3-fpm-alpine

# Source code into:
WORKDIR /var/www/html

# Import Symfony + Composer
COPY --chown=www-data:www-data ./symfony .
COPY --from=composer /usr/bin/composer /usr/bin/composer

# Alpine Linux packages + PHP extensions
RUN apk update && apk add \
        supervisor \
        nginx \
        bash \
        postgresql-dev \
        wget \
        libzip-dev zip \
        yarn \
        npm \
    && apk --no-cache add pcre-dev ${PHPIZE_DEPS} \
    && pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo_pgsql \
    && docker-php-ext-configure zip --with-libzip \
    && docker-php-ext-install zip \
    && composer install \
        --prefer-dist \
        --no-interaction \
        --no-progress \
    && yarn install \
    && npm rebuild node-sass \
    && yarn encore dev \
    && mkdir -p /run/nginx

# Nginx conf + Supervisor entrypoint
COPY ./dev.conf /etc/nginx/conf.d/default.conf
COPY ./.htpasswd /etc/nginx/.htpasswd
COPY ./supervisord.conf /etc/supervisord.conf

EXPOSE 80 443

ENTRYPOINT /usr/bin/supervisord -c /etc/supervisord.conf

更新 2:
pull编辑了我的Docker映像并仅使用docker-compose.dev.yml(没有docker-compose.local.yml我在本地使用的)运行应用程序。我已经可以登录了,一切正常。

所以......它适用于本地Docker Compose ,但不适用于远程服务器上的Docker Swarm 。


更新 3:
我让开发服务器离开Swarm 集群并使用Docker Compose启动服务。有用。

问题是关于从ComposeSwarm。我创建了一个问题:docker/swarm #2956

标签: symfonydockerauthenticationsymfony4docker-swarm

解决方案


也许这不是您的具体情况,但它可以帮助一些在使用 Docker Swarm 时遇到问题的用户,这些问题在 Docker Compose 中不存在。

我已经为这个问题奋斗了一个多星期。我发现 Docker Compose 的默认网络使用桥接驱动程序,而 Docker Swarm 使用Overlay

后来,我在Postgres Docker 映像存储库的警告部分中读到,覆盖网络中的 IPVS 连接超时存在问题,它参考了此博客以获取解决方案。

我尝试使用第一个选项并将endpoint_mode设置更改为dnsrr我的docker-compose.yml文件中:

db:
    image: postgres:12
    # Others settings ...
    deploy:
        endpoint_mode: dnsrr

请记住,有一些警告(在博客中提到)需要考虑。但是,您可以尝试其他选项。

同样在这个问题中,您可能会发现一些有用的东西,因为他们面临同样的问题。


推荐阅读