Skip to content

Instantly share code, notes, and snippets.

@oscarrenalias
Last active September 12, 2017 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarrenalias/652a030cfc42e6fdad7658c5e1e9f431 to your computer and use it in GitHub Desktop.
Save oscarrenalias/652a030cfc42e6fdad7658c5e1e9f431 to your computer and use it in GitHub Desktop.

Introduction

This is an example of a Docker multi-stage build.

Multi-stage builds allow Dockerfiles to use separate containers for building and for execution, thereby preventing final containers from being bloated with development tooling.

In this specific exampe, we use the OpenJDK container to compile some Java code and then we build an executable container that only has the JRE. In a real world scenario, the build container would likely also contain tools like Maven and Gradle, as well as the dependencies that these build tools would eventually download.

Pre-requisites

Docker 17.06

Running the example

docker build -t multi-stage .
docker run multi-sage
FROM openjdk:8-jdk AS builder
RUN mkdir /src
COPY HelloWorld.java /src
RUN javac /src/HelloWorld.java
FROM openjdk:8-jre
COPY --from=builder /src/HelloWorld.class /
WORKDIR /
CMD ["java", "HelloWorld"]
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment