Anto Subash.

abp
dotnet
docker

Abp Dockerfile

Table of contents

Introduction

In this post we will see how to create proper docker file for your abp application.

What is the problem?

If you are using abp framework for your application, you might have noticed that abp framework stoped including the "libs" folder in the application template. This was done to reduce the size of the application. It is a good practice to not include the "libs" folder in the sorces control. But if you are using docker, you need to include the "libs" folder in the docker image. This is a problem when you are doing CI/CD.

We will see what are the ways to solve this problem.

Solutions

There are few ways to solve this problem.

1. Include the "libs" folder in the source control

This is the easiest way to solve the problem. But it is not a good practice to include the "libs" folder in the source control. I have been using this method for a long time. But I have decided to move on and find a better solution.

2. Install the npm packages in CI/CD pipeline

I have tried this method. It works but it is not a good solution. Because then i'm tied to the CI/CD product. If i want to change the CI/CD product, i have to change the way i install the npm packages.

3. Create a docker file which will install the npm packages

This is the best solution. We will create a docker file which will install the npm packages. Then we will use this docker file in our CI/CD pipeline.

Create a docker file

We will create a docker file which will install the npm packages. Then we will use this docker file in our CI/CD pipeline.

1. Create a docker file

Create a docker file in the root of your application. Name it "Dockerfile".

2. Add the following content to the docker file

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

ENV ASPNETCORE_URLS=http://+:80

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src

# set up node
ENV NODE_VERSION 16.13.0
ENV NODE_DOWNLOAD_URL https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
ENV NODE_DOWNLOAD_SHA 589b7e7eb22f8358797a2c14a0bd865459d0b44458b8f05d2721294dacc7f734

RUN curl -SL "$NODE_DOWNLOAD_URL" --output nodejs.tar.gz \
    && echo "$NODE_DOWNLOAD_SHA nodejs.tar.gz" | sha256sum -c - \
    && tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1 \
    && rm nodejs.tar.gz \
    && ln -s /usr/local/bin/node /usr/local/bin/nodejs

RUN apt update && apt -y install gnupg

ENV YARN_VERSION 1.22.15

RUN set -ex \
  && wget -qO- https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --import \
  && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
  && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz.asc" \
  && gpg --batch --verify yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz \
  && mkdir -p /opt/yarn \
  && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/yarn --strip-components=1 \
  && ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn \
  && ln -s /opt/yarn/bin/yarn /usr/local/bin/yarnpkg \
  && rm yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz

COPY ["abp/AbpTemplate/AbpTemplate.csproj", "abp/AbpTemplate/"]
RUN dotnet restore "abp/AbpTemplate/AbpTemplate.csproj"
COPY . .
WORKDIR "/src/abp/AbpTemplate"
RUN dotnet tool install -g Volo.Abp.Cli
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN abp install-libs
RUN dotnet build "AbpTemplate.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "AbpTemplate.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AbpTemplate.dll"]

This docker file will install the npm packages. Then it will build the application.

if you are new to docker, then this docker file might look complicated. But it is not. It is just a bunch of commands. You can read more about docker files here.

let me explain what each command is doing.

FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80

ENV ASPNETCORE_URLS=http://+:80

This is the base image. It is using the dotnet 7.0 aspnet image. It is also exposing the port 80.

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src

This is the build image. It is using the dotnet 7.0 sdk image. It is also setting the working directory to "/src".

# set up node
ENV NODE_VERSION 16.13.0
ENV NODE_DOWNLOAD_URL https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.gz
ENV NODE_DOWNLOAD_SHA 589b7e7eb22f8358797a2c14a0bd865459d0b44458b8f05d2721294dacc7f734

RUN curl -SL "$NODE_DOWNLOAD_URL" --output nodejs.tar.gz \
    && echo "$NODE_DOWNLOAD_SHA nodejs.tar.gz" | sha256sum -c - \
    && tar -xzf "nodejs.tar.gz" -C /usr/local --strip-components=1 \
    && rm nodejs.tar.gz \
    && ln -s /usr/local/bin/node /usr/local/bin/nodejs

RUN apt update && apt -y install gnupg

ENV YARN_VERSION 1.22.15

RUN set -ex \
  && wget -qO- https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --import \
  && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" \
  && curl -fSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz.asc" \
  && gpg --batch --verify yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz \
  && mkdir -p /opt/yarn \
  && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/yarn --strip-components=1 \
  && ln -s /opt/yarn/bin/yarn /usr/local/bin/yarn \
  && ln -s /opt/yarn/bin/yarn /usr/local/bin/yarnpkg \
  && rm yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz

This is the part where we are installing the node and yarn. You can change the node version to the version you are using. You can also change the yarn version to the version you are using.

COPY ["abp/AbpTemplate/AbpTemplate.csproj", "abp/AbpTemplate/"]
RUN dotnet restore "abp/AbpTemplate/AbpTemplate.csproj"
COPY . .
WORKDIR "/src/abp/AbpTemplate"
RUN dotnet tool install -g Volo.Abp.Cli
ENV PATH="${PATH}:/root/.dotnet/tools"
RUN abp install-libs
RUN dotnet build "AbpTemplate.csproj" -c Release -o /app/build

This is the part where we are restoring the nuget packages and building the application. You can change the project name to the name of your project. You can also change the working directory to the directory of your project.

We are also installing the abp cli. You can read more about the abp cli here. The abp cli is used to install the npm packages. You can also use the npm cli to install the npm packages. But I prefer to use the abp cli because you can use the abp cli to install the npm packages for all the modules in your solution.

FROM build AS publish
RUN dotnet publish "AbpTemplate.csproj" -c Release -o /app/publish /p:UseAppHost=false

This is the part where we are publishing the application.

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "AbpTemplate.dll"]

This is the part where we are copying the published application to the base image.

Here is the link to the docker file in github. Dockerfile

Here is the repo for the application. AbpTemplate

Conclusion

In this post we saw how to create a docker file which will install the npm packages for your abp application. This will help you to create a docker image which will have the "libs" folder for your CI/CD pipeline. Hope this helps. Fell free to leave a comment if you have any questions.

Buy Me a Coffee at ko-fi.com