kaiser
kaiser

Reputation: 22333

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.

Upvotes: 7

Views: 3446

Answers (2)

Anton Bessonov
Anton Bessonov

Reputation: 9803

Run in the same issue. My workaround is:

Use login shell to read .profile:

# need docker >= 1.12
SHELL ["bash", "--login", "-c"]

Create new environment variable with substitution:

RUN echo "export PROXY_HOST_IP=${PROXY_HOST#"http://"}" >> ${HOME}/.profile

Upvotes: 0

kaiser
kaiser

Reputation: 22333

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.

Upvotes: 3

Related Questions