Running an executable in a C# dockerfile

2

I am new to Docker. I created a c# Project which runs mongodump using C# process

   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;

Above code work fine in local machine. but when i change this projet to docker the mongodump package can't be included.how to add mongodump to docker file.``

My docker file.

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()

All these are running using docker-compose.yaml.Also, MongoDB docker is separately running for storage. That MongoDB is represented as localhost in code.

c#
mongodb
docker
mongodump
asked on Stack Overflow Sep 14, 2019 by arun thomas • edited Sep 14, 2019 by arun thomas

1 Answer

2

The problem is you're doing a multi-stage build and installing mongodump on the build image instead of the runtime image.

Try something like this:

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"]

Also notice I changed the install command for mongodump to apt-get install -y mongodb-clients, since the runtime image is based on debian and has a different package name.

answered on Stack Overflow Sep 14, 2019 by erikbozic • edited Sep 16, 2019 by erikbozic

User contributions licensed under CC BY-SA 3.0