构建 ASP.NET 核心和 C++ 二进制文件的 Dockerfile

Dockerfile that Builds both ASP.NET Core and C++ binaries

我需要构建一个 ASP.NET 核心应用程序,它调用 C++ 可执行文件来完成一些工作。我有 docker 文件来为 .NET Core 和 C++ 构建图像,效果很好,但是 分别 .

ASP.NET 核心 Docker 文件如下所示:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 4444
EXPOSE 5599
ENV ASPNETCORE_URLS=https://+:4444;https://+:5599

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
# RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
# USER appuser

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

COPY ["Core/Blundergat.Common/Blundergat.Common.csproj", "Core/Blundergat.Common/"]
COPY ["Core/Blundergat.Core/Blundergat.Core.csproj", "Core/Blundergat.Core/"]
COPY ["Core/Blundergat.Domain/Blundergat.Domain.csproj", "Core/Blundergat.Domain/"]
COPY ["Io/Blundergat.Io/Blundergat.Io.csproj", "Io/Blundergat.Io/"]
COPY ["Meshing/Blundergat.Meshing.Generator/Blundergat.Meshing.Generator.csproj", "Meshing/Blundergat.Meshing.Generator/"]
COPY ["Meshing/Blundergat.Meshing.Decimator/Blundergat.Meshing.Decimator.csproj", "Meshing/Blundergat.Meshing.Decimator/"]
COPY ["Optimization/Blundergat.Fireworks/Blundergat.Fireworks.csproj", "Optimization/Blundergat.Fireworks/"]
COPY ["Registration/Blundergat.CoarseRegistration/Blundergat.CoarseRegistration.csproj", "Registration/Blundergat.CoarseRegistration/"]
COPY ["Registration/Blundergat.FineRegistration/Blundergat.FineRegistration.csproj", "Registration/Blundergat.FineRegistration/"]
COPY ["Services/Blundergat/Blundergat.csproj", "Services/Blundergat/"]
COPY ["Storage/Blundergat.Storage/Blundergat.Storage.csproj", "Storage/Blundergat.Storage/"]
COPY ["Transport/Blundergat.Grpc/Blundergat.Grpc.csproj", "Transport/Blundergat.Grpc/"]

RUN dotnet restore "Services/Blundergat/Blundergat.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Services/Blundergat/Blundergat.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Services/Blundergat/Blundergat.csproj" -c Release -o /app/publish

FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .
COPY --from=ubuntu /src/bin/PoissonRecon .
#ENTRYPOINT ["dotnet", "Blundergat.dll"]
ENTRYPOINT /app/Blundergat start && /bin/bash

效果很好。

C++ Docker文件如下所示:

FROM ubuntu:16.04 AS ubuntu

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        vim g++ make

WORKDIR "/src/"

COPY ["Meshing/Blundergat.Meshing/jpeg", "jpeg/"]
COPY ["Meshing/Blundergat.Meshing/png", "png/"]
COPY ["Meshing/Blundergat.Meshing/zlib", "zlib/"]
COPY ["Meshing/Blundergat.Meshing/linux", "src/"]
COPY ["Meshing/Blundergat.Meshing/Makefile", "."]

# TODO REMOVE unwanted files. 

RUN make

这可以编译,并且可执行文件 运行 也很好。

因此,我尝试将两者结合如下:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 4444
EXPOSE 5599
ENV ASPNETCORE_URLS=https://+:4444;https://+:5599

FROM ubuntu:16.04 AS ubuntu

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        vim g++ make

WORKDIR "/src/"

COPY ["Meshing/Blundergat.Meshing/jpeg", "jpeg/"]
COPY ["Meshing/Blundergat.Meshing/png", "png/"]
COPY ["Meshing/Blundergat.Meshing/zlib", "zlib/"]
COPY ["Meshing/Blundergat.Meshing/linux", "src/"]
COPY ["Meshing/Blundergat.Meshing/Makefile", "."]

#REMOVE unwanted files. 

RUN make

# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
# RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
# USER appuser

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

COPY ["Core/Blundergat.Common/Blundergat.Common.csproj", "Core/Blundergat.Common/"]
COPY ["Core/Blundergat.Core/Blundergat.Core.csproj", "Core/Blundergat.Core/"]
COPY ["Core/Blundergat.Domain/Blundergat.Domain.csproj", "Core/Blundergat.Domain/"]
COPY ["Io/Blundergat.Io/Blundergat.Io.csproj", "Io/Blundergat.Io/"]
COPY ["Meshing/Blundergat.Meshing.Generator/Blundergat.Meshing.Generator.csproj", "Meshing/Blundergat.Meshing.Generator/"]
COPY ["Meshing/Blundergat.Meshing.Decimator/Blundergat.Meshing.Decimator.csproj", "Meshing/Blundergat.Meshing.Decimator/"]
COPY ["Optimization/Blundergat.Fireworks/Blundergat.Fireworks.csproj", "Optimization/Blundergat.Fireworks/"]
COPY ["Registration/Blundergat.CoarseRegistration/Blundergat.CoarseRegistration.csproj", "Registration/Blundergat.CoarseRegistration/"]
COPY ["Registration/Blundergat.FineRegistration/Blundergat.FineRegistration.csproj", "Registration/Blundergat.FineRegistration/"]
COPY ["Services/Blundergat/Blundergat.csproj", "Services/Blundergat/"]
COPY ["Storage/Blundergat.Storage/Blundergat.Storage.csproj", "Storage/Blundergat.Storage/"]
COPY ["Transport/Blundergat.Grpc/Blundergat.Grpc.csproj", "Transport/Blundergat.Grpc/"]

RUN dotnet restore "Services/Blundergat/Blundergat.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "Services/Blundergat/Blundergat.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Services/Blundergat/Blundergat.csproj" -c Release -o /app/publish

FROM base AS final

WORKDIR /app

COPY --from=publish /app/publish .
COPY --from=ubuntu /src/bin/PoissonRecon .
#ENTRYPOINT ["dotnet", "Blundergat.dll"]
ENTRYPOINT /app/Blundergat start && /bin/bash

这可以很好地编译 C++ 和 .NET Core 的东西。

问题是基础 ubuntu:16.04 图像丢失了,所以当我尝试 运行 C++ 可执行文件(使用 -lgomp g++/gcc 选项编译)时,我得到了以下错误:

./PoissonRecon: error while loading shared libraries: libgomp.so.1: cannot open shared object file: No such file or directory

这是因为 /usr/lib/ 文件夹仅包含 .NET 程序集的最低限度。

我如何构建这个 Docker 图像以便我编译两者,但将所需的 C++ 库保留在 /usr/lib/ 目录中?

有点奇怪,因为 libgomp1 似乎没有默认安装在 ubuntu:16.04 上,甚至在 运行 apt-get update && apt-get install -y --no-install-recommends 之后,所以可能会有一些不同在 ubuntu 16.04 和需要该软件包的 Debian buster (10) 之间。

不过,您可以使用 apt-get update && apt-get install -y libgomp1 自行将其安装在已有映像 (mcr.microsoft.com/dotnet/aspnet:5.0) 之上。另一种选择是使用基于 ubuntu 20.04 的版本 (mcr.microsoft.com/dotnet/aspnet:5.0-focal) 或检查 this link 以了解其他版本和体系结构,使用此图像可能会出于相同(但未知)的原因起作用适用于您的常规 Ubuntu 服务器。

第三个更全面的选项是,因为您知道它适用于您当前的 ubuntu:16.04 图像,您可以按照指定在 it 上安装 aspnet 5.0 here

更新:

在基于 Debian 的原始映像上安装丢失的软件包 (libgomp1) 解决了这个问题