Skip to content

Commit 5de503e

Browse files
committed
Add cross-platform Docker client and providers
1 parent 615e754 commit 5de503e

File tree

74 files changed

+9748
-462
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+9748
-462
lines changed

.idea/workspace.xml

Lines changed: 23 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ repositories {
1919
}
2020

2121
dependencies {
22-
implementation("com.github.docker-java:docker-java:3.5.1") {
23-
exclude group: "com.github.docker-java", module: "docker-java-transport-jersey"
24-
}
25-
implementation "com.github.docker-java:docker-java-transport-httpclient5:3.5.1"
2622
implementation "com.google.code.gson:gson:2.10.1"
2723
implementation "org.apache.commons:commons-compress:1.24.0"
24+
implementation "com.kohlschutter.junixsocket:junixsocket-core:2.9.0"
2825
implementation "org.slf4j:slf4j-simple:2.0.7"
2926
testImplementation "org.junit.jupiter:junit-jupiter:5.10.1"
3027
}
3128

3229
test {
3330
useJUnitPlatform()
34-
}
31+
}
32+
33+
java {
34+
sourceCompatibility = JavaVersion.VERSION_11
35+
targetCompatibility = JavaVersion.VERSION_11
36+
}
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
package io.github.intisy.docker;
2+
3+
import io.github.intisy.docker.command.container.*;
4+
import io.github.intisy.docker.command.image.*;
5+
import io.github.intisy.docker.command.network.*;
6+
import io.github.intisy.docker.command.system.*;
7+
import io.github.intisy.docker.command.volume.*;
8+
import io.github.intisy.docker.transport.DockerHttpClient;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import java.io.Closeable;
13+
import java.io.IOException;
14+
15+
/**
16+
* Docker client for communicating with the Docker daemon.
17+
* <p>
18+
* Example usage:
19+
* <pre>{@code
20+
* DockerClient client = DockerClient.builder()
21+
* .withHost("unix:///var/run/docker.sock")
22+
* .build();
23+
*
24+
* // List containers
25+
* List<Container> containers = client.listContainers().withShowAll(true).exec();
26+
*
27+
* // Pull and run a container
28+
* client.pullImage("nginx:alpine").exec(5, TimeUnit.MINUTES);
29+
* CreateContainerResponse response = client.createContainer("nginx:alpine")
30+
* .withName("my-nginx")
31+
* .exec();
32+
* client.startContainer(response.getId()).exec();
33+
*
34+
* // Clean up
35+
* client.stopContainer(response.getId()).exec();
36+
* client.removeContainer(response.getId()).exec();
37+
* }</pre>
38+
*
39+
* @author Finn Birich
40+
*/
41+
public class DockerClient implements Closeable {
42+
private static final Logger log = LoggerFactory.getLogger(DockerClient.class);
43+
44+
private final DockerHttpClient httpClient;
45+
46+
private DockerClient(DockerHttpClient httpClient) {
47+
this.httpClient = httpClient;
48+
}
49+
50+
public static Builder builder() {
51+
return new Builder();
52+
}
53+
54+
public static DockerClient createDefault() {
55+
String os = System.getProperty("os.name").toLowerCase();
56+
if (os.contains("win")) {
57+
return builder().withHost("npipe:////./pipe/docker_engine").build();
58+
} else {
59+
return builder().withHost("unix:///var/run/docker.sock").build();
60+
}
61+
}
62+
63+
// ==================== Container Commands ====================
64+
65+
public ListContainersCmd listContainers() {
66+
return new ListContainersCmd(httpClient);
67+
}
68+
69+
public CreateContainerCmd createContainer(String image) {
70+
return new CreateContainerCmd(httpClient, image);
71+
}
72+
73+
public StartContainerCmd startContainer(String containerId) {
74+
return new StartContainerCmd(httpClient, containerId);
75+
}
76+
77+
public StopContainerCmd stopContainer(String containerId) {
78+
return new StopContainerCmd(httpClient, containerId);
79+
}
80+
81+
public RestartContainerCmd restartContainer(String containerId) {
82+
return new RestartContainerCmd(httpClient, containerId);
83+
}
84+
85+
public KillContainerCmd killContainer(String containerId) {
86+
return new KillContainerCmd(httpClient, containerId);
87+
}
88+
89+
public PauseContainerCmd pauseContainer(String containerId) {
90+
return new PauseContainerCmd(httpClient, containerId);
91+
}
92+
93+
public UnpauseContainerCmd unpauseContainer(String containerId) {
94+
return new UnpauseContainerCmd(httpClient, containerId);
95+
}
96+
97+
public RemoveContainerCmd removeContainer(String containerId) {
98+
return new RemoveContainerCmd(httpClient, containerId);
99+
}
100+
101+
public InspectContainerCmd inspectContainer(String containerId) {
102+
return new InspectContainerCmd(httpClient, containerId);
103+
}
104+
105+
public LogsContainerCmd logs(String containerId) {
106+
return new LogsContainerCmd(httpClient, containerId);
107+
}
108+
109+
public WaitContainerCmd waitContainer(String containerId) {
110+
return new WaitContainerCmd(httpClient, containerId);
111+
}
112+
113+
public ExecCreateCmd execCreate(String containerId) {
114+
return new ExecCreateCmd(httpClient, containerId);
115+
}
116+
117+
public ExecStartCmd execStart(String execId) {
118+
return new ExecStartCmd(httpClient, execId);
119+
}
120+
121+
public ExecInspectCmd execInspect(String execId) {
122+
return new ExecInspectCmd(httpClient, execId);
123+
}
124+
125+
// ==================== Image Commands ====================
126+
127+
public ListImagesCmd listImages() {
128+
return new ListImagesCmd(httpClient);
129+
}
130+
131+
public PullImageCmd pullImage(String image) {
132+
return new PullImageCmd(httpClient, image);
133+
}
134+
135+
public RemoveImageCmd removeImage(String imageId) {
136+
return new RemoveImageCmd(httpClient, imageId);
137+
}
138+
139+
public InspectImageCmd inspectImage(String imageId) {
140+
return new InspectImageCmd(httpClient, imageId);
141+
}
142+
143+
public TagImageCmd tagImage(String imageId) {
144+
return new TagImageCmd(httpClient, imageId);
145+
}
146+
147+
public BuildImageCmd buildImage() {
148+
return new BuildImageCmd(httpClient);
149+
}
150+
151+
// ==================== Volume Commands ====================
152+
153+
public ListVolumesCmd listVolumes() {
154+
return new ListVolumesCmd(httpClient);
155+
}
156+
157+
public CreateVolumeCmd createVolume() {
158+
return new CreateVolumeCmd(httpClient);
159+
}
160+
161+
public RemoveVolumeCmd removeVolume(String volumeName) {
162+
return new RemoveVolumeCmd(httpClient, volumeName);
163+
}
164+
165+
public InspectVolumeCmd inspectVolume(String volumeName) {
166+
return new InspectVolumeCmd(httpClient, volumeName);
167+
}
168+
169+
// ==================== Network Commands ====================
170+
171+
public ListNetworksCmd listNetworks() {
172+
return new ListNetworksCmd(httpClient);
173+
}
174+
175+
public CreateNetworkCmd createNetwork() {
176+
return new CreateNetworkCmd(httpClient);
177+
}
178+
179+
public RemoveNetworkCmd removeNetwork(String networkId) {
180+
return new RemoveNetworkCmd(httpClient, networkId);
181+
}
182+
183+
public InspectNetworkCmd inspectNetwork(String networkId) {
184+
return new InspectNetworkCmd(httpClient, networkId);
185+
}
186+
187+
public ConnectNetworkCmd connectNetwork(String networkId) {
188+
return new ConnectNetworkCmd(httpClient, networkId);
189+
}
190+
191+
public DisconnectNetworkCmd disconnectNetwork(String networkId) {
192+
return new DisconnectNetworkCmd(httpClient, networkId);
193+
}
194+
195+
// ==================== System Commands ====================
196+
197+
public PingCmd ping() {
198+
return new PingCmd(httpClient);
199+
}
200+
201+
public InfoCmd info() {
202+
return new InfoCmd(httpClient);
203+
}
204+
205+
public VersionCmd version() {
206+
return new VersionCmd(httpClient);
207+
}
208+
209+
@Override
210+
public void close() throws IOException {
211+
if (httpClient != null) {
212+
httpClient.close();
213+
}
214+
}
215+
216+
public static class Builder {
217+
private String dockerHost;
218+
private int timeout = 30000;
219+
220+
private Builder() {
221+
}
222+
223+
public Builder withHost(String dockerHost) {
224+
this.dockerHost = dockerHost;
225+
return this;
226+
}
227+
228+
public Builder withTimeout(int timeoutMs) {
229+
this.timeout = timeoutMs;
230+
return this;
231+
}
232+
233+
public DockerClient build() {
234+
if (dockerHost == null) {
235+
String os = System.getProperty("os.name").toLowerCase();
236+
if (os.contains("win")) {
237+
dockerHost = "npipe:////./pipe/docker_engine";
238+
} else {
239+
dockerHost = "unix:///var/run/docker.sock";
240+
}
241+
}
242+
log.debug("Building DockerClient for host: {}", dockerHost);
243+
DockerHttpClient httpClient = new DockerHttpClient(dockerHost, timeout);
244+
return new DockerClient(httpClient);
245+
}
246+
}
247+
}

0 commit comments

Comments
 (0)