Hesham Osman
Hesham Osman

Reputation: 111

Docker ARGs don't get substituted in docker file

I'm trying to use ARG inside docker file but it doesn't work, I'm a newbie

Here is my docker file

 ARG appver=0.0.1
 ENV ausername=any
 COPY ./target/userservice-${appver}-SNAPSHOT.jar .
 CMD  ["java", "-jar", "userservice-${appver}-SNAPSHOT.jar"]

so, when I build the image I expected the jar name to be

 userservice-0.0.1-SNAPSHOT.jar

but what I get is

Step 9/10 : COPY ./target/userservice-${appver}-SNAPSHOT.jar .
---> 3f2772c50130
Step 10/10 : CMD  ["java", "-jar", "userservice-${appver}-SNAPSHOT.jar"]
---> Running in 47286088595a

Upvotes: 0

Views: 48

Answers (1)

BMitch
BMitch

Reputation: 265045

Docker doesn't expand build args in the CMD step. You'll want to do two things. First, copy your build arg into an environment variable. And second, switch to the shell syntax to expand that environment variable.

ARG appver=0.0.1
ENV ausername=any
ENV appver=${appver}
COPY ./target/userservice-${appver}-SNAPSHOT.jar .
CMD exec java -jar userservice-${appver}-SNAPSHOT.jar

I added an exec to get rid of the shell from pid 1 as soon as it expands the command. This aids in signal handling.

Upvotes: 2

Related Questions