我是 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 包。如何将 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 运行。此外,MongoDB docker 单独运行用于存储。 MongoDB 在代码中表示为本地主机。
最佳答案
问题是您正在执行多阶段构建并在构建镜像而不是运行时镜像上安装 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 并且具有不同的包名称。
关于c# - 在 C# dockerfile 中运行可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933340/