I am running a Dockerfile
, but, am encountering a problem when I assign a variable for modifying /etc/fail2ban/jail.local
, with its string content-"PRIVATEIP".
Dockerfile
generates an error and stops. Because I have no idea about assigning a variable with sed
to modify the string content in a specific file.
RUN IP=$(cat /root/ip_variable) | sed -i -r "s/PRIVATEIP/${IP}/g" /etc/fail2ban/jail.local
---> Running in 1e78ef4318ab
If there are any alternative methods to handle this please let me know.
===========================================================================
Add more explanation for an alternative method as below on 2018/July 11:
I add some command lines on Dockerfile. The purpose is same as before. I alternatively use echo to write a script as below. But, it's weird. Why does the fellow character "/" after "s" on "sed" this command get missed? (If there are any methods to add high light or mark on the character within the output of the code, I thanks to you.) If this matter is resolved, I thinks I am able to close this issues.
Step 64/73 : RUN echo "#!/bin/bash\nexport PRIVATEIP=$(cat /home/ip_variable)\nsed -i -r \"s/\bPRIVATEIP\b/\${PRIVATEIP}/g\" /etc/fail2ban/jail.local\nsed -i -r \"s/\bPRIVATEIP\b/\${PRIVATEIP}/g\" /etc/opendkim/TrustedHosts" > /home/start_script.sh
---> Running in bbff59f82475
Removing intermediate container bbff59f82475
---> 0068d9d600ff
Step 65/73 : RUN cat /home/ip_variable
---> Running in faeb4bb1a6ba
172.17.0.2/16
Removing intermediate container faeb4bb1a6ba
---> f6cc35fd01e5
Step 66/73 : RUN cat /home/start_script.sh
---> Running in 95a5ef3b7d23
#!/bin/bash
export PRIVATEIP=172.17.0.2/16
sed -i -r "sPRIVATEI/${PRIVATEIP}/g" /etc/fail2ban/jail.local
sed -i -r "sPRIVATEI/${PRIVATEIP}/g" /etc/opendkim/TrustedHosts
Removing intermediate container 95a5ef3b7d23
---> 6720e7c01efc
Step 67/73 : RUN chmod 755 /home/start_script.sh
---> Running in d8ab5e04a846
Removing intermediate container d8ab5e04a846
---> decb9e147db9
Step 68/73 : RUN sh /home/start_script.sh
---> Running in fc86000f9a2f
sed: -e expression #1, char 28: unknown option to `s'
sed: -e expression #1, char 28: unknown option to `s'
The command '/bin/sh -c sh /home/start_script.sh' returned a non-zero code: 1
Best Answer
This works for me:
With ipconf & ipaddr
Then:
However, this is exactly how I would not do it. A better way is to use a build arg:
Then:
However I'm not even sure this is the right solution. I would either:
COPY
. This gives full control on its contents.CMD
orENTRYPOINT
can be script that patches the file before calling the main command. You can also pass the configuration file as a VOLUME (this can avoid issues if CMD is run as an application user and patching the config requires root privileges).