Skip to content

Commit d91b3b2

Browse files
authored
Merge branch 'master' into master
2 parents a73b8fe + e0275e0 commit d91b3b2

7 files changed

Lines changed: 165 additions & 11 deletions

File tree

docker-java-core/src/main/java/com/github/dockerjava/core/DefaultDockerClientConfig.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.dockerjava.core.NameParser.HostnameReposName;
77
import com.github.dockerjava.core.NameParser.ReposTag;
88
import org.apache.commons.lang.StringUtils;
9+
import org.apache.commons.lang.SystemUtils;
910
import org.apache.commons.lang.builder.EqualsBuilder;
1011
import org.apache.commons.lang.builder.HashCodeBuilder;
1112
import org.apache.commons.lang.builder.ToStringBuilder;
@@ -58,6 +59,10 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
5859

5960
static final Properties DEFAULT_PROPERTIES = new Properties();
6061

62+
static final String DEFAULT_DOCKER_HOST = "unix:///var/run/docker.sock";
63+
64+
static final String WINDOWS_DEFAULT_DOCKER_HOST = "npipe:////./pipe/docker_engine";
65+
6166
static {
6267
CONFIG_KEYS.add(DOCKER_HOST);
6368
CONFIG_KEYS.add(DOCKER_TLS_VERIFY);
@@ -69,7 +74,6 @@ public class DefaultDockerClientConfig implements Serializable, DockerClientConf
6974
CONFIG_KEYS.add(REGISTRY_EMAIL);
7075
CONFIG_KEYS.add(REGISTRY_URL);
7176

72-
DEFAULT_PROPERTIES.put(DOCKER_HOST, "unix:///var/run/docker.sock");
7377
DEFAULT_PROPERTIES.put(DOCKER_CONFIG, "${user.home}/.docker");
7478
DEFAULT_PROPERTIES.put(REGISTRY_URL, "https://index.docker.io/v1/");
7579
DEFAULT_PROPERTIES.put(REGISTRY_USERNAME, "${user.name}");
@@ -337,8 +341,12 @@ public static class Builder {
337341
* registry.email, DOCKER_CERT_PATH, and DOCKER_CONFIG.
338342
*/
339343
public Builder withProperties(Properties p) {
340-
return withDockerHost(p.getProperty(DOCKER_HOST))
341-
.withDockerTlsVerify(p.getProperty(DOCKER_TLS_VERIFY))
344+
345+
if (p.getProperty(DOCKER_HOST) != null) {
346+
withDockerHost(p.getProperty(DOCKER_HOST));
347+
}
348+
349+
return withDockerTlsVerify(p.getProperty(DOCKER_TLS_VERIFY))
342350
.withDockerConfig(p.getProperty(DOCKER_CONFIG))
343351
.withDockerCertPath(p.getProperty(DOCKER_CERT_PATH))
344352
.withApiVersion(p.getProperty(API_VERSION))
@@ -412,6 +420,10 @@ public final Builder withDockerTlsVerify(Boolean dockerTlsVerify) {
412420
return this;
413421
}
414422

423+
public final boolean isDockerHostSetExplicitly() {
424+
return dockerHost != null;
425+
}
426+
415427
/**
416428
* Overrides the default {@link SSLConfig} that is used when calling {@link Builder#withDockerTlsVerify(java.lang.Boolean)} and
417429
* {@link Builder#withDockerCertPath(String)}. This way it is possible to pass a custom {@link SSLConfig} to the resulting
@@ -435,7 +447,11 @@ public DefaultDockerClientConfig build() {
435447
sslConfig = customSslConfig;
436448
}
437449

438-
return new DefaultDockerClientConfig(dockerHost, dockerConfig, apiVersion, registryUrl, registryUsername,
450+
URI dockerHostUri = dockerHost != null
451+
? dockerHost
452+
: URI.create(SystemUtils.IS_OS_WINDOWS ? WINDOWS_DEFAULT_DOCKER_HOST : DEFAULT_DOCKER_HOST);
453+
454+
return new DefaultDockerClientConfig(dockerHostUri, dockerConfig, apiVersion, registryUrl, registryUsername,
439455
registryPassword, registryEmail, sslConfig);
440456
}
441457

docker-java-transport-httpclient5/src/main/java/com/github/dockerjava/httpclient5/ApacheDockerHttpClientImpl.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.github.dockerjava.httpclient5;
22

33
import com.github.dockerjava.transport.DockerHttpClient;
4-
import com.github.dockerjava.transport.DomainSocket;
54
import com.github.dockerjava.transport.NamedPipeSocket;
65
import com.github.dockerjava.transport.SSLConfig;
6+
import com.github.dockerjava.transport.UnixSocket;
77
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
88
import org.apache.hc.client5.http.config.RequestConfig;
99
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
@@ -24,11 +24,13 @@
2424
import org.apache.hc.core5.http.config.RegistryBuilder;
2525
import org.apache.hc.core5.http.impl.DefaultContentLengthStrategy;
2626
import org.apache.hc.core5.http.impl.io.EmptyInputStream;
27+
import org.apache.hc.core5.http.io.SocketConfig;
2728
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
2829
import org.apache.hc.core5.http.io.entity.InputStreamEntity;
2930
import org.apache.hc.core5.http.protocol.BasicHttpContext;
3031
import org.apache.hc.core5.http.protocol.HttpContext;
3132
import org.apache.hc.core5.net.URIAuthority;
33+
import org.apache.hc.core5.util.Timeout;
3234
import org.slf4j.Logger;
3335
import org.slf4j.LoggerFactory;
3436

@@ -93,6 +95,12 @@ protected ApacheDockerHttpClientImpl(
9395
null
9496
)
9597
);
98+
// See https://github.com/docker-java/docker-java/pull/1590#issuecomment-870581289
99+
connectionManager.setDefaultSocketConfig(
100+
SocketConfig.copy(SocketConfig.DEFAULT)
101+
.setSoTimeout(Timeout.ZERO_MILLISECONDS)
102+
.build()
103+
);
96104
connectionManager.setMaxTotal(maxConnections);
97105
connectionManager.setDefaultMaxPerRoute(maxConnections);
98106
RequestConfig.Builder defaultRequest = RequestConfig.custom();
@@ -134,7 +142,7 @@ private Registry<ConnectionSocketFactory> createConnectionSocketFactoryRegistry(
134142
.register("unix", new PlainConnectionSocketFactory() {
135143
@Override
136144
public Socket createSocket(HttpContext context) throws IOException {
137-
return DomainSocket.get(dockerHost.getPath());
145+
return UnixSocket.get(dockerHost.getPath());
138146
}
139147
})
140148
.register("npipe", new PlainConnectionSocketFactory() {

docker-java-transport-okhttp/src/main/java/com/github/dockerjava/okhttp/UnixSocketFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.github.dockerjava.okhttp;
22

3-
import com.github.dockerjava.transport.DomainSocket;
3+
import com.github.dockerjava.transport.UnixSocket;
44

55
import javax.net.SocketFactory;
66
import java.io.IOException;
@@ -18,7 +18,7 @@ class UnixSocketFactory extends SocketFactory {
1818
@Override
1919
public Socket createSocket() {
2020
try {
21-
return DomainSocket.get(socketPath);
21+
return UnixSocket.get(socketPath);
2222
} catch (IOException e) {
2323
throw new RuntimeException(e);
2424
}

docker-java-transport/src/main/java/com/github/dockerjava/transport/DomainSocket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public void close() throws IOException {
130130
* @param path the path to the domain socket
131131
* @return a {@link DomainSocket} instance
132132
* @throws IOException if the socket cannot be opened
133+
* @deprecated use {@link UnixSocket#get(String)}
133134
*/
135+
@Deprecated
134136
public static DomainSocket get(String path) throws IOException {
135137
if (Platform.isMac() || isBsdPlatform()) {
136138
return new BsdDomainSocket(path);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.github.dockerjava.transport;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.net.Socket;
7+
import java.net.SocketAddress;
8+
import java.net.SocketException;
9+
import java.nio.channels.Channels;
10+
import java.nio.channels.SocketChannel;
11+
12+
public class UnixSocket extends AbstractSocket {
13+
14+
/**
15+
* Return a new {@link Socket} for the given path. Will use JDK's {@link java.net.UnixDomainSocketAddress}
16+
* if available and fallback to {@link DomainSocket} otherwise.
17+
*
18+
* @param path the path to the domain socket
19+
* @return a {@link Socket} instance
20+
* @throws IOException if the socket cannot be opened
21+
*/
22+
public static Socket get(String path) throws IOException {
23+
try {
24+
return new UnixSocket(path);
25+
} catch (Exception e) {
26+
//noinspection deprecation
27+
return DomainSocket.get(path);
28+
}
29+
}
30+
31+
private final SocketAddress socketAddress;
32+
33+
private final SocketChannel socketChannel;
34+
35+
private UnixSocket(String path) throws Exception {
36+
Class<?> unixDomainSocketAddress = Class.forName("java.net.UnixDomainSocketAddress");
37+
this.socketAddress =
38+
(SocketAddress) unixDomainSocketAddress.getMethod("of", String.class)
39+
.invoke(null, path);
40+
this.socketChannel = SocketChannel.open(this.socketAddress);
41+
}
42+
43+
@Override
44+
public InputStream getInputStream() throws IOException {
45+
if (isClosed()) {
46+
throw new SocketException("Socket is closed");
47+
}
48+
if (!isConnected()) {
49+
throw new SocketException("Socket is not connected");
50+
}
51+
if (isInputShutdown()) {
52+
throw new SocketException("Socket input is shutdown");
53+
}
54+
55+
return Channels.newInputStream(socketChannel);
56+
}
57+
58+
@Override
59+
public OutputStream getOutputStream() throws IOException {
60+
if (isClosed()) {
61+
throw new SocketException("Socket is closed");
62+
}
63+
if (!isConnected()) {
64+
throw new SocketException("Socket is not connected");
65+
}
66+
if (isOutputShutdown()) {
67+
throw new SocketException("Socket output is shutdown");
68+
}
69+
70+
return Channels.newOutputStream(socketChannel);
71+
}
72+
73+
@Override
74+
public SocketAddress getLocalSocketAddress() {
75+
return socketAddress;
76+
}
77+
78+
@Override
79+
public SocketAddress getRemoteSocketAddress() {
80+
return socketAddress;
81+
}
82+
83+
@Override
84+
public void close() throws IOException {
85+
super.close();
86+
this.socketChannel.close();
87+
}
88+
}

docker-java/src/test/java/com/github/dockerjava/core/DefaultDockerClientConfigTest.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public void emptyHost() {
9898
DefaultDockerClientConfig config = buildConfig(env, new Properties());
9999

100100
assertEquals(
101-
config.getDockerHost().toString(),
102-
DefaultDockerClientConfig.DEFAULT_PROPERTIES.get(DefaultDockerClientConfig.DOCKER_HOST)
101+
DefaultDockerClientConfig.DEFAULT_DOCKER_HOST,
102+
config.getDockerHost().toString()
103103
);
104104
}
105105

@@ -224,6 +224,46 @@ public void withDockerTlsVerify() throws Exception {
224224
assertThat((Boolean) field.get(builder), is(true));
225225
}
226226

227+
@Test
228+
public void dockerHostSetExplicitlyOnSetter() {
229+
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(Collections.emptyMap(), new Properties());
230+
assertThat(builder.isDockerHostSetExplicitly(), is(false));
231+
232+
builder.withDockerHost("tcp://foo");
233+
assertThat(builder.isDockerHostSetExplicitly(), is(true));
234+
}
235+
236+
@Test
237+
public void dockerHostSetExplicitlyOnSystemProperty() {
238+
Properties systemProperties = new Properties();
239+
systemProperties.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo");
240+
241+
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(Collections.emptyMap(), systemProperties);
242+
243+
assertThat(builder.isDockerHostSetExplicitly(), is(true));
244+
}
245+
246+
@Test
247+
public void dockerHostSetExplicitlyOnEnv() {
248+
Map<String, String> env = new HashMap<>();
249+
env.put(DefaultDockerClientConfig.DOCKER_HOST, "tcp://foo");
250+
251+
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(env, new Properties());
252+
253+
assertThat(builder.isDockerHostSetExplicitly(), is(true));
254+
}
255+
256+
@Test
257+
public void dockerHostSetExplicitlyIfSetToDefaultByUser() {
258+
Map<String, String> env = new HashMap<>();
259+
env.put(DefaultDockerClientConfig.DOCKER_HOST, DefaultDockerClientConfig.DEFAULT_DOCKER_HOST);
260+
261+
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder(env, new Properties());
262+
263+
assertThat(builder.isDockerHostSetExplicitly(), is(true));
264+
}
265+
266+
227267
@Test
228268
public void testGetAuthConfigurationsFromDockerCfg() throws URISyntaxException {
229269
File cfgFile = new File(Resources.getResource("com.github.dockerjava.core/registry.v1").toURI());

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<jackson.version>2.10.3</jackson.version>
6262
<jackson-jaxrs.version>2.10.3</jackson-jaxrs.version>
6363
<httpclient.version>4.5.12</httpclient.version><!-- 4.5.1-4.5.2 broken -->
64-
<commons-compress.version>1.20</commons-compress.version>
64+
<commons-compress.version>1.21</commons-compress.version>
6565
<commons-codec.version>1.11</commons-codec.version>
6666
<commons-io.version>2.6</commons-io.version>
6767
<commons-lang.version>2.6</commons-lang.version>

0 commit comments

Comments
 (0)