Skip to content
This repository has been archived by the owner on Jan 22, 2018. It is now read-only.

Commit

Permalink
Allow to use unix sockets as URL (jenkinsci#206, JENKINS-23301)
Browse files Browse the repository at this point in the history
- allow to directly connect to the container without going through
  the mapped port on the host

This is useful if Jenkins itself runs as a container with the host's
Docker socket mapped[1]. To use the docker plugin in this case simply
set the Docker URL to "unix:///var/run/docker.sock".

[1] https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
  • Loading branch information
ermshiperete committed Oct 28, 2015
1 parent f2e134b commit 9b0f231
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ work
*.iml
/target
.*
*~
Expand Up @@ -31,9 +31,11 @@ public void buildVariablesFor(AbstractBuild build, Map<String, String> variables
variables.put("JENKINS_CLOUD_ID", dockerComputer.getCloudId());
try {
//replace http:// and https:// from docker-java to tcp://
final String dockerHost = new URIBuilder(dockerComputer.getCloud().serverUrl)
.setScheme("tcp")
.toString();
final URIBuilder uriBuilder = new URIBuilder(dockerComputer.getCloud().serverUrl);
if (uriBuilder.getScheme() == "http" || uriBuilder.getScheme() == "https") {
uriBuilder.setScheme("tcp");
}
final String dockerHost = uriBuilder.toString();
variables.put("DOCKER_HOST", dockerHost);
} catch (URISyntaxException e) {
LOG.error("Can't build 'DOCKER_HOST' var: {}", e.getMessage());
Expand Down
Expand Up @@ -123,6 +123,11 @@ public PortUtils getPortUtils(String cloudId, DockerTemplate dockerTemplate, Ins
//get address, if docker on localhost, then use local?
if (host == null || host.equals("0.0.0.0")) {
host = URI.create(DockerCloud.getCloudByName(cloudId).serverUrl).getHost();
if (host == null || host.equals("0.0.0.0")) {
// Try to connect to the container directly (without going through the host)
host = networkSettings.getIpAddress();

This comment has been minimized.

Copy link
@magnayn

magnayn Oct 28, 2015

Would I be right in assuming that the IP address here will be in the "private" IP range for docker containers (172.16.x.x or whatever)?

This comment has been minimized.

Copy link
@ermshiperete

ermshiperete Oct 28, 2015

Author Owner

yes

port = sshConnector.port;
}
}

return PortUtils.canConnect(host, port);
Expand Down
@@ -1,3 +1,3 @@
<div>
The URL to use to access your Docker server API (e.g: http://172.16.42.43:4243). Only http:// supported.
The URL to use to access your Docker server API (e.g: http://172.16.42.43:4243 or unix:///var/run/docker.sock).
</div>

0 comments on commit 9b0f231

Please sign in to comment.