1

I would like to pass argument (from the docker command) to the shell script inside the Dockerfile.

This is the docker command line.

docker build --file=DockerfileTest --build-arg docker_image=PX-release-migration --tag=test-image:latest --rm=true . 

This is a script that is called inside the Dockerfile.

#!/bin/sh -e image_name=$1 echo "docker image is $image_name" if [[ ($image_name == '') || ($image_name == *"-dev-"*) ]]; then echo "This is development" cp src/main/resources/application-dev.properties src/main/resources/application.properties elif [[ $image_name == *"-preprod-"* ]]; then echo "This is preprod" cp src/main/resources/application-stg.properties src/main/resources/application.properties elif [[ $image_name == *"-release-"* ]]; then echo "This is production" cp src/main/resources/application-prod.properties src/main/resources/application.properties fi 

When I execute separately the script, it works, but it doe

This is docker file.

ARG spring_env=local ARG docker_image=-local- FROM maven:3.6.1-jdk-8 COPY . /apps/demo WORKDIR /apps/demo RUN chmod +x /apps/demo/initialize_env.sh RUN ./initialize_env.sh $docker_image RUN echo "spring_env is ${spring_env}" 

So basically, i would like to use a different spring application properties file during the build depending on the docker_image name. If a docker image name contains 'release', i would like to package application-prod.properties during the build.

This is the error message that I am getting.

Step 1/8 : ARG spring_env=local Step 2/8 : ARG docker_image=-local- Step 3/8 : FROM maven:3.6.1-jdk-8 ---> 4c81be38db66 Step 4/8 : COPY . /apps/demo ---> 41439197c465 Step 5/8 : WORKDIR /apps/demo ---> Running in 56bd408c2eb1 Removing intermediate container 56bd408c2eb1 ---> 4c4025bf5f64 Step 6/8 : RUN chmod +x /apps/demo/initialize_env.sh ---> Running in 18dc3a5c1a54 Removing intermediate container 18dc3a5c1a54 ---> 60d2037a0209 Step 7/8 : RUN ./initialize_env.sh $docker_image ---> Running in 2e049b2cf630 docker image is ./initialize_env.sh: 5: ./initialize_env.sh: Syntax error: word unexpected (expecting ")") The command '/bin/sh -c ./initialize_env.sh $docker_image' returned a non-zero code: 2 

When I execute separately the script, it works, but it doesn't inside the docker container.

1 Answer 1

1

Tip: Use ShellCheck to check scripts for syntax errors.

#!/bin/sh -e 
if [[ ($image_name == '') || ($image_name == *"-dev-"*) ]]; then 

[[ is bash syntax but your script is declared to use plain sh. It works on your machine presumably because sh is really symlinked to bash, but inside the container that's not the case. maven:3.6.1-jdk-8 is based on debian:stretch which uses dash instead of bash.

Change the shebang line. You can also delete the parentheses; they're superfluous.

#!/bin/bash -e 
if [[ $image_name == '' || $image_name == *"-dev-"* ]]; then 

You could also use a case block to simplify the repetitive checks.

case "$image_name" in ''|*-dev-*) echo "This is development" cp src/main/resources/application-dev.properties src/main/resources/application.properties ;; *-preprod-*) echo "This is preprod" cp src/main/resources/application-stg.properties src/main/resources/application.properties ;; *-release-*) echo "This is production" cp src/main/resources/application-prod.properties src/main/resources/application.properties ;; esac 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the answer, but i am getting 'this is development' for printout. it seems the 'image_name' is empty. For image_name=$,1 is this a correct syntax?
No comma. image_name=$1.
I think you need braces in the dockerfile: RUN ./initialize_env.sh ${docker_image}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.