Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Docker ENV var?

While building a Dockerfile, I often allow to configure arguments during build time to make configuring only slightly different containers easier to build. To achieve this, I use defaults for the ENV vars combined with user definable ARGs. Example Dockerfile to quickly test with:

FROM busybox
ARG FLAGS
ENV FLAGS ${FLAGS:-}
RUN echo "${FLAGS}"

This then can be used like this:

docker build --build-arg FLAGS="foo --remove-me" -t <imagename>:<tag> .

Now I find myself in the situation that I do want to actively remove a specific flag (in above example: --remove-me) from a command that I allow to run (due to a bug not fixed since more than a year). While I know how to remove the flag in other situations:

LC_ALL=C sed -e 's/ --remove-me//'

I am facing the problem that I have no idea how to pipe and remove the flag. I know that I can do it while using RUN, but then I would have to repeat above sed usage for every RUN statement, therefore not making it repeatable.

like image 519
kaiser Avatar asked Jun 21 '16 09:06

kaiser


1 Answers

After reading about shell variable replacement in this AskUbuntu answer, I gave it a try:

ENV FLAGS ${FLAGS//--fully-static/}

However, I ran into the following error:

Missing ':' in substitution: "${FLAGS:-}" | "${FLAGS//--fully-static/}"

First I found out that Docker only supports a limited bit of bash variable replacement methods. Finally I could find the shell_parser.go file in the Docker GitHub Repo where it's clear that the : must be there, which actively kills any efforts going down this route right in the first step.

like image 56
kaiser Avatar answered Oct 10 '22 03:10

kaiser