All checks were successful
Caddy Manager CI build / docker (push) Successful in 1m16s
48 lines
1.6 KiB
C#
48 lines
1.6 KiB
C#
using Docker.DotNet;
|
|
using Docker.DotNet.Models;
|
|
using CaddyManager.Contracts.Configurations.Docker;
|
|
using CaddyManager.Contracts.Configurations;
|
|
using CaddyManager.Contracts.Docker;
|
|
|
|
namespace CaddyManager.Services.Docker;
|
|
|
|
/// <inheritdoc />
|
|
public class DockerService(IConfigurationsService configurationsService) : IDockerService
|
|
{
|
|
private DockerServiceConfiguration Configuration => configurationsService.Get<DockerServiceConfiguration>();
|
|
|
|
/// <summary>
|
|
/// Method to get the container id of the Caddy container by the name configured
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<string> GetCaddyContainerId()
|
|
{
|
|
var client = new DockerClientConfiguration(new Uri(Configuration.DockerHostWithEnvCheck)).CreateClient();
|
|
|
|
if (client == null) return string.Empty;
|
|
|
|
var containers = await client.Containers.ListContainersAsync(new ContainersListParameters
|
|
{
|
|
All = true
|
|
});
|
|
|
|
return containers.FirstOrDefault(container => container.Names.Contains($"/{Configuration.CaddyContainerName}"))
|
|
?.ID ?? string.Empty;
|
|
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task RestartCaddyContainerAsync()
|
|
{
|
|
var containerId = await GetCaddyContainerId();
|
|
|
|
if (string.IsNullOrEmpty(containerId)) return;
|
|
|
|
var client = new DockerClientConfiguration(new Uri(Configuration.DockerHostWithEnvCheck)).CreateClient();
|
|
|
|
if (client != null)
|
|
{
|
|
await client.Containers.RestartContainerAsync(containerId, new ContainerRestartParameters());
|
|
}
|
|
}
|
|
} |