Skip to content

Commit f7593aa

Browse files
updated docker-java to Docker Client API v1.8, Docker Server version 0.7.6
Merged multiple contributions from other forks
1 parent 4093458 commit f7593aa

16 files changed

Lines changed: 704 additions & 280 deletions

File tree

README.md

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Java API client for [Docker](http://docs.docker.io/ "Docker")
44

5-
Supports Docker Client API v1.3, Docker Server version 0.6.1
5+
Supports a subset of the Docker Client API v1.8, Docker Server version 0.7.6
66

77
## Build with Maven
88

@@ -12,52 +12,53 @@ Supports Docker Client API v1.3, Docker Server version 0.6.1
1212
* Maven 3.0.5
1313
* Docker daemon running
1414

15+
Maven will run tests during build process. Tests are using localhost instance of Docker, make sure that
16+
you have Docker running for tests to work or just turn off tests.
1517

16-
By default maven will run tests during build process. Tests are using localhost instance of Docker, make sure that
17-
you have Docker running, or the tests.
18+
If you don't have Docker running locally, you can skip tests with -DskipTests flag set to true:
19+
20+
$ mvn clean install -DskipTests=true
1821

19-
*Since version 0.6, Docker is using unix socket for communication, however java client works over TCP/IP, so you need to
20-
make sure that your Docker server is listening on TCP/IP port.*
2122

22-
Run docker:
23+
By default Docker server is using UNIX sockets for communication with the Docker client, however docker-java
24+
client uses TCP/IP to connect to the Docker server, so you will need to make sure that your Docker server is
25+
listening on TCP port. To allow Docker server to use TCP add the following line to /etc/default/docker
2326

24-
$ sudo docker -H=tcp://127.0.0.1:4243 -d
27+
DOCKER_OPTS="-H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock"
2528

26-
Make sure that docker is up:
29+
More details setting up docket server can be found in official documentation: http://docs.docker.io/en/latest/use/basics/
30+
31+
Now make sure that docker is up:
2732

28-
$docker -H=tcp://127.0.0.1:4243 version
29-
Client version: 0.6.1
30-
Server version: 0.6.1
31-
Git commit: 5105263
32-
Go version: go1.1.2
33-
Last stable version: 0.6.1
33+
$ docker -H tcp://127.0.0.1:4243 version
34+
35+
Client version: 0.7.6
36+
Go version (client): go1.2
37+
Git commit (client): bc3b2ec
38+
Server version: 0.7.6
39+
Git commit (server): bc3b2ec
40+
Go version (server): go1.2
41+
Last stable version: 0.7.6
3442

3543
Run build with tests:
3644

3745
$ mvn clean install
3846

39-
If you don't have Docker running localy, you can skip tests with -DskipTests flag set to true:
40-
41-
$ mvn clean install -DskipTests=true
42-
4347
## Docker Java Client usage:
4448

45-
To use Java Docker client, include dependency into your pom.xml:
49+
docker-java is vailable though Maven Central, so you can include the following dependency into your pom.xml:
4650

4751
<dependency>
4852
<groupId>com.kpelykh</groupId>
4953
<artifactId>docker-java</artifactId>
50-
<version>0.6.1-SNAPSHOT</version>
54+
<version>0.7.6</version>
5155
</dependency>
5256

53-
*Currently Docker Java client is not available in Maven Central, so you will need to install it to a local
54-
repository, before you can use it in your projects.*
55-
57+
5658
## Example code snippets:
5759

5860
DockerClient dockerClient = new DockerClient("http://localhost:4243");
5961

60-
6162
###### Get Docker info:
6263

6364
Info info = dockerClient.info();
@@ -81,7 +82,11 @@ repository, before you can use it in your projects.*
8182

8283
dockerClient.stopContainer(container.id);
8384

84-
85+
86+
##### Support for UNIX sockets:
87+
88+
Support for UNIX socket should appear in docker-java pretty soon. I'm working on its integration.
89+
8590
##### Docker Builder:
8691

8792
To use Docker Builder, as described on page http://docs.docker.io/en/latest/use/builder/,

pom.xml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<groupId>com.kpelykh</groupId>
1212
<artifactId>docker-java</artifactId>
1313
<packaging>jar</packaging>
14-
<version>0.6.1-SNAPSHOT</version>
14+
<version>0.7.6-SNAPSHOT</version>
1515

1616
<name>docker-java</name>
1717
<url>https://github.com/kpelykh/docker-java</url>
@@ -48,7 +48,7 @@
4848

4949
<version.slf4j>1.6.1</version.slf4j>
5050

51-
<jersey.version>1.17.1</jersey.version>
51+
<jersey.version>1.18</jersey.version>
5252
<jersey-apache-client4.version>1.9</jersey-apache-client4.version>
5353

5454
<httpclient.version>4.2.5</httpclient.version>
@@ -136,15 +136,6 @@
136136
<version>${slf4j-api.version}</version>
137137
</dependency>
138138

139-
<dependency>
140-
<!-- needed only for annotations -->
141-
<groupId>com.google.code.findbugs</groupId>
142-
<artifactId>jsr305</artifactId>
143-
<optional>true</optional>
144-
<version>${jsr305.version}</version>
145-
</dependency>
146-
147-
148139
<!-- /// Test /////////////////////////// -->
149140
<dependency>
150141
<groupId>ch.qos.logback</groupId>

src/main/java/com/google/common/base/Preconditions.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.common.base;
1818

1919

20-
import javax.annotation.Nullable;
2120
import java.util.NoSuchElementException;
2221

2322
/**
@@ -83,7 +82,7 @@ public static void checkArgument(boolean expression) {
8382
* @throws IllegalArgumentException if {@code expression} is false
8483
*/
8584
public static void checkArgument(
86-
boolean expression, @Nullable Object errorMessage) {
85+
boolean expression, Object errorMessage) {
8786
if (!expression) {
8887
throw new IllegalArgumentException(String.valueOf(errorMessage));
8988
}
@@ -109,8 +108,8 @@ public static void checkArgument(
109108
* this happen)
110109
*/
111110
public static void checkArgument(boolean expression,
112-
@Nullable String errorMessageTemplate,
113-
@Nullable Object... errorMessageArgs) {
111+
String errorMessageTemplate,
112+
Object... errorMessageArgs) {
114113
if (!expression) {
115114
throw new IllegalArgumentException(
116115
format(errorMessageTemplate, errorMessageArgs));
@@ -140,7 +139,7 @@ public static void checkState(boolean expression) {
140139
* @throws IllegalStateException if {@code expression} is false
141140
*/
142141
public static void checkState(
143-
boolean expression, @Nullable Object errorMessage) {
142+
boolean expression, Object errorMessage) {
144143
if (!expression) {
145144
throw new IllegalStateException(String.valueOf(errorMessage));
146145
}
@@ -166,8 +165,8 @@ public static void checkState(
166165
* this happen)
167166
*/
168167
public static void checkState(boolean expression,
169-
@Nullable String errorMessageTemplate,
170-
@Nullable Object... errorMessageArgs) {
168+
String errorMessageTemplate,
169+
Object... errorMessageArgs) {
171170
if (!expression) {
172171
throw new IllegalStateException(
173172
format(errorMessageTemplate, errorMessageArgs));
@@ -199,7 +198,7 @@ public static <T> T checkNotNull(T reference) {
199198
* @return the non-null reference that was validated
200199
* @throws NullPointerException if {@code reference} is null
201200
*/
202-
public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
201+
public static <T> T checkNotNull(T reference, Object errorMessage) {
203202
if (reference == null) {
204203
throw new NullPointerException(String.valueOf(errorMessage));
205204
}
@@ -224,8 +223,8 @@ public static <T> T checkNotNull(T reference, @Nullable Object errorMessage) {
224223
* @throws NullPointerException if {@code reference} is null
225224
*/
226225
public static <T> T checkNotNull(T reference,
227-
@Nullable String errorMessageTemplate,
228-
@Nullable Object... errorMessageArgs) {
226+
String errorMessageTemplate,
227+
Object... errorMessageArgs) {
229228
if (reference == null) {
230229
// If either of these parameters is null, the right thing happens anyway
231230
throw new NullPointerException(
@@ -295,7 +294,7 @@ public static int checkElementIndex(int index, int size) {
295294
* @throws IllegalArgumentException if {@code size} is negative
296295
*/
297296
public static int checkElementIndex(
298-
int index, int size, @Nullable String desc) {
297+
int index, int size, String desc) {
299298
// Carefully optimized for execution by hotspot (explanatory comment above)
300299
if (index < 0 || index >= size) {
301300
throw new IndexOutOfBoundsException(badElementIndex(index, size, desc));
@@ -345,7 +344,7 @@ public static int checkPositionIndex(int index, int size) {
345344
* @throws IllegalArgumentException if {@code size} is negative
346345
*/
347346
public static int checkPositionIndex(
348-
int index, int size, @Nullable String desc) {
347+
int index, int size, String desc) {
349348
// Carefully optimized for execution by hotspot (explanatory comment above)
350349
if (index < 0 || index > size) {
351350
throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
@@ -409,7 +408,7 @@ private static String badPositionIndexes(int start, int end, int size) {
409408
* template. Arguments are converted to strings using
410409
* {@link String#valueOf(Object)}. Arguments can be null.
411410
*/
412-
static String format(String template, @Nullable Object... args) {
411+
static String format(String template, Object... args) {
413412
template = String.valueOf(template); // null -> "null"
414413

415414
// start substituting the arguments into the '%s' placeholders

src/main/java/com/kpelykh/docker/client/DockerClient.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class DockerClient
4848
private String restEndpointUrl;
4949

5050
public DockerClient(String serverUrl) {
51-
restEndpointUrl = serverUrl + "/v1.3";
51+
restEndpointUrl = serverUrl + "/v1.8";
5252
ClientConfig clientConfig = new DefaultClientConfig();
5353
clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
5454

@@ -61,9 +61,11 @@ public DockerClient(String serverUrl) {
6161
// Increase default max connection per route
6262
cm.setDefaultMaxPerRoute(1000);
6363

64-
// HttpClient httpClient = new DefaultHttpClient(cm);
65-
//client = new ApacheHttpClient4(new ApacheHttpClient4Handler(httpClient, null, false), clientConfig);
66-
client = new UnixSocketClient(clientConfig);
64+
HttpClient httpClient = new DefaultHttpClient(cm);
65+
client = new ApacheHttpClient4(new ApacheHttpClient4Handler(httpClient, null, false), clientConfig);
66+
67+
//Experimental support for unix sockets:
68+
//client = new UnixSocketClient(clientConfig);
6769

6870
client.addFilter(new JsonClientFilter());
6971
client.addFilter(new LoggingFilter());
@@ -316,21 +318,19 @@ public ContainerCreateResponse createContainer(ContainerConfig config) throws Do
316318
return createContainer(config, null);
317319
}
318320

319-
public ContainerCreateResponse createContainer(ContainerConfig config, String name) throws DockerException {
320-
321-
String url = restEndpointUrl + "/containers/create";
321+
public ContainerCreateResponse createContainer(ContainerConfig config,String name) throws DockerException {
322322

323-
if (!StringUtils.isEmpty(name)){
324-
url = url + "?name=" + name;
323+
MultivaluedMap<String,String> params = new MultivaluedMapImpl();
324+
if(name != null){
325+
params.add("name", name);
325326
}
326-
327-
WebResource webResource = client.resource(url);
327+
WebResource webResource = client.resource(restEndpointUrl + "/containers/create").queryParams(params);
328328

329329
try {
330-
LOGGER.trace("POST: {}", webResource);
330+
LOGGER.trace("POST: {} ", webResource);
331331
return webResource.accept(MediaType.APPLICATION_JSON)
332-
.type(MediaType.APPLICATION_JSON)
333-
.post(ContainerCreateResponse.class, config);
332+
.type(MediaType.APPLICATION_JSON)
333+
.post(ContainerCreateResponse.class, config);
334334
} catch (UniformInterfaceException exception) {
335335
if (exception.getResponse().getStatus() == 404) {
336336
throw new DockerException(String.format("%s is an unrecognized image. Please pull the image first.", config.getImage()));
@@ -643,20 +643,20 @@ public ClientResponse build(File dockerFolder, String tag) throws DockerExceptio
643643
FileUtils.copyFileToDirectory(dockerFile, tmpDockerContextFolder);
644644

645645
for (String cmd : dockerFileContent) {
646-
if (StringUtils.startsWithIgnoreCase(cmd.trim(), "ADD")) {
646+
if (StringUtils.startsWith(cmd.trim(), "ADD")) {
647647
String addArgs[] = StringUtils.split(cmd, " \t");
648648
if (addArgs.length != 3) {
649649
throw new DockerException(String.format("Wrong format on line [%s]", cmd));
650650
}
651651

652652
File src = new File(addArgs[1]);
653-
if (!src.isAbsolute()) {
654-
src = new File(dockerFolder, addArgs[1]).getCanonicalFile();
653+
/* if (!src.isAbsolute()) {
654+
src = new File(dockerFolder, addArgs[1]).getCanonicalFile();
655655
}
656656
657657
if (!src.exists()) {
658-
throw new DockerException(String.format("Sorce file %s doesnt' exist", src));
659-
}
658+
throw new DockerException(String.format("Sorce file %s doesnt' exist", src));
659+
}*/
660660
if (src.isDirectory()) {
661661
FileUtils.copyDirectory(src, tmpDockerContextFolder);
662662
} else {

src/main/java/com/kpelykh/docker/client/UnixSocketClientHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.HttpEntity;
1111
import org.apache.http.HttpException;
1212
import org.apache.http.HttpResponse;
13+
import org.apache.http.annotation.NotThreadSafe;
1314
import org.apache.http.client.methods.*;
1415
import org.apache.http.entity.AbstractHttpEntity;
1516
import org.apache.http.entity.BufferedHttpEntity;
@@ -22,7 +23,6 @@
2223
import org.slf4j.Logger;
2324
import org.slf4j.LoggerFactory;
2425

25-
import javax.annotation.concurrent.NotThreadSafe;
2626
import java.io.*;
2727
import java.net.URI;
2828
import java.nio.channels.Channels;

src/main/java/com/kpelykh/docker/client/model/CommitConfig.java

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -82,55 +82,4 @@ public CommitConfig(String container) {
8282
this.container = container;
8383
}
8484

85-
private CommitConfig(Builder b) {
86-
this.container = b.container;
87-
this.repo = b.repo;
88-
this.tag = b.tag;
89-
this.message = b.message;
90-
this.author = b.author;
91-
this.run = b.run;
92-
}
93-
94-
public static class Builder implements IBuilder<CommitConfig> {
95-
private String container;
96-
private String repo;
97-
private String tag;
98-
private String message;
99-
private String author;
100-
private String run;
101-
102-
public Builder(String containerId) {
103-
this.container = containerId;
104-
}
105-
106-
public Builder repo(String repo) {
107-
this.repo = repo;
108-
return this;
109-
}
110-
111-
public Builder tag(String tag) {
112-
this.tag = tag;
113-
return this;
114-
}
115-
116-
public Builder message(String message) {
117-
this.message = message;
118-
return this;
119-
}
120-
121-
public Builder author(String author) {
122-
this.author = author;
123-
return this;
124-
}
125-
126-
public Builder run(String run) {
127-
this.run = run;
128-
return this;
129-
}
130-
131-
public CommitConfig build() {
132-
return new CommitConfig(this);
133-
}
134-
}
135-
13685
}

0 commit comments

Comments
 (0)