简体   繁体   English

无法在产品环境中使用 Docker 在 Symfony 4.4 上安装 LiipImagineBundle 2.3

[英]Can't install LiipImagineBundle 2.3 on Symfony 4.4 with Docker in prod environment

In my Dockerized Symfony 4.4 project I can switch between dev and prod environments with two commands: make up-dev and make up-prod (using Makefile)在我的 Dockerized Symfony 4.4 项目中,我可以使用两个命令在 dev 和 prod 环境之间切换: make up-devmake up-prod (使用 Makefile)

In dev mode, inside the container, I can install LiipImagineBundle with no problem, then when I switch to prod environment the make up-prod exits with this error message:在开发模式下,在容器内,我可以毫无问题地安装 LiipImagineBundle,然后当我切换到 prod 环境时, make up-prod退出并显示以下错误消息:

Generated optimized autoload files containing 7171 classes
+ APP_ENV=prod composer run-script --no-dev post-install-cmd

Run composer recipes at any time to see the status of your Symfony recipes.

Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 1
!!  
!!   // Clearing the cache for the prod environment with debug                      
!!   // false                                                                       
!!  
!!  
!!  In EnvVarProcessor.php line 171:
!!                                                   
!!    Environment variable not found: "APP_SECRET".  
!!                                                   
!!  
!!  cache:clear [--no-warmup] [--no-optional-warmers] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command>
!!  
!!  
Script @auto-scripts was called via post-install-cmd
ERROR: Service 'app' failed to build: The command '/bin/sh -c set -eux;     mkdir -p var/cache var/log;     chmod +x bin/console;     composer dump-autoload --no-dev --no-scripts --optimize;     APP_ENV=prod composer run-script --no-dev post-install-cmd' returned a non-zero code: 1
make: *** [Makefile:26: up] Error 1

This is my Dockerfile:这是我的 Dockerfile:

##
#  Base
##

FROM debian:9-slim as base

ENV TERM="xterm" \
    DEBIAN_FRONTEND="noninteractive" \
    TIMEZONE="Europe/Paris" \
    COMPOSER_ALLOW_SUPERUSER=1 \
    NODE_VERSION=8.12.0 \
    PHP_VERSION=7.2

# System depdendencies
RUN apt-get update --quiet && \
    apt-get install --quiet --no-install-recommends --yes \
        apt-transport-https \
        ca-certificates \
        lsb-release \
        wget && \
    wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg && \
    echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" >> /etc/apt/sources.list.d/php.list && \
    apt-get update --quiet && \
    apt-get install --quiet --no-install-recommends --yes \
        curl \
        git \
        nginx \
        php${PHP_VERSION} \
        php${PHP_VERSION}-apcu \
        php${PHP_VERSION}-curl \
        php${PHP_VERSION}-dom \
        php${PHP_VERSION}-fpm \
        php${PHP_VERSION}-gd \
        php${PHP_VERSION}-iconv \
        php${PHP_VERSION}-intl \
        php${PHP_VERSION}-mbstring \
        php${PHP_VERSION}-mysql \
        php${PHP_VERSION}-opcache \
        php${PHP_VERSION}-pdo \
        php${PHP_VERSION}-uuid \
        php${PHP_VERSION}-xml \
        php${PHP_VERSION}-simplexml \
        php${PHP_VERSION}-zip \
        supervisor \
        p7zip-full \
        tzdata \
        unzip \
        libxml2-dev \
        php${PHP_VERSION}-soap \
        vim && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
    mkdir /run/php && \
    cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \
    echo "${TIMEZONE}" > /etc/timezone

# Composer

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
    composer global require "hirak/prestissimo:^0.3" --prefer-dist --no-progress --no-suggest --classmap-authoritative && \
    composer clear-cache

# Yarn

RUN curl -L -o /tmp/nodejs.tar.gz https://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz && \
    tar xfvz /tmp/nodejs.tar.gz -C /usr/local --strip-components=1 && \
    rm -f /tmp/nodejs.tar.gz && \
    npm install yarn -g

COPY .docker/php.ini /etc/php/${PHP_VERSION}/cli/conf.d/50-setting.ini
COPY .docker/php.ini /etc/php/${PHP_VERSION}/fpm/conf.d/50-setting.ini
COPY .docker/pool.conf /etc/php/${PHP_VERSION}/fpm/pool.d/www.conf
COPY .docker/nginx.conf /etc/nginx/nginx.conf
COPY .docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

EXPOSE 80

CMD ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

WORKDIR /app

##
#  Production
##

FROM base as prod

# PHP dependencies
COPY composer.json composer.lock symfony.lock ./
RUN set -eux; \
    composer install --no-dev --no-autoloader --no-scripts --no-progress --no-suggest

COPY bin bin/
COPY config config/
COPY public public/
COPY src src/
COPY templates templates/
COPY translations translations/
COPY resources resources/
RUN set -eux; \
    mkdir -p var/cache var/log; \
    chmod +x bin/console; \
    composer dump-autoload --no-dev --no-scripts --optimize; \
    APP_ENV=prod composer run-script --no-dev post-install-cmd

# Assets dependencies
COPY assets/ ./assets
COPY package.json yarn.lock webpack.config.js ./
RUN set -eux; \
    mkdir -p public/build; \
    yarn install --no-progress;  \
    yarn encore production

RUN usermod -u 1000 www-data

And this is the Makefile:这是 Makefile:

EXEC := docker-compose exec app
PHP := $(EXEC) php -d memory_limit=-1
CONSOLE := $(PHP) bin/console

##
# Service
##

.PHONY: up-dev up-prod up down

up-dev: export ENV := dev
up-dev: up vendor public/build
    $(CONSOLE) cache:clear
ifeq ($(shell uname), Linux)
    $(EXEC) chown -R $(shell id -u):$(shell id -g) .
endif

up-prod: export ENV := prod
up-prod: up

up-beta: export ENV := beta
up-beta: up

up:
    docker-compose -f docker-compose.yml -f docker-compose.$(ENV).yml up -d --build --remove-orphans

I think FROM base as prod is the source of the problem but can't figure out how to fix it and don't want to modify this section since I took the project on the way.我认为FROM base as prod是问题的根源,但不知道如何解决它,也不想修改这一部分,因为我在进行项目的过程中。

I'm sure LiipImagine is the source of the problem because before installing it make up-prod worked fine !我确信 LiipImagine 是问题的根源,因为在安装它之前, make up-prod工作正常!

UPDATE #1更新#1

I've tried two updates:我尝试了两个更新:

First one: I've deleted this:第一个:我已经删除了这个:

 RUN set -eux; \
     mkdir -p var/cache var/log; \
     chmod +x bin/console; \
     composer dump-autoload --no-dev --no-scripts --optimize; \
     APP_ENV=prod composer run-script --no-dev post-install-cmd

Second one: I've added APP_SECRET like this第二个:我已经像这样添加了 APP_SECRET

 RUN set -eux; \
...
     APP_ENV=prod APP_SECRET=sameValueAsDotEnv composer run-script --no-dev post-install-cmd

In both cases, the make up-prod works fine with no errors, but I have to run composer install inside the container otherwise depencies are missing... I really don't get it:(在这两种情况下, make up-prod都可以正常工作,没有错误,但我必须在容器内运行composer install否则会丢失依赖项......我真的不明白:(

UPDATE #2更新#2

Based on the 1st part of Update #1, I've added this to the up section of Makefile:基于更新 #1 的第一部分,我已将此添加到 Makefile 的上部分:

$(EXEC) composer install --no-interaction

Now make up-prod is ok, but I still can't find the root of the problem !现在make up-prod没问题,但我还是找不到问题的根源!

I see the error:我看到了错误:

Environment variable not found: "APP_SECRET".未找到环境变量:“APP_SECRET”。

First of all, check your.env and.env.loc files include this variable.首先,检查你的.env 和.env.loc 文件是否包含这个变量。 I guess, that in DEV mode APP_SECRET is set up randomly, but it must be specified explicitly in the PROD.我猜,在 DEV 模式下,APP_SECRET 是随机设置的,但必须在 PROD 中明确指定。

Secondly, pay attention that DEV and PROD install dependencies slightly different:其次,注意 DEV 和 PROD 安装依赖略有不同:

if [ "$APP_ENV" = 'prod' ]; then
    composer install --prefer-dist --no-dev --no-progress --no-suggest --optimize-autoloader --classmap-authoritative --no-interaction
else
    composer install --prefer-dist --no-progress --no-suggest --no-interaction
fi

Hope, it will give you the right direction.希望,它会给你正确的方向。

Well it works after I've added APP_SECRET like this好吧,在我像这样添加 APP_SECRET 之后它就可以工作了

 RUN set -eux; \
...
     APP_ENV=prod APP_SECRET=123 composer run-script --no-dev post-install-cmd

Inside the container the APP_SECRET env variable contains the value defined in .env file not 123 !在容器内, APP_SECRET 环境变量包含.env文件中定义的值,而不是123 I really don't understand why I have to add APP_SECRET before composer !我真的不明白为什么我必须在 composer 之前添加 APP_SECRET !

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM