blue-sky
blue-sky

Reputation: 53826

Docker arg not being read correctly from command line

Using this command to start and run my docker container passing in argument ./target/myapp-SNAPSHOT.jar where ./target/myapp-SNAPSHOT.jar is the Spring boot app I wish to run :

docker build -t foo . && docker run -it foo -e J_FILE=./target/myapp-SNAPSHOT.jar

I receive error :

standard_init_linux.go:211: exec user process caused "exec format error"

Dockerfile:

FROM adoptopenjdk/openjdk8:alpine-jre

LABEL app.name="test"
LABEL app.type="test"

ARG J_FILE
ADD ${J_FILE} /myapp.jar

COPY J_FILE /myapp.jar

COPY startup.sh /
RUN chmod +x startup.sh;

ENTRYPOINT ["/startup.sh"]
EXPOSE 8080

startup.sh :

#!/bin/sh

JAVA_HEAP_INITIAL=384m
JAVA_HEAP_MAX=768m
JAVA_METASPACE_MAX=128m

java -jar /app.jar

If I change Dockerfile to explicitly copy the jar file :

FROM adoptopenjdk/openjdk8:alpine-jre

LABEL app.name="test"
LABEL app.type="test"

COPY ./target/myapp-SNAPSHOT.jar /myapp.jar

COPY startup.sh /
RUN chmod +x startup.sh;

ENTRYPOINT ["/startup.sh"]
EXPOSE 8080

The app starts as expected.

Upvotes: 0

Views: 472

Answers (1)

Nguyen Lam Phuc
Nguyen Lam Phuc

Reputation: 1421

The ARG flag in Dockerfile is meant for configuration when building a docker image. It will not have any effect when you run the container.

Therefore, you should update your command to be like this:

docker build --build-arg J_FILE=./target/myapp-SNAPSHOT.jar -t foo . && docker run -it foo

You can also update your Dockerfile to be like this:

FROM adoptopenjdk/openjdk8:alpine-jre

LABEL app.name="test"
LABEL app.type="test"

ARG J_FILE=./target/myapp-SNAPSHOT.jar
COPY ${J_FILE} /myapp.jar

COPY startup.sh /
RUN chmod +x startup.sh;

ENTRYPOINT ["/startup.sh"]
EXPOSE 8080

Note 2 changes in the file above:

  1. Use COPY instead of ADD
  2. Set the default value for J_FILE as ./target/myapp-SNAPSHOT.jar in case the --build-arg is not passed with the docker build command.

Upvotes: 1

Related Questions