Hello guys, I’m here because I have a personal project in hand, which revolves around docker-desktop.
I am developing in C#/Winform for .net8.0 a user interface that allows for the automated deployment of a LAMP server system.
It is something similar to what configuration scripting would be but at the docker level.
The complexity that I am facing right now is that to correctly deploy and/or the characteristics that I need from the lamp server system, I need to activate/modify some settings in docker desktop (I don’t know if it can be done through direct commands to the Docker engine) to achieve The task I am working with is first:
C:\Users<USERNAME>\AppData\Roaming\Docker\setting.json
I programmatically alter the json making these changes:
Dictionary<string, string> settingsToReplace = new()
{
{ "\"exposeDockerAPIOnTCP2375\": false,", "\"exposeDockerAPIOnTCP2375\": true," },
{ "\"updateHostsFile\": false,", "\"updateHostsFile\": true," },
{ "\"displayedOnboarding\": false,", "\"displayedOnboarding\": true," },
{ "\"licenseTermsVersion\": 0,", "\"licenseTermsVersion\": 2," },
{ "\"noWindowsContainers\": true,", "\"noWindowsContainers\": false," },
{ "\"runWinServiceInWslMode\": false,", "\"runWinServiceInWslMode\": true," }
};
foreach (var setting in settingsToReplace)
{
if (jsonContent.Contains(setting.Key))
{
jsonContent = jsonContent.Replace(setting.Key, setting.Value); // with this I exchange the json values
update = true;
}
}
So far everything is going well… the first real problem is restarting the docker-desktop environment and all its services or those that are necessary for it to recognize the new settings.
I have tried to run the following commands:
string dockerDesktopPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Docker\Docker\Docker Desktop.exe");
await run.RunCommand("Stop-Process -Name \\\"Docker Desktop\\\"", "powershell",true);
await run.RunCommand("Stop-Service -Name \\\"com.docker.service\\\"", "powershell",true);
await run.RunCommand("Start-Service -Name \\\"com.docker.service\\\"", "powershell",true);
await run.RunCommand("Start-Process -FilePath \\\"" + dockerDesktopPath + "\\\"", "powershell",true);
from the C# application and tried to execute the following commands, calling them through a PowerShell instance:
public async Task RunCommand(string script, string command = "powershell", Boolean show = false, string? workDirectory = null)
{
string arguments = $"-Command \"Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force ; {script}";
ProcessStartInfo processStartInfo = new(command)
{
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
Verb = "runas",
CreateNoWindow = true
};
Process process = new() { StartInfo = processStartInfo };
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
//write on console for tracking
}
};
process.ErrorDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
//write on console for tracking
}
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync();
}
The problem I am having is that when I execute the commands, and open docker-desktop, it becomes completely unstable, to the point that the docker-desktop graphical interface crashes.