2

During the Docker build phase I pass in an argument for the particular JAR file I want to copy into the container.

Docker Build

docker build -t foo-service . --build-arg FAT_JAR=foo-service-1.0.0-SNAPSHOT

Dockerfile

FROM maven:3.3-jdk-8-alpine

# Install packages
RUN apk add --no-cache curl tar bash wget

EXPOSE 9000

ARG FAT_JAR

RUN echo "Copying $FAT_JAR.jar to container"


# Copy Fat Jar to directory
RUN mkdir /home/test
COPY target/$FAT_JAR.jar /home/test/
ADD config /home/test/config

# Set Jar permissions
RUN chmod -R 766 /home/test

CMD cd /home/test && java -jar "$FAT_JAR.jar" server config/local.yml

When I try to run the Docker container it cannot open my jar file because $FAT_JAR is not persisted inside the container.

Docker Run

docker run --add-host="localhost:10.0.2.2" -t foo-service

Error

Error: Unable to access jarfile .jar

How can I persist or make the Container know which file it needs to load?

i.e.

java -jar foo-service-1.0.0-SNAPSHOT.jar server config/local.yml

Thanks

tomaytotomato
  • 3,788
  • 16
  • 64
  • 119
  • Where do I use that, during build or run? – tomaytotomato Nov 21 '16 at 17:25
  • Sorry, I misread the question. Your real problem is that you're putting the jar in /home/test, but you're running java from /home/checkout -- the jar isn't in the checkout directory, it's in the test directory. – Software Engineer Nov 21 '16 at 18:06
  • Yea that was a typo from obfusctation, still the same issue. – tomaytotomato Nov 22 '16 at 09:39
  • Do you mean that you need to select a jar file according to the running ENV? – John Zeng Nov 22 '16 at 10:24
  • 1
    While ARG defines build-time arguments, CMD instruction is evaluated when the container is running. Have you tried running like: `docker run --add-host="localhost:10.0.2.2" -t -e FAT_JAR='foo-service-1.0.0-SNAPSHOT' foo-service` – Kadir Nov 22 '16 at 13:57
  • 2
    If you really want to specify it at build time use the ENV instruction in the dockerfile and set a variable to the given argument's value. – Software Engineer Nov 22 '16 at 14:45
  • Possible duplicate of [ARG or ENV, which one to use in this case?](https://stackoverflow.com/questions/41916386/arg-or-env-which-one-to-use-in-this-case) – Pierre Gillet Jul 09 '18 at 15:43

0 Answers0