Dockerfiles: Multi Stage Example

This Dockerfile will create a slimmed down image containing only the assets that are generated from building and publishing the Corporate Website Solution. A subsequent action (not shown on this page) will then copy the necessary assets on top of the Sitecore Application Base Image.

The main steps in the Dockerfile are:

  1. Specify the allowed parameters (and also the defaults, by adding the equals sign)
  2. Create Prep image (from BUILD_IMAGE – image with SDK)
  3. Copy artifacts necessary for NuGet restore.
    1. SLN file into NuGet
    2. Copy ./src/ folder to temp
    3. Use robocopy to only copy required files and retain folder structure

      Note: This prep image is effectively another build image. The reason for separating out is because the result of this action is unlikely to change often, so can be cached and not ran in subsequent builds.
  4. Create Builder Image (from BUILD_IMAGE – image with SDK)
  5. Set Working Directory as c:\build
  6. Copy NuGet Artifacts and run NuGet Restore
  7. Copy Remaining Source Code from ./src/ of the Context
  8. Build and Publish to C:/out/website/ each project using MSBUILD
  9. Create final image from BASE_IMAGE (small image capable of storing files)
  10. Copy Final Assets from Builder Image to Final Image

The full Dockerfile can be seen below:

# escape=`



# Gather only artifacts necessary for NuGet restore, retaining directory structure.
COPY \src\*.sln \nuget\
COPY .\src\ \temp\
RUN Invoke-Expression 'robocopy C:\temp C:\nuget /s /ndl /njh /njs *.csproj *.scproj packages.config'



SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]

# Create an empty working directory
WORKDIR C:\build

# Copy prepped NuGet artifacts, and restore as distinct layer to take better advantage of caching
COPY --from=prep .\nuget .\
RUN nuget restore

# Copy remaining source code
COPY .\src\ .\

# Copy transforms, retaining directory structure
RUN Invoke-Expression 'robocopy C:\build\ C:\out\transforms /s /ndl /njh /njs *.xdt'

# Build website with file publish
RUN msbuild .\Unn.Ac.CustomSolution.sln /p:DeployOnBuild=true /p:PublishProfile=docker /p:AllowUntrustedCertificate=true /p:PublishDirectory=C:\out\website


WORKDIR C:\artifacts

# Copy final build artifacts
COPY --from=builder C:\out\website .\website\
COPY --from=builder C:\build\Healthchecks .\website\Healthchecks
COPY --from=builder C:\out\transforms .\transforms\

Leave a Reply

Your email address will not be published. Required fields are marked *