Charles Brown
Charles Brown

Reputation: 927

Build args does not pass in value to docker file

I want to build a docker image. And I run

docker build --build-arg project_file_name=account.jar -t account:1.0 .

The docker file looks like this (#1)

FROM anapsix/alpine-java:8u172b11_server-jre

ARG project_file_name

MAINTAINER jim
COPY src/${project_file_name} /home/${project_file_name}
CMD java -jar /home/${project_file_name}

If hardcode the variable, it will look like this (#2)

FROM anapsix/alpine-java:8u172b11_server-jre

MAINTAINER jim
enter code here
COPY src/account.jar /home/account.jar
CMD java -jar /home/account.jar

After I build the image with #1 and #2

Using #1, when I docker run, docker tell me it cannot find the specified jar file

Using #2, when I docker run, docker is able to execute the java jar file correctly.

To me both #1 and #2 are same. Just #1 use build-arg variable way and #2 is hardcoding the value. I believe the way I use build-args is incorrect. Can anyone guide me on this?

Regards

Upvotes: 6

Views: 5712

Answers (1)

Sukhpal Singh
Sukhpal Singh

Reputation: 2288

A running container won’t have access to an ARG variable value., you'll need ENV variable for that. Though you can use ARG variable to set ENV variable. In your situation you can do

FROM anapsix/alpine-java:8u172b11_server-jre
ARG project_file_name
ENV PROJECT_FILE=${project_file_name}

MAINTAINER jim
COPY src/${project_file_name} /home/${project_file_name}
CMD java -jar /home/${PROJECT_FILE}

You can read more here

Upvotes: 7

Related Questions