Skip to content

Commit 4a1880d

Browse files
authored
Merge pull request #1 from nais/jar-file-build-arg
Set JAR file as environment variable
2 parents 098e0a8 + f64181f commit 4a1880d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

java-8/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ WORKDIR /app
1313

1414
EXPOSE 8080
1515

16-
CMD ["sh", "-c", "java", "$DEFAULT_JAVA_OPTS $JAVA_OPTS", "-jar", "/app/app.jar"]
16+
CMD ["sh", "-c", "java", "$DEFAULT_JAVA_OPTS", "$JAVA_OPTS", "-jar", "${JAR_FILE:-app.jar}"]

java-8/README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@ WIP draft.
77
Usage
88
---------------------
99

10-
Build your app to `./target/app.jar`.
10+
Build your app to `./target/app.jar` or provide your own jar file
11+
using the `JAR_FILE` environment variable.
1112

1213
Make your app expose services on port 8080 as default.
1314

1415
Include custom Java options in `$JAVA_OPTS`.
1516

17+
Example
18+
---------------------
19+
20+
The `Dockerfile` below allows us to specify an alternate JAR file build time (via `--build-arg JAR_FILE=my.jar`)
21+
and run time as an environment variable. We also specify additional options for the JVM:
1622

1723
```
1824
FROM nais:java-8
1925
20-
ENV JAVA_OPTS="-Djavax.net.ssl.trustStore=/truststore.jks -Djavax.net.ssl.trustStorePassword=changeit"
26+
ARG JAR_FILE
27+
ENV JAR_FILE ${JAR_FILE}
28+
29+
ENV JAVA_OPTS="-Djavax.net.ssl.trustStore=/truststore.jks \
30+
-Djavax.net.ssl.trustStorePassword=changeit"
31+
32+
```
33+
34+
The JAR file can be a bit simplified during build:
2135

36+
```
37+
ARG BUILD_NO
38+
ENV JAR_FILE ${JAR_FILE:-myapp-${BUILD_NO}.jar}
39+
```
40+
41+
This will either use the environment variable `JAR_FILE` or fall back
42+
to the default `myapp-BUILD_NO`, where `BUILD_NO` is specified build time:
43+
44+
```
45+
docker build -t myapp:latest --build-arg BUILD_NO=1337 .
2246
```

0 commit comments

Comments
 (0)