1

Never wrote any shell script before but after extensively googling it I came up with the following code for my docker file. But don't understand why is it doesn't work.

###stage 2####################
FROM nginx:alpine

##########Calculate the environment type #########
ARG BUILD_TYPE

####echo of build build_type does gives me output of Development when passed argument is Development.
RUN if [ "$BUILD_TYPE" = "Development" ]; then BUILD_TYPE='dev'; fi
RUN if [ "$BUILD_TYPE" = "Production" ]; then BUILD_TYPE='prod'; fi
RUN echo "UI BUILD_TYPE=$BUILD_TYPE---------"
##########Calculate the environment type #########

The above echo always comes as Development.

UPDATE

Now I built a sample in a separate docker file to isolate the issue. After this I realised that the assignment is not happening though the condition matched.

Here is the new sample docker file code.

FROM nginx:alpine

ARG BUILD_TYPE
ARG ENV_TYPE

RUN if [ "$BUILD_TYPE" = "Development" ]; then ENV_TYPE='dev'; echo "matched dev"; fi
RUN if [ "$BUILD_TYPE" = "Production" ]; then ENV_TYPE="prod"; echo "matched prod"; fi
RUN echo "UI BUILD_TYPE=$BUILD_TYPE ENV_TYPE = $ENV_TYPE---------"

The output is

  1. matched dev
  2. UI BUILD_TYPE=Development ENV_TYPE = ---------

I see ENV_TYPE is empty.

coolcake
  • 2,917
  • 8
  • 42
  • 57
  • I think this problem is same with https://stackoverflow.com/a/43656644/6284644 – Jc Din Jan 21 '20 at 05:29
  • @coolcake: Please be clear about the requirements: You are saying that you want to write a POSIX shell script, but then you are tagging the question by _bash_. If you need POSIX compatibility, please replace this tag by _shell_. – user1934428 Jan 21 '20 at 07:20
  • @coolcake : Also, it would make sense to add the _docker_ tag to this question. – user1934428 Jan 21 '20 at 07:22

1 Answers1

2

Each RUN command in a Dockerfile is executed in a separate shell session, so when you set BUILD_TYPE, you are setting an environment variable for that session only, which overrides the build-argument. You are not overwriting the build-argument for the entire docker build.

You can see this by the fact that if you change your if statements to:

RUN if [ "$BUILD_TYPE" = "Development" ]; then BUILD_TYPE='dev'; fi; echo $BUILD_ENV
RUN if [ "$BUILD_TYPE" = "Production" ]; then BUILD_TYPE='prod'; fi; echo $BUILD_ENV

The env var is correctly set, and echoed at the end of the line but your final echo will still return the build argument.

If you instead put these statements in a shell script and run that instead, it works just fine:

build.sh:

####echo of build build_type does gives me output of Development when passed argument is Development.
if [ "$BUILD_TYPE" = "Development" ]; then BUILD_TYPE='dev'; fi
if [ "$BUILD_TYPE" = "Production" ]; then BUILD_TYPE='prod'; fi
echo "UI BUILD_TYPE=$BUILD_TYPE---------"
##########Calculate the environment type #########

Dockerfile:

###stage 2####################
FROM nginx:alpine

##########Calculate the environment type #########
ARG BUILD_TYPE

COPY build.sh .
RUN ./build.sh

Output:

docker build --build-arg BUILD_TYPE=Production .

Sending build context to Docker daemon  166.9kB
Step 1/4 : FROM nginx:alpine
 ---> 36189e6707f4
Step 2/4 : ARG BUILD_TYPE
 ---> Running in cab2e8749e7e
Removing intermediate container cab2e8749e7e
 ---> ea9ec7779909
Step 3/4 : COPY build.sh .
 ---> 336989bf6389
Step 4/4 : RUN ./build.sh
 ---> Running in ecd09ee58780
UI BUILD_TYPE=prod---------
Removing intermediate container ecd09ee58780
 ---> ed9ca30af483
Successfully built ed9ca30af483
SiHa
  • 7,830
  • 13
  • 34
  • 43