skip to Main Content

I need to fill .env with production or test environment multiline data
I try to fill it in Dockerfile as follow below

CMD echo -e ${ENV_DATABASE_URL} > /var/www/.env
CMD echo -e ${ENV_LOGIN_USER_ID} >> /var/www/.env

command cat .env printout will be only one row last ${ENV_LOGIN_USER_ID} data.
${ENV_DATABASE_URL} data will be lost

Why >> append to .env file not work in Dockerfile ?

P.s. I solve this like, maybe are more elegant solution ?

ARG LOGIN_USER_ID=2
ENV ENV_LOGIN_USER_ID="LOGIN_USER_ID=${LOGIN_USER_ID}n"
ENV ENV_DATABASE_URL='DATABASE_URL="postgresql://postgres:db_password@bot_postrgres:5432/postgres_eis_bot'${LOGIN_USER_ID}'?serverVersion=15&charset=utf8"n'
CMD echo -e ${ENV_DATABASE_URL}${ENV_EIS_LOGIN_USER_ID} > /var/www/.env

2

Answers


  1. CMD instruction creates a new layer in the Docker image and runs in a separate shell. Thus, when you use multiple CMD instructions like this, they don’t operate on the same file.

    There’s a more suitable approach for what you’re trying to achieve, by combining the echo commands into a single RUN instruction and using a shell command.

    ENV ENV_DATABASE_URL='DATABASE_URL="postgresql://postgres:db_password@bot_postrgres:5432/postgres_eis_bot'${LOGIN_USER_ID}'?serverVersion=15&charset=utf8"'
    ENV ENV_LOGIN_USER_ID="LOGIN_USER_ID=${LOGIN_USER_ID}"
    
    RUN echo -e "${ENV_DATABASE_URL}n${ENV_LOGIN_USER_ID}" > /var/www/.env
    

    Or you can use the printf command, which allows you to easily format the string with line breaks:

    RUN printf "%sn%sn" "${ENV_DATABASE_URL}" "${ENV_LOGIN_USER_ID}" > /var/www/.env
    
    Login or Signup to reply.
  2. To add to @humza’S answer.

    I use this format as it is way easier to read and extend:

    RUN echo "memory_limit = 128M" > /usr/local/etc/php/conf.d/custom.ini 
     && echo "max_execution_time = 60" >> /usr/local/etc/php/conf.d/custom.ini 
     && echo "error_reporting=E_ALL" >> /usr/local/etc/php/conf.d/custom.ini 
     && echo "log_errors=On" >> /usr/local/etc/php/conf.d/custom.ini 
     && echo "error_log = /proc/self/fd/2" >> /usr/local/etc/php/conf.d/custom.ini
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search