officefileapi-401528-integration-guide-dockerize-an-office-file-api-app.md
This tutorial describes how to create and dockerize an ASP.NET Core Web API application that uses the Office File API library to convert Excel and Word documents to HTML.
View Example: Dockerize an Office File API Application
Create a folder for your project (DocumentConversionWebApi in this example) and open this folder in Visual Studio Code.
Click View → Terminal in the main menu to open the integrated terminal.
Use the dotnet new command to create a new Web API application.
Install the DevExpress.Document.Processor and DevExpress.Drawing.Skia NuGet packages as described in this help topic: Use NuGet Packages to Install Office File API Components.
Implement an API controller to handle HTTP requests. In the Controllers folder, create a ConvertFileController.cs file with the code below. Implement a PostConvertFile action method to handle HTTP POST requests and to convert uploaded files to HTML.
Create a Dockerfile within your project folder. This file contains instructions required to build a Docker image and deploy it inside a container. Add the following commands to your Dockerfile:
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Install the latest version of font libraries
RUN apt update &&\
apt install -y libc6 libicu-dev libfontconfig1
# (Optional step) Install the ttf-mscorefonts-installer package
# to use Microsoft TrueType core fonts in the application
RUN echo "deb http://ftp.debian.org/debian/ bookworm contrib" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y ttf-mscorefonts-installer
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
# Copy the project file
COPY ./DocumentConversionWebApi.csproj ./
# Restore as distinct layers
RUN dotnet restore
# Publish a release
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "DocumentConversionWebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
# Define the entry point for the application
ENTRYPOINT ["dotnet", "DocumentConversionWebApi.dll"]
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Install dependencies
RUN apt update
RUN apt install -y libc6 libicu-dev libfontconfig1
# (Optional step) Install the ttf-mscorefonts-installer package
# to use Microsoft TrueType core fonts in the application
RUN echo ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true | debconf-set-selections
RUN apt install -y ttf-mscorefonts-installer
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# Copy project files and build the application
COPY . ./
# Restore packages from nuget.org (default source)
RUN dotnet restore
# Publish a release
RUN dotnet publish "DocumentConversionWebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
# Define the entry point for the application
ENTRYPOINT ["dotnet", "DocumentConversionWebApi.dll"]
# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
# Install dependencies
RUN apk update && apk upgrade
RUN apk add icu-libs icu-data-full fontconfig
# (Optional step) Install the ttf-mscorefonts-installer package
# to use Microsoft TrueType core fonts in the application
RUN apk add ttf-dejavu && \
apk add msttcorefonts-installer && \
apk add ttf-dejavu && \
update-ms-fonts && \
fc-cache -f
ENV DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=false
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# Copy project files and build the application
COPY . ./
# Restore packages from nuget.org (default source)
RUN dotnet restore
# Publish a release
RUN dotnet publish "DocumentConversionWebApi.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=build /app/publish .
# Define the entry point for the application
ENTRYPOINT ["dotnet", "DocumentConversionWebApi.dll"]
Add a .dockerignore file to your project. Exclude the following folders from the build context to increase build performance:
bin/
obj/
Use the following terminal commands to build and run your Docker image:
docker build -t documentconversionwebapi .
docker run -d -p 8080:80 documentconversionwebapi
This tutorial uses the Thunder Client extension to test the created Web API application.
Install and start Thunder Client in Visual Studio Code. Click New Request to create a new HTTP request to the API endpoint. Select the POST method in the drop-down list and enter the following URL in the input field:
Specify a Request Body for the POST request. Switch to the Body tab and click Form.
Select the Files check box to display the Files section. Enter file as the field name and click Choose File to select a document you wish to convert to HTML.
Click Send to upload the selected file to the server and convert this document to HTML.
See Also
Use NuGet Packages to Install Office File API Components
Use the Spreadsheet Document API to Create a Loan Amortization Schedule within a Blazor Server App
Use the Word Document API to Generate and Send Business Letters within a Blazor Server App
Load and Use Custom Fonts Without Installation on the System