Sed variable substitution not working in a dockerfile - docker

I have set up as entrypoint for my docker image.
CMD ["/bin/bash","/usr/local/sbin/wrapper.sh"]
which contains
set -m
sed -i "s/DBPASSWORD/$DBPASSWORD/g" db_config.php
/usr/sbin/php-fpm --nodaemonize &
/usr/sbin/httpd -D FOREGROUND
and then when I debug i see that
[root#edoc2-7bdc69555-ftbzt ctdocs]# cat db_config.php
<?php
$dbhost = "10.100.16.11";
$dbuser = "demo-docs";
$dbpass = "DBPASSWORD";
$db = "demodocsdb";
?>

Add RUN before the sed command as following:
RUN sed -i "s/DBPASSWORD/$DBPASSWORD/g" db_config.php

what I did was that I set up: set -x before the sed and it worked, thanks all for your help.

Related

Echo dynamic sed to file inside Dockerfile

I am working on a Dockerfile, inside of which I want to dynamically create a sed expression based on the input argument variable, and write this expression to a file.
Here's part of the Dockerfile:
FROM ubuntu
ARG VERSION
RUN echo $VERSION > /usr/local/testfile
RUN echo '#!/bin/sh \n\
sed -i "s/\"version\"/\${VERSION}/g" file' > /usr/local/foo.sh
the image builds fine.
When I start a container from that image, and inspect the files:
# cat /usr/local/testfile
0.0.1
# cat /usr/local/foo.sh
#!/bin/sh
sed -i "s/\"version\"/\${VERSION}/g" file
I notice that the $VERSION was not replaced correctly in the sed command. What am I missing here? I've tried a few different things (e.g. "$VERSION") but none of them worked.
I ended up breaking down the command. I created a variable for the sed command by using string concatenation and then I echoed that to the file separately:
FROM ubuntu
ARG VERSION
ENV command="sed -i s/\"version\"/""$VERSION""/g"
RUN echo '#!/bin/sh' > /usr/local/foo.sh
RUN echo $command >> usr/local/foo.sh
# cat /usr/local/foo.sh
#!/bin/sh
sed -i s/"version"/0.0.1/g

Dockerfile RUN shell-script not running during docker build

I try to build a custom image for the EMQ MQTT server. But the script update_config.sh is not executed by during docker copmose up.
Dockerfile:
FROM emqttd-docker-v2.3.5
# change configuration file
ADD update_config.sh /opt/emqttd/etc/update_config.sh
ADD ./certs/MyEMQ1.key /opt/emqttd/etc/certs/MyEMQ1.key
ADD ./certs/MyEMQ1.pem /opt/emqttd/etc/certs/MyEMQ1.pem
ADD ./certs/MyRootCA.pem /opt/emqttd/etc/certs/MyRootCA.pem
WORKDIR /opt/emqttd/etc/
#update the emqtt config file
RUN /bin/ash -c /opt/emqttd/etc/update_config.sh
update_config.sh
#!/bin/ash
cd /opt/emqttd/etc
cp ./emq.conf ./emq.conf.bak
sed -i 's|.*listener.ssl.external.keyfile.*|listener.ssl.external.keyfile = etc/certs/MyEMQ1.key|g' ./emq.conf
sed -i 's|.*listener.ssl.external.certfile.*|listener.ssl.external.certfile = etc/certs/MyEMQ1.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.cacertfile.*|listener.ssl.external.cacertfile = etc/certs/MyRootCA.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.verify.*|listener.ssl.external.verify = verify_peer|g' ./emq.conf
I use docker-compose to build the image.
The update_config.sh script is copied to the image but not executed.
What I tried so far:
Used COPY instead of ADD to copy the file
Tried the RUN /bin/ash -c /opt/emqttd/etc/update_config.sh in the following
flavors:
RUN /bin/ash -c /opt/emqttd/etc/update_config.sh
RUN /opt/emqttd/etc/update_config.sh
RUN ./update_config.sh
Tried to add RUN chmod +x /opt/emqttd/etc/update_config.sh before the line RUN /bin/ash -c /opt/emqttd/etc/update_config.sh which results in the error chmod: /opt/emqttd/etc/update_config.sh: Operation not permitted during build
Can anyone help me? Thanks.
Just add ENTRYPOINT ["/bin/bash", "update_config.sh" ] this as your last line.
And also update_config.sh file to start your application and make your container in infinite loop.
Example update_config.sh:
#!/bin/ash
cd /opt/emqttd/etc
cp ./emq.conf ./emq.conf.bak
sed -i 's|.*listener.ssl.external.keyfile.*|listener.ssl.external.keyfile = etc/certs/MyEMQ1.key|g' ./emq.conf
sed -i 's|.*listener.ssl.external.certfile.*|listener.ssl.external.certfile = etc/certs/MyEMQ1.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.cacertfile.*|listener.ssl.external.cacertfile = etc/certs/MyRootCA.pem|g' ./emq.conf
sed -i 's|.*listener.ssl.external.verify.*|listener.ssl.external.verify = verify_peer|g' ./emq.conf
sh start_your_app.sh
touch 1.txt;tail -f 1.txt #This will make your container in running infinite so that even after all the steps of this script has been executed your container will continue running. until you kill tail -f 1.txt command.
Hope this will help.
Thank you!
ash - is one of the smallest shells. This command interpreter has 24 built-in commands and 10 different command-line options.
ash hasn't all commands which you need. You should use /bin/bash

How to Search for a number and replace it with other number

I have a file server.xml with below line-
Server port="8007" shutdown="SHUTDOWN"
I want to search for 8007 and replace it with other number eg.: 8010.
How can I do it?
try this :
sed 's/8007/8010/g' yourfile
To make replacements you can use sed. I suppose that the new "port" is stored in a variable. As a result both solutions bellow works ok with GNU Sed:
$ a=$'Server port="8007" shutdown="SHUTDOWN"';echo "$a"
Server port="8007" shutdown="SHUTDOWN"
$ newport="8010"
$ sed -r "s/([0-9]+)/$newport/g" <<<"$a"
Server port="8010" shutdown="SHUTDOWN"
# Alternative:
$ sed -r 's/(port=")(.*)(" .*)/\1'"$newport"'\3/g' <<<"$a"
sed -r (or sed -E) enables extended regex groups.
If you need to apply replacements in-place (on the file) you need to use also -i switch in sed (sed -r -i.bak "....")

Xcode Pre-Action Scripts not working?

Problem
I've attempted to add pre-action shell scripts that would switch on/off certain definitions in my .pch file depending on what I was building for.
However, when running a build, nothing happens. I'm not a fluent shell scripter, so the solution may be my incorrect syntax, but Xcode won't tell me anything.
Details
Here's some code:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i 's/source/working/' $prefix
sed -i 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD' $prefix
sed -i 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED' $prefix
sed -i 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD' $prefix
I added the first line to test if it would even remove a basic word I know is in the .pch file. It didn't. This leads me to believe that my path is invalid.
I've tried several different variations of the .pch file's path and have failed with all of them, though they all could have been wrong.
Thank you for your help
Your sed lines seem not to be correct. Try:
prefix=${PROJECT_DIR}/${GCC_PREFIX_HEADER}
sed -i -e 's/source/working/' $prefix
sed -i -e 's/\/\/#define\ HOCKEYAPP_BUILD/#define\ HOCKEYAPP_BUILD/' $prefix
sed -i -e 's/\/\/#define\ FLURRY_ENABLED/#define\ FLURRY_ENABLED/' $prefix
sed -i -e 's/\/\/#define\ PRODUCTION_BUILD/#define\ PRODUCTION_BUILD/' $prefix

sed returning empty line on second pass

I have a configuraton file that I am editing and on the first pass the is correctly changed but on the next two lines sed returns the lines blank.
The lines to be edited are
word.word.word.database=dbase
word.word.word.username=someone
word.word.word.password=someone
The sed commands I am using are
cat config.file | \sed -e "s/database/$dbname/" > config.file.1
cat config.file.1 | \sed -e "s/username/$dbuser/" > config.file.2
cat config.file.2 | \sed -e "s/database/$password/" > config.file.3
cp config.file.3 config.file
The end result is
word.word.word.database=dbname
word.word.word.username=
word.word.word.password=
Can't figure out what is going wrong with this. Any help would be great.
Thanks!
$dbname , $dbuser and $password are begin treated like shell variables, hence the leading dollar signs. If you're trying to incorporate shell vars, try:
sed -i -e "s/database/$dbname/" -e "s/username/$dbuser/" -e "s/password/$password/" config.file
Notice that I've added the -i flag to the above command. It enables in-place editing using GNU sed. Other types of sed require an extension to be set, like: -i.bak and this creates a back up file; in your case this would be: config.bak.
If you're just looking to get the full list of results, either drop the dollarsigns or use single quotes. For example:
sed -i -e "s/database/dbname/" -e "s/username/dbuser/" -e "s/password/password/" config.file
or
sed -i -e 's/database/$dbname/' -e 's/username/$dbuser/' -e 's/password/$password/' config.file
EDIT1:
If I've completely mis-understood your question, try this:
sed -i -e "s/\(.*database=\).*/\1dbname/" -e "s/\(.*username=\).*/\1dbuser/" -e "s/\(.*password=\).*/\1password/" config.file
Results:
word.word.word.database=dbname
word.word.word.username=dbuser
word.word.word.password=password
EDIT2:
If you have shell vars labelled $dbname , $dbuser and $password:
sed -i -e "s/\(.*database=\).*/\1$dbname/" -e "s/\(.*username=\).*/\1$dbuser/" -e "s/\(.*password=\).*/\1$password/" config.file

Resources