skip to Main Content

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)

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 :

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 :

##
#  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 :

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.

I’m sure LiipImagine is the source of the problem because before installing it make up-prod worked fine !

UPDATE #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

 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 🙁

UPDATE #2

Based on the 1st part of Update #1, I’ve added this to the up section of Makefile :

$(EXEC) composer install --no-interaction

Now make up-prod is ok, but I still can’t find the root of the problem !

2

Answers


  1. Chosen as BEST ANSWER

    Well it works after I've added APP_SECRET like this

     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 ! I really don't understand why I have to add APP_SECRET before composer !


  2. I see the error:

    Environment variable not found: "APP_SECRET".

    First of all, check your .env and .env.loc files include this variable. I guess, that in DEV mode APP_SECRET is set up randomly, but it must be specified explicitly in the PROD.

    Secondly, pay attention that DEV and PROD install dependencies slightly different:

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search