运行 C# dockerfile 中的可执行文件

Running an executable in a C# dockerfile

我是 Docker 的新手。我创建了一个使用 C# 进程运行 mongodump 的 C# 项目

   var process = new Process () {
                StartInfo = new ProcessStartInfo {
                FileName = "mongodump",
                Arguments = "--db vct --collection tr -u vt -p vct13 --authenticationDatabase admin --host localhost  --port 27017 --gzip --archive=//tmp/backup/db.archive",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                }
            };
            process.Start ();
            string output = process.StandardOutput.ReadToEnd ();
            string error = process.StandardError.ReadToEnd ();
            process.WaitForExit ();
            if (string.IsNullOrEmpty (error)) { return output; } else {
                Console.WriteLine (error);
                return error;

以上代码在本地机器上运行良好。 但是当我将此项目更改为 docker 时,mongodump 包无法 included.how 将 mongodump 添加到 docker 文件。``

我的 docker 文件。

FROM microsoft/aspnetcore-build:2.0 AS build-env

WORKDIR /app
RUN apt-get update -y && \ 
    apt-get install -y mongodb-org-tools
# Copy csproj and restore as distinct layers
COPY DJobScheduler/JobScheduler.API/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish DJobScheduler/JobScheduler.API/JobScheduler.API.csproj -c Release -o /app/out

# Build runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "JobScheduler.API.dll"]
Showing error
System.ComponentModel.Win32Exception (0x80004005): No such file or directory
   at System.Diagnostics.Process.ResolvePath(String filename)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()

这些都是运行使用docker-compose.yaml.Also,MongoDBdocker单独运行存储。 MongoDB 在代码中表示为 localhost。

问题是您正在执行多阶段构建并在构建映像而不是运行时映像上安装 mongodump

尝试这样的事情:

FROM mcr.microsoft.com/dotnet/core/sdk:2.2 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY DJobScheduler/JobScheduler.API/*.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish DJobScheduler/JobScheduler.API/JobScheduler.API.csproj -c Release -o /app/out

# Build runtime image
FROM microsoft/aspnetcore:2.0
RUN apt-get update -y && \ 
    apt-get install -y mongodb-clients
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "JobScheduler.API.dll"]

另请注意,我将 mongodump 的安装命令更改为 apt-get install -y mongodb-clients,因为运行时映像基于 debian 并且具有不同的包名称。