Gabriel
Gabriel

Reputation: 61

Start.Process() .net Core doesn't work in Linux Docker

I have created a docker container with a ASP.net Core Service. This service, however, should start another service inside the docker container. The problem I am facing right now is that Process.Start() doesn't seem to work in this Linux container.

This is what I tried so far:

            Process process;
            ProcessStartInfo processInfo;

            string fileName = "bin/bash dotnet run /root/SubService/SubService.API.dll";
            string arguments = $"Company --urls http://localhost:5000/SolutionName";
            string escapedArguments  = $"-c /{arguments}/";

            processInfo = new ProcessStartInfo(escapedFilename, escapedArguments)
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardError = false,
                RedirectStandardOutput = false
            };

            try
            {
                process = Process.Start(processInfo);
                process.WaitForExit();
                process.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

When Process.Start(processInfo) is called I get the exception "No such file or directory". The path, however, is correct. What could be the problem? Or is there an alternative way to start a process from a asp.net core service.

Thank you.

Upvotes: 3

Views: 2401

Answers (1)

Gabriel
Gabriel

Reputation: 61

After some more testing, the problem seemed to be the incorrect use of the Process.Start() method. This time arguments were used.

This is how it worked:

            Process process;
            string outputStream;
            string errorStream;

            try
            {
                string fileName = "dotnet";
                string arguments = $"{subServiceData.ApplicationPath} {subServiceData.SolutionName} --urls { subServiceData.SubServiceUrl}";
                process = Process.Start(fileName, arguments);
                process.WaitForExit();

                // *** Read the streams ***
                outputStream = process.StandardOutput.ReadToEnd();
                errorStream = process.StandardError.ReadToEnd();

                int exitCode = process.ExitCode;
                process.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

Upvotes: 3

Related Questions