1

I use dockerfiles to create a container. I know docker, but my bash understanding is very very limited. I know this question is probably a duplicate, but I don't understand what to search for.

Issue The step RUN export A_NUM=$(echo ${A_VERSION} | head -c 3) does not work. Even though in bash it perfectly works. I tried:

  1. setting ENV A_VERSION=$A_VERSION
  2. replicating my issue in bash (by going into the built container)

Any export statement in the dockerfile seems to be futile - can anybody explain this?

Setting

I have app_A and app_B. When I configure app_B I need to take the chosen version of app_A into account. A simplified dockerfile looks like this:

FROM openjdk:8-jdk-slim

ARG A_VERSION=3.1.3
# RUN install app_A with A_VERSION 
# RUN install app_B
 
WORKDIR /HOME/A_USER
USER A_USER

# RUN set some environment variables for app_A


RUN export A_NUM=$(echo ${A_VERSION} | head -c 3) 
RUN echo $A_NUM

RUN if awk 'BEGIN {exit !('$A_NUM' >= '3.2')}'; then export path_A="/opt/Name_A"; else export path_A="/usr/local/Name_A"; fi 

# RUN Use B_VERSION to set some environment variables for app_B
5th
  • 2,097
  • 3
  • 22
  • 41
  • I now wrote a seperate bash script which creates the container. The `if awk`-line is executed from and passes its argument to an `ARG` command in the dockerfile – 5th Jun 01 '21 at 17:12

1 Answers1

5

Each RUN is a separate bash process, with its own environment variables. You must run all your bash commands in a single RUN:

RUN export A_NUM=$(echo ${A_VERSION} | head -c 3) \
    && echo $A_NUM \
    && if awk 'BEGIN {exit !('$A_NUM' >= '3.2')}'; then export path_A="/opt/Name_A"; else export path_A="/usr/local/Name_A"; fi 

Moreover, you will save disk space because each RUN creates an image.

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • Thanks that helps eliminating one error source. Didn't know about the separate bash processes. I think I made more than one error in this dockerfile. One of them being [not using `ENV` to update my path variable](https://stackoverflow.com/questions/27093612/in-a-dockerfile-how-to-update-path-environment-variable) – 5th Jun 01 '21 at 16:01