Docker Community Forums

Share and learn in the Docker community.

How to execute a shell script based on a if check inside Docker


I have a requirement where i have to define a variable inside the DockerFile and if the value of the variable is “true” then execute a shell script.

I am trying to do as below:

RUN if $FLYWAY_ENABLED is "true"; then RUN chmod+x /; fi

I tried changing like below, but still i am getting the error.

RUN if $FLYWAY_ENABLED is "true"; then RUN /bin/bash -c /; fi

But its syntactically incorrect so i am getting the below error:

Step 9/55 : RUN if $FLYWAY_ENABLED is "true"; then RUN chmod+x /; fi
 ---> Running in b09c557fb3a9
/bin/sh: 1: RUN: not found
The command '/bin/sh -c if $FLYWAY_ENABLED is "true"; then RUN chmod+x /; fi' returned a non-zero code: 127

Can someone help me here pointing me the correct syntax ?

I am pretty new to this Docker world.

Thanks In Advance.



Though, it feels very wrong to have an if condition in a Dockerfile. Instead you should put whatever the if condition does in a second Dockerfile and use the image of the first Dockerfile as the base image of the second.

I am basically trying to execute a script if the flag is true. In that case how should i deal in . the docker world ?

For me it seems like we need the if statement because idea is, i only wanted to get that script executed when its true.

From the link above i didn’t understand what i will be using to fix the error i am getting.

It is not quite clear what you are doing here, do you want to build different images depending on FLYWAY_ENABLED or do you want to modify the container on startup? You probably want to move this code to an entrypoint script. Take a look at the docs for ENTRYPOINT, you find several examples there. Or take a look at the entrypoint script for MySQL.

The goal here is, while building the image, i wanted to setup MySql and flyway and apply DB migration if FLYWAY_ENABLED=true inside the docker file.

And the flyway and mysql setup related commands will go inside the shell script.