gpt4 book ai didi

c++ - 构建 ASP.NET Core 和 C++ 二进制文件的 Dockerfile

转载 作者:行者123 更新时间:2023-12-04 11:12:14 28 4
gpt4 key购买 nike

我需要构建一个 ASP.NET Core 应用程序,它调用 C++ 可执行文件来完成一些工作。我有 docker 文件来为 .NET Core 和 C++ 构建两个图像,但它们分别运行得很好。
ASP.NET Core Dockerfile 如下所示:

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++ Dockerfile 如下所示:
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 镜像,以便我编译两者,但在 /usr/lib/ 中保留所需的 C++ 库目录?

最佳答案

这有点奇怪,因为 libgomp1 ubuntu:16.04 上似乎没有默认安装甚至在运行后 apt-get update && apt-get install -y --no-install-recommends因此需要该软件包的 ubuntu 16.04 和 Debian buster (10) 之间可能存在一些差异。
但是,您可以使用 mcr.microsoft.com/dotnet/aspnet:5.0 将它自己安装在您预先存在的图像( apt-get update && apt-get install -y libgomp1 )之上。 .另一种选择是使用基于 ubuntu 20.04 的 ( mcr.microsoft.com/dotnet/aspnet:5.0-focal ) 或检查 this link 对于其他版本和体系结构,使用此图像可能与在常规 Ubuntu 服务器上工作的原因相同(但未知)。
第三个更全面的选项是,因为您知道它适用于您当前的 ubuntu:16.04 镜像,您可以在 上安装 aspnet 5.0。它如指定 here
更新:
在基于 Debian 的原始镜像上安装缺少的软件包 ( libgomp1 ) 解决了该问题

关于c++ - 构建 ASP.NET Core 和 C++ 二进制文件的 Dockerfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68915515/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com