Using C# to check local Docker container status
Posted: (EET/GMT+2)
Docker is great for running containerized applications, but sometimes, you might need to connect the status information to something larger. So, if you are running containers locally or in a small-scale environment, a small C# console application can be enough to check basic Docker status.
Note that this is not a replacement for production monitoring. It is a quick developer tool for local machines, build agents, demos, or lab environments.
The simplest approach is to call the Docker CLI and read the output. Along these lines:
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "docker",
Arguments = "ps --format \"{{.Names}} {{.Status}}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
using Process process = Process.Start(startInfo)!;
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine(error);
return;
}
Console.WriteLine(output);
This prints the running containers and their current status.
sql-server Up 2 hours redis Up 2 hours web-api Up 10 minutes
Code like this is useful when you need a simple health check before running integration tests or starting a local development environment.
You can also include stopped containers by changing the Docker command by adding -a:
docker ps -a --format "{{.Names}} {{.Status}}"
And the matching C# argument value:
Arguments = "ps -a --format \"{{.Names}} {{.Status}}\""
Tip: keep this kind of helper small. If your tool grows, move the Docker command execution behind a separate service class so the rest of the application does not depend directly on ProcessStartInfo.
Also remember that this depends on Docker being installed and available in the current PATH.
Happy containerization!