C++ 和 .Net 核心不在单个 Dockerfile 中构建

C++ and .Net core not building in single Dockerfile

我有以下 dockerfile。它正在编译 c++ 以及 .net 项目。

当我添加 .net 代码时,C++ 层无法正常工作。如果我删除 .Net 层,则 C++ 层可以正常工作。

这是不是不能在单个文件中完成?

# GCC support can be specified at major, minor, or micro version
# (e.g. 8, 8.2 or 8.2.0).
# See https://hub.docker.com/r/library/gcc/ for all supported GCC
# tags from Docker Hub.
# See https://docs.docker.com/samples/library/gcc/ for more on how to use this image
FROM gcc:latest

RUN apt-get update
RUN apt-get install -y libmotif-dev

# These commands copy your files into the specified directory in the image
# and set that as the working location
COPY . /usr/src/myapp

COPY ca-8-5-5-linux-x86-64/redist /usr/src/myapp/ca-8-5-5-linux-x86-64/sdk/demo

WORKDIR /usr/src/myapp/ca-8-5-5-linux-x86-64/sdk/samplecode/unix/
# This command compiles your app using GCC, adjust for your source code
RUN make

############################################################################################### 
NEED HELP HERE. ABOVE IS NOT WORKING. IF I REMOVE FOLLOWING THEN ABOVE WORKS.
###############################################################################################


FROM mcr.microsoft.com/dotnet/runtime:6.0-focal AS base
WORKDIR /app

# 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

COPY . .

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["sdk/samplecode/myAppExport/myAppExport.csproj", "./"]
RUN dotnet restore "myAppExport.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "sdk/samplecode/myAppExport/myAppExport.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "sdk/samplecode/myAppExport/myAppExport.csproj" -c Release -o /app/publish

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

以下是“Make”和 dotnet 构建在一起时我得到的日志。

[+] Building 19.4s (20/20) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                     0.0s
 => => transferring dockerfile: 1.70kB                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                        0.0s
 => => transferring context: 35B                                                                                                                                         0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0-focal                                                                                                  0.5s
 => [internal] load metadata for mcr.microsoft.com/dotnet/runtime:6.0-focal                                                                                              0.4s
 => [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:6.0-focal@sha256:213bd9012c064e2e80c9ffc17e4e4ebd97fe01a232370af2eab31ecf4c773fcb                                      0.0s
 => [base 1/4] FROM mcr.microsoft.com/dotnet/runtime:6.0-focal@sha256:9adcd9a2eee0506f461f81226bea8d725c5111809f1afcd12534c523f6406665                                   0.0s
 => => transferring context: 172.90MB                                                                                                                                    3.3s
 => CACHED [base 2/4] WORKDIR /app                                                                                                                                       0.0s
 => CACHED [base 3/4] RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app                                                                0.0s
 => CACHED [build 2/7] WORKDIR /src                                                                                                                                      0.0s
 => CACHED [build 3/7] COPY [sdk/samplecode/myAppExport/myAppExport.csproj, ./]                                                                                0.0s
 => CACHED [build 4/7] RUN dotnet restore "myAppExport.csproj"                                                                                                      0.0s
 => [build 5/7] COPY . .                                                                                                                                                 5.6s
 => [base 4/4] COPY . .                                                                                                                                                  5.6s
 => [final 1/2] WORKDIR /app                                                                                                                                             0.1s
 => [build 6/7] WORKDIR /src/.                                                                                                                                           0.1s
 => [build 7/7] RUN dotnet build "sdk/samplecode/myAppExport/myAppExport.csproj" -c Release -o /app/build                                                      5.5s
 => [publish 1/1] RUN dotnet publish "sdk/samplecode/myAppExport/myAppExport.csproj" -c Release -o /app/publish                                                2.3s
 => [final 2/2] COPY --from=publish /app/publish .                                                                                                                       0.1s
 => exporting to image                                                                                                                                                   1.8s
 => => exporting layers                                                                                                                                                  1.8s
 => => writing image sha256:96304565f390cf4ba352b792e6fb93c96832cdb41b56cd465feacf34f3f5005c                                                                             0.0s
 => => naming to docker.io/library/myAppextract:1                                                                                                                   0.0s

以下是只有 gcc 部分在 dockerfile 中时的日志(从 dockerfile 中删除了 dotnet 部分)。

[+] Building 10.8s (12/12) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                           0.0s
 => => transferring dockerfile: 1.74kB                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                              0.0s
 => => transferring context: 35B                                                                                                                                                               0.0s
 => [internal] load metadata for docker.io/library/gcc:latest                                                                                                                                  3.5s
 => [1/7] FROM docker.io/library/gcc:latest@sha256:084eaedf8e3c51f3db939ad7ed2b1455ff9ce4705845a014fb9fe5577b35c901                                                                            0.0s
 => [internal] load build context                                                                                                                                                              0.1s 
 => => transferring context: 54.94kB                                                                                                                                                           0.1s 
 => CACHED [2/7] RUN apt-get update                                                                                                                                                            0.0s
 => CACHED [3/7] RUN apt-get install -y libmotif-dev                                                                                                                                           0.0s 
 => [4/7] COPY . /usr/src/myapp                                                                                                                                                                2.8s 
 => [5/7] COPY ca-8-5-5-linux-x86-64/redist /usr/src/myapp/ca-8-5-5-linux-x86-64/sdk/demo                                                                                                      0.6s
 => [6/7] WORKDIR /usr/src/myapp/ca-8-5-5-linux-x86-64/sdk/samplecode/unix/                                                                                                                    0.1s
 => [7/7] RUN make                                                                                                                                                                             1.9s 
 => exporting to image                                                                                                                                                                         1.7s
 => => exporting layers                                                                                                                                                                        1.6s 
 => => writing image sha256:6904800ca9760db29751820b15f8952213ad084955f62c03ef98b6723b484420                                                                                                   0.0s
 => => naming to docker.io/library/myappextract:1

实际上我改变了解决问题的方法。

我在下面添加了 build-essential 而不是 FROM gcc:latest

RUN apt-get install -y libmotif-dev build-essential

并且安装了所有必需的 gccmake 依赖项,现在一切正常。