Using Sitecore Dianoga Asset Image in Containerized Environment

Anton Tishchenko
Anton Tishchenko
Cover Image for Using Sitecore Dianoga Asset Image in Containerized Environment

We built Dianoga Asset Image in the previous article. Now, let’s figure out how to use it in your Docker setup.

The process of configuration Dianoga will be very similar to the process of configuration SXA or any other Sitecore modules. Because we built our image to have the same structure as Sitecore uses for their asset images. You will need:

  1. Open your docker-compose file
  2. Find the definition for cd instance
  3. Add argument
DIANOGA_IMAGE: ${REGISTRY}${COMPOSE_PROJECT_NAME}-dianoga-assets:${VERSION:-lates}

DIANOGA_IMAGE could be the image that you build by yourself, or you can use an image that I build using Github Actions. Actual version on current date is antonytm/dianoga-assets:6.0.1-net48-Release-8.

  1. Open your Dockerfile of cd instance
  2. Add next code:
...
ARG DIANOGA_IMAGE
...
FROM ${DIANOGA_IMAGE} as dianoga
...
WORKDIR C:\inetpub\wwwroot
# Add Dianoga module
# Install Microsoft Visual C++ Runtime, because image tools(e.g. pngquant.exe, avifenc.exe) depends on it.
RUN Powershell Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
RUN choco install vcredist-all -y
# Copy files from Dianoga Asset Image to your website folder
COPY --from=dianoga \module\cd\content .\
...
# Optionally enable all configurations that you need, for example:
# Enable SVG optimization
RUN Rename-Item -Path C:\inetpub\wwwroot\App_Config\Include\Dianoga\Dianoga.Svg.config.disabled -NewName Dianoga.Svg.config
# Enable WebP optimization
RUN Rename-Item -Path C:\inetpub\wwwroot\App_Config\Include\Dianoga\z.01.Dianoga.NextGenFormats.WebP.config.disabled -NewName z.01.Dianoga.NextGenFormats.WebP.config
# Enable AVIF optimization
RUN Rename-Item -Path C:\inetpub\wwwroot\App_Config\Include\Dianoga\z.02.Dianoga.NextGenFormats.Avif.config.disabled -NewName z.02.Dianoga.NextGenFormats.Avif.config.config
# Enable JXL optimization
RUN Rename-Item -Path C:\inetpub\wwwroot\App_Config\Include\Dianoga\z.03.Dianoga.NextGenFormats.JpegXL.config.disabled -NewName z.03.Dianoga.NextGenFormats.JpegXL.config

# If you want to use webp/jpegxl/avif, you need to make changes in your web.config file
# Change default Media Request Handler to be able to use NextGenFormats
COPY Web.config.xdt ./Web.config.Dianoga.xdt
RUN Powershell C:\tools\scripts\Invoke-XdtTransform.ps1 -Path Web.config -XdtPath Web.config.Dianoga.xdt

and web.config.Dianoga.xdt itself:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <system.webServer>
      <handlers>
         <add verb="*" path="sitecore_media.ashx" name="Sitecore.MediaRequestHandler" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" type="Dianoga.NextGenFormats.MediaRequestHandlerXA, Dianoga" />
      </handlers>
   </system.webServer>
</configuration>
  1. Run docker-compose build
  2. Run docker-compose up

That is all. Now you don’t need to include Dianoga as a NuGet package for your project.