-
Notifications
You must be signed in to change notification settings - Fork 1
/
DockerProcessEngine.cs
104 lines (92 loc) · 2.59 KB
/
DockerProcessEngine.cs
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
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using Docker.DotNet;
using Docker.DotNet.Models;
namespace Camunda.Test.Example.ProcessEngine
{
public class DockerProcessEngine
{
private DockerClient DockerClient;
private string ContainerId;
public DockerProcessEngine()
{
DockerClient = BuildDockerClient();
}
public void Start()
{
Console.WriteLine($"Building Camunda docker container");
var progress = new Progress<JSONMessage>();
DockerClient.Images.CreateImageAsync(
new ImagesCreateParameters
{
FromImage = "camunda/camunda-bpm-platform:",
Tag = "run-7.13.0",
}, null, progress).Wait();
ContainerId = DockerClient.Containers.CreateContainerAsync(new CreateContainerParameters()
{
Name = "camunda-unit-test",
Image = "camunda/camunda-bpm-platform:run-7.13.0",
ExposedPorts = new Dictionary<string, EmptyStruct>
{
{
"8080", default(EmptyStruct)
},
{
"8000", default(EmptyStruct)
},
{
"9404", default(EmptyStruct)
}
},
HostConfig = new HostConfig
{
PortBindings = new Dictionary<string, IList<PortBinding>>
{
{"8080", new List<PortBinding> {new PortBinding {HostPort = "8090"}}},
{"8000", new List<PortBinding> {new PortBinding {HostPort = "8000"}}},
{"9404", new List<PortBinding> {new PortBinding {HostPort = "9404"}}}
},
PublishAllPorts = true
}
}).Result.ID;
DockerClient.Containers.StartContainerAsync(ContainerId, null).Wait();
var started = false;
while (!started)
{
Thread.Sleep(5000);
Stream result = DockerClient.Containers.GetContainerLogsAsync(ContainerId, new ContainerLogsParameters()
{
ShowStdout = true
}).Result;
StreamReader reader = new StreamReader(result);
started = reader.ReadToEnd().Contains("starting to acquire jobs");
}
}
public void Stop()
{
Console.WriteLine($"Stopping Camunda docker container");
DockerClient.Containers.RemoveContainerAsync(ContainerId, new ContainerRemoveParameters()
{
RemoveVolumes = true,
Force = true
}).Wait();
}
private DockerClient BuildDockerClient()
{
string url;
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
{
url = "npipe://./pipe/docker_engine";
}
else
{
url = "unix:///var/run/docker.sock";
}
return new DockerClientConfiguration(new Uri(url)).CreateClient();
}
}
}