Reputation: 323
I have something like this in my Dockerfile
docker-compose.yml
version: '3.7'
services:
backuppc-app:
image: tiredofit/backuppc
container_name: backuppc-app
build:
context: .
dockerfile: ./Dockerfile
volumes:
- /mnt/TOSHIBA-BACKUP/backuppc/data:/var/lib/backuppc
- /mnt/TOSHIBA-BACKUP/backuppc/conf/etc/:/etc/backuppc
- /mnt/TOSHIBA-BACKUP/backuppc/conf/home/:/home/backuppc
ports:
- "8081:80"
- "8082:10050"
environment:
- BACKUPPC_UUID=1000
- BACKUPPC_GUID=1000
- SMTP_HOSTNAME=${SMTP_HOSTNAME}
- SMTP_PORT=${SMTP_PORT}
- SMTP_USERNAME=${SMTP_USERNAME}
- SMTP_PASSWORD=${SMTP_PASSWORD}
restart: always
Dockfile
FROM tiredofit/backuppc
RUN echo $'defaults \n\
auth on \n\
tls on \n\
account gmail \n\
host $SMTP_HOSTNAME \n\
port $SMTP_PORT \n\
user $SMTP_USERNAME \n\
from $SMTP_USERNAME \n\
password $SMTP_PASSWORD \n\
account default : gmail \n' > /root/.msmtprc
Checking into /root/.msmtprc variables not evaluated.. tryed many tests.. using ${VAR_NAME} doesn't work. Using double quotes variables are evaluated but carriage return not.
Also when i try to write that string into /home/backuppc/.msmtprc file is not overwritten
-rw-r--r-- 1 root backuppc 200 Jan 17 11:19 .msmtprc
Upvotes: 2
Views: 6194
Reputation: 982
To counter the fact that the shell is ignoring the \n when using double quotes and that using single quotes performs no variable expansion, you can create a variable to hold the newline character and expand it in a string surrounded by double quotes.
The following should work:
RUN nl=$'\n' && echo "\
foobar $nl\
$YOUR_VAR $nl\
hello world" > file.txt
After building the image, you should have the contents of file with the newlines as you wish.
Adding on @blami's comment, you should pass these variables as args to your build
section in your docker-compose and declare them in your Dockerfile before using them with ARG var_name
, like the link he posted recommends.
So at the end, you should have a Dockerfile similar to:
FROM tiredofit/backuppc
ARG YOUR_VAR
RUN nl=$'\n' && echo "\
first line $nl\
$YOUR_VAR $nl\
third line " > file.txt
You can try to run now
> docker build . -t test --build-arg YOUR_VAR="second line"
> docker run test cat file.txt
And get something like
first line
second line
third line
As for the docker-compose, I haven't really tried this, but I think it should look something like this:
version: '3.7'
services:
backuppc-app:
container_name: backuppc-app
image: tiredofit/backuppc
build:
context: .
dockerfile: ./Dockerfile
args:
- YOUR_VAR=${YOUR_VAR}
...
Assuming you've set the YOUR_VAR environment variable.
Upvotes: 2