0% found this document useful (0 votes)
16 views

Develop Intelligence - Docker Multi-Stage Builds: #Build Stage Build-Env

Multi-stage builds allow building and publishing artifacts in one Dockerfile and copying them between stages. This avoids needing scripts to publish outputs. A .NET Core app can be built in one stage using the SDK image then copied to a runtime image. Stages can be named and only a target stage built. This enables debugging individual stages or using different stages for testing vs production. Examples show multi-stage Dockerfiles for Java compiling in one stage then copying class files to a runtime stage, and Python compiling in one stage then copying .pyc files to a slim runtime stage.

Uploaded by

saphana9800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Develop Intelligence - Docker Multi-Stage Builds: #Build Stage Build-Env

Multi-stage builds allow building and publishing artifacts in one Dockerfile and copying them between stages. This avoids needing scripts to publish outputs. A .NET Core app can be built in one stage using the SDK image then copied to a runtime image. Stages can be named and only a target stage built. This enables debugging individual stages or using different stages for testing vs production. Examples show multi-stage Dockerfiles for Java compiling in one stage then copying class files to a runtime stage, and Python compiling in one stage then copying .pyc files to a slim runtime stage.

Uploaded by

saphana9800
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Develop Intelligence – Docker Multi-Stage Builds

Multi-Stage Builds

 Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client.
 Before the multi-stage build feature was added, a script was required to copy the published output of your
build container onto your disk and then the runtime container would read in that output. This was tedious to
implement and not super-efficient. Now, containers can share build artifacts from different stages within a
single Dockerfile.
 Using this feature, you can build a .NET Core app in an SDK image and then copy the published app into a
runtime image all in the same Dockerfile.

Example:
#Build Stage

FROM mcr.microsoft.com/dotnet/sdk:6.0 as build-env

WORKDIR /src

COPY *.csproj .

RUN dotnet restore

COPY . .

RUN dotnet build

RUN dotnet publish -o /out

WORKDIR /out

ENTRYPOINT ["dotnet", "HelloWebApp.dll"]

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as production-env

WORKDIR /app

COPY --from=build-env /out .

ENTRYPOINT ["dotnet", "HelloWebApp.dll"]

NOTE: You can name your stages, by adding an AS to the FROM instruction.By default, the stages are not named,
and you can refer to them by their integer number, starting with 0 for the first FROM instruction.

Stop at a specific build stage:


When you build your image, you don’t necessarily need to build the entire Dockerfile including every stage. You
can specify a target build stage.
The following command assumes you are using the previous Dockerfile but stops at the stage named build-env:
$ docker build --target build-env -t sandeepsoni/hello-world:dev .
Develop Intelligence – Docker Multi-Stage Builds

$ docker build -t sandeepsoni/hello-world:prod .

A few scenarios where this might be very powerful are:


 Debugging a specific build stage
 Using a debug stage with all debugging symbols or tools enabled, and a lean production stage
 Using a testing stage in which your app gets populated with test data, but building for production using a
different stage which uses real data

Multi-Stage Dockerfile for Java


FROM openjdk:11 as base
WORKDIR /HelloWorld
COPY . .
RUN javac -d ./bin demo/HelloWorld.java

FROM openjdk:11-jre-slim
WORKDIR /app
COPY --from=base /HelloWorld/bin/demo/*.class ./demo/
ENTRYPOINT ["java", "demo.HelloWorld"]

Multi-Stage Dockerfile for Python


FROM python:3.9.1-buster as base
WORKDIR /app
COPY ["*.py", "."]
RUN python -m py_compile hello.py

FROM python:3.9.1-slim-buster
WORKDIR /app
COPY --from=base /app/__pycache__/*.pyc .
CMD ["python", "hello.cpython-39.pyc"]

You might also like