Thursday, July 7, 2022

Dianoga Docker Asset Image

 This is a copy of my blog post originally posted here to keep all things in one place.

Before reading this article, I do recommend you read
 about Sitecore Docker asset images. It will make understanding what is going on here much easier. Let's build our first Sitecore Docker Asset image.

Let’s create Dianoga Docker Image Asset for Sitecore.

Dianoga is a good candidate for start. It is well-known community module, which means that what we create will be used by others. It doesn’t have any items, it is easy to build the image. And it has many different configurations. Having the image may be used for integration testing and reproduction of bugs with non-standard configuration.

Each Docker image should have a base image. Let’s define what image will we use as a base. You need to remember that our image will be used only as a container of files. It makes sense to take the smallest image that is possible. It will be mcr.microsoft.com/windows/nanoserver:1809 for Windows images. (There is even a reserved scratch image in Docker, but that is designed for Linux images and it is a separate story)

ARG BASE_IMAGE=mcr.microsoft.com/windows/nanoserver:1809

But as the input to build the image, we will get only Dianoga sources. We need to compile them. And as we selected the base image smallest possible, we don’t have anything there to build C# project. That is why we need one more image: “build image”. It should contain all build tools that we need. For our case, it will be mcr.microsoft.com/dotnet/framework/sdk:4.8

ARG BUILD_IMAGE=mcr.microsoft.com/dotnet/framework/sdk:4.8
FROM ${BUILD_IMAGE} AS build-env

Dianoga could be built in 2 configurations: debug and release. Also, a different .Net version should be used depending on what Sitecore version, where we want to use it. It would be nice to put these values as arguments:

ARG BUILD_CONFIGURATION=
ARG DOTNET_VERSION_ARG=

Now, we are ready to build Dianoga project inside Docker containers.

First of all, we need to register Sitecore Nuget packages source

RUN dotnet nuget add source https://sitecore.myget.org/F/sc-packages/api/v3/index.json

Then copy all project and solution files and restore Nuget packages

COPY *.sln ./
COPY src/Dianoga/Dianoga.csproj ./src/Dianoga/
COPY src/Dianoga.Tests/Dianoga.Tests.csproj ./src/Dianoga.Tests/
RUN dotnet restore

After a successful package restoration, we may copy the whole project and build it

COPY src ./src
RUN dotnet build -c $env:BUILD_CONFIGURATION

After a successful build, we need to copy everything that we need from the “build image” into the “base image”.

RUN mkdir ./src/bin
RUN Copy ./src/Dianoga/bin/$env:BUILD_CONFIGURATION/$env:DOTNET_VERSION_ARG/Dianoga.* ./src/bin/
RUN Copy ./src/Dianoga/bin/$env:BUILD_CONFIGURATION/$env:DOTNET_VERSION_ARG/System.Threading.Tasks.Dataflow.dll ./src/bin/

FROM ${BASE_IMAGE}

# Copy Dianoga dll and pdb (if present)
COPY --from=build-env /src/bin/ ./module/cd/content/bin/
COPY --from=build-env /src/bin/ ./module/cm/content/bin/

# Copy Dianoga Tools
ARG src="/src/Dianoga/Dianoga Tools"
ARG target="./module/cm/content/App_Data/Dianoga Tools"
COPY --from=build-env ${src} ${target}

ARG src="/src/Dianoga/Dianoga Tools"
ARG target="./module/cd/content/App_Data/Dianoga Tools"
COPY --from=build-env ${src} ${target}

# Copy Configs
ARG src="/src/Dianoga/Default Config Files"
ARG target="./module/cm/content/App_Config/Include/Dianoga"
COPY --from=build-env ${src} ${target}

ARG src="/src/Dianoga/Default Config Files"
ARG target="./module/cd/content/App_Config/Include/Dianoga"
COPY --from=build-env ${src} ${target}

Dockerfile is ready and we can build the Dianoga Docker Asset Image

docker build . --build-arg DOTNET_VERSION_ARG=net48 --build-arg BUILD_CONFIGURATION=debug

Now we are ready to use the Sitecore Dianoga Docker image in our projects. It will be described in further articles.

No comments:

Post a Comment