2.1.8 Compiling code with a multistage build

View in the book. Buy the book.

Using a multi-step build to compile code.

It’s not ideal to compile code directly in the final container. For example, run ls on the prior container

$ docker run compiled_code ls
Dockerfile
Hello.class
Hello.java

Instead, we can use a 2-stage build

2.2 A multistage container build, where an intermediate container is used to build the binary
Figure 2.2 A multistage container build, where an intermediate container is used to build the binary

Here’s the dockerfile

Build & run

$ cd Chapter02/2.1.8_MultiStage
$ docker build . -t compiled_code2
$ docker run compiled_code2
Hello Docker

Compare with ls:

$ docker run compiled_code2 ls
Hello.class