Hello and welcome to our tiny tip of the day!
I wish to build an experimental standalone binary app in controlled environment and just pull out the results on my machine, so I can avoid installing all the build dependencies.
Shouldn’t Docker help me with such thing? And sure it DOES xD!
I’ve mainly used Docker to create runnable images with web applications exposing their ports, so this was new for me! I hope you find it simple and useful too!
1. Create your build Dockerfile as usual and then add a scratch images to expose only the result files:
# Dockerfile with conventional Java Application build with Gradle
FROM eclipse-temurin:17-jdk-jammy as build
WORKDIR /app
COPY build.gradle .
COPY gradlew .
COPY settings.gradle .
COPY gradle/ ./gradle
COPY src/ ./src
RUN ./gradlew build
# Expose /build content
FROM scratch AS binaries
COPY --from=build /app/build /
scratch is the minimal image used to create base images in Docker
2. Build image with output and target params
In your preferred terminal, navigate at your Dockerfile directory and run the following command:
docker build --output=<OUTPUT_PATH> --target=<TARGET_NAME> .
TARGET_NAME: the instruction name inside your Dockerfile where you’ve placed the result files.
OUTPUT_PATH: the path on your machine to where you want to copy the result files
For the Dockerfile sample above, run the following command to get the output in the ./bin folder:
docker build --output=./bin --target=binaries .
And… Dekiagari (Voila)!
Check the ./bin folder created with all the generated resources resulting from the build action.
Read the complete documentation from official Docker website here 🙂
Hope you find it useful! You can fund the sample code in my repository here. Have a nice coding day!
