Issue
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.
Solution
I ended up breaking down the command. I created a variable for the sed
command by using string concatenation and then I echo
ed 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
Answered By - Paolo Answer Checked By - Katrina (WPSolving Volunteer)