我在本地使用 Docker Compose:
- app 容器:Nginx 和 PHP-FPM 以及 Symfony 4 应用
- PostgreSQL 容器
- Redis容器
它在本地运行良好,但是当部署到开发 Docker Swarm 集群时,我无法登录到 Symfony 应用程序。 Swarm 堆栈与本地相同,除了 PostgreSQL 安装在自己的服务器上(不是 Docker 容器)。
使用探查器,我几乎总是会遇到以下错误:
Token not found
Token "2df1bb" was not found in the database.
当我显示 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
这是用于在开发服务器上构建app 镜像的 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:
我拉
了我的Docker图像并仅使用docker-compose.dev.yml
(没有docker-compose .local.yml
我会在本地使用)。我已经可以登录了,一切正常。
所以...它适用于 Docker Compose 本地,但不适用于远程服务器上的 Docker Swarm。
更新 3:
我让开发服务器离开 Swarm 集群,并使用 Docker Compose 启动服务。它有效。
问题是关于从 Compose 到 Swarm。我创建了一个问题:docker/swarm #2956
最佳答案
也许这不是您的具体情况,但它可以帮助一些在使用 Docker Swarm 时遇到问题的用户,这些问题在 Docker Compose 中不存在。
我已经为这个问题奋斗了一个多星期。我发现 Docker Compose 的默认网络使用 bridge 驱动程序,而 Docker Swarm 使用 Overlay。
后来,我阅读了 Postgres Docker image repo 中的注意事项部分覆盖网络中的 IPVS 连接超时存在问题,它指的是 this blog寻求解决方案。
我尝试使用第一个选项并更改了 endpoint_mode在我的 docker-compose.yml
文件中设置为 dnsrr
:
db:
image: postgres:12
# Others settings ...
deploy:
endpoint_mode: dnsrr
请记住,有一些注意事项(在博客中提到)需要考虑。但是,您可以尝试其他选项。
也在 this issue 中也许您会发现一些有用的东西,因为他们面临着同样的问题。
关于Symfony 4 应用程序与 Docker Compose 一起工作,但与 Docker Swarm 一起中断(无法登录,分析器中断),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57143819/