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
CMD
instruction creates a new layer in the Docker image and runs in a separate shell. Thus, when you use multipleCMD
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 singleRUN
instruction and using a shell command.Or you can use the
printf
command, which allows you to easily format the string with line breaks:To add to @humza’S answer.
I use this format as it is way easier to read and extend: