This repository was archived by the owner on May 4, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (83 loc) · 3.3 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (83 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Docker.DotNet;
using Docker.DotNet.Models;
var client = new DockerClientConfiguration().CreateClient();
var filters = new Dictionary<string, IDictionary<string, bool>>
{
["label"] = new Dictionary<string, bool>
{
{ "classinsights.update", true }
}
};
var containers = await client.Containers.ListContainersAsync(new ContainersListParameters
{
Filters = filters,
All = true
});
foreach (var container in containers)
{
try
{
Console.WriteLine($"Updating {container.ID}");
// pull the latest image
await client.Images.CreateImageAsync(new ImagesCreateParameters
{
FromImage = container.Image,
Tag = "latest"
}, new AuthConfig(), new Progress<JSONMessage>(msg => Console.WriteLine(msg.Status)));
// prevent termination of own container
if (container.ID.StartsWith(Environment.MachineName))
continue;
// inspect old container
var inspect = await client.Containers.InspectContainerAsync(container.ID);
// stop and remove the old container
await client.Containers.StopContainerAsync(container.ID, new ContainerStopParameters
{
WaitBeforeKillSeconds = 10
});
await client.Containers.RemoveContainerAsync(container.ID, new ContainerRemoveParameters
{
Force = true,
RemoveVolumes = true
});
// recreate with the same settings
var created = await client.Containers.CreateContainerAsync(new CreateContainerParameters
{
Name = inspect.Name,
Image = inspect.Config.Image,
Env = inspect.Config.Env,
ExposedPorts = inspect.Config.ExposedPorts,
HostConfig = inspect.HostConfig,
Cmd = inspect.Config.Cmd,
Volumes = inspect.Config.Volumes,
NetworkingConfig = new NetworkingConfig { EndpointsConfig = inspect.NetworkSettings.Networks },
Labels = inspect.Config.Labels,
WorkingDir = inspect.Config.WorkingDir,
Hostname = inspect.Config.Hostname,
Domainname = inspect.Config.Domainname,
User = inspect.Config.User,
AttachStdin = inspect.Config.AttachStdin,
AttachStdout = inspect.Config.AttachStdout,
AttachStderr = inspect.Config.AttachStderr,
Tty = inspect.Config.Tty,
OpenStdin = inspect.Config.OpenStdin,
StdinOnce = inspect.Config.StdinOnce,
Entrypoint = inspect.Config.Entrypoint,
MacAddress = inspect.Config.MacAddress,
ArgsEscaped = inspect.Config.ArgsEscaped,
Healthcheck = inspect.Config.Healthcheck,
NetworkDisabled = inspect.Config.NetworkDisabled,
OnBuild = inspect.Config.OnBuild,
StopSignal = inspect.Config.StopSignal,
StopTimeout = inspect.Config.StopTimeout,
Shell = inspect.Config.Shell,
Platform = inspect.Platform
});
// start the new container
await client.Containers.StartContainerAsync(created.ID, new ContainerStartParameters());
Console.WriteLine($"Replaced {container.ID} → {created.ID}");
}
catch (Exception e)
{
Console.WriteLine($"Error updating {container.ID}: {e.Message}");
}
}