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:
ENV FLYWAY_ENABLED=true
RUN if $FLYWAY_ENABLED is "true"; then RUN chmod+x /flyway-setup.sh; fi
I tried changing like below, but still i am getting the error.
RUN if $FLYWAY_ENABLED is "true"; then RUN /bin/bash -c /flyway-setup.sh; 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 /flyway-setup.sh; fi
---> Running in b09c557fb3a9
/bin/sh: 1: RUN: not found
The command '/bin/sh -c if $FLYWAY_ENABLED is "true"; then RUN chmod+x /flyway-setup.sh; fi' returned a non-zero code: 127
Can someone help me here pointing me the correct syntax ?
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.
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.