tomm
tomm

Reputation: 321

Dockerfile - get, modify and set env variable

probably really simple question, but I cannot find a solution. I have some env variable what stores some version number. At this moment it's just 11.1.1. I need to modify it to get 11.1 and save to env ABC_VER. So it's possible to do it after building my container like below:

export ABC=$(echo $ABC_VERSION | head -c 4)

I was trying to do the same in my dockerfile:

ENV ABC_VER $(echo $ABC_VERSION | head -c 4)

But after building and running echo $ABC_VER I got:

$(echo 11.1.1 | head -c 4)

instead of 11.1

Not sure, how to fix it?

Upvotes: 0

Views: 259

Answers (1)

frippe
frippe

Reputation: 1393

Docker's ENV instruction is a bit limited in this sense. As far as I know, the most advanced thing you can do is environment replacement, but that won't be of much help here.

I can think of two low-hanging options:

  1. Use RUN to evaluate the expression you're using and appending an export instruction with the result to the appropriate shell startup script (e.g., /etc/profile (or preferably a separate file under /etc/profile.d))
  2. Use ARG MYARG in the Dockerfile and provide the correctly formatted version as a --build-arg and use ENV ABC_VER=${MYARG}.

Upvotes: 1

Related Questions