Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-64954 Explicitly preserve docker connection used for a specific image #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -206,8 +207,9 @@ public Execution() {
}

ImageAction.add(step.image, run);
String dockerHost = envHost.get("DOCKER_HOST", "unix:///var/run/docker.sock");
getContext().newBodyInvoker().
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, dockerHost, envHost, ws, toolName, dockerVersion))).
withCallback(new Callback(container, toolName)).
start();
return false;
Expand Down Expand Up @@ -240,14 +242,16 @@ private static class Decorator extends LauncherDecorator implements Serializable

private static final long serialVersionUID = 1;
private final String container;
private final String dockerHost;
private final String[] envHost;
private final String ws;
private final @CheckForNull String toolName;
private final boolean hasEnv;
private final boolean hasWorkdir;

Decorator(String container, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
Decorator(String container, String dockerHost, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
this.container = container;
this.dockerHost = dockerHost;
this.envHost = Util.mapToEnv(envHost);
this.ws = ws;
this.toolName = toolName;
Expand Down Expand Up @@ -332,6 +336,8 @@ private static class Decorator extends LauncherDecorator implements Serializable
System.arraycopy(originalMasks, 0, masks, prefix.size(), originalMasks.length);
starter.masks(masks);

starter.envs(Collections.singletonMap("DOCKER_HOST", dockerHost));

return super.launch(starter);
}
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,23 @@ private static final class Execution extends SynchronousNonBlockingStepExecution
}
}

@Test
@Issue("JENKINS-64954")
public void dockerConnectionTracking() {
story.then(r -> {
DockerTestUtil.assumeDocker();
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node {\n"
+ " docker.image(\"docker:dind\").withRun(\"--privileged\", \"dockerd --tls=false --host 0.0.0.0\") { dockerContainer ->\n"
+ " docker.image(\"docker:dind\").inside(\"--link ${dockerContainer.id}:docker --entrypoint=''\") {\n"
+ " docker.withServer(\"tcp://docker\") {\n"
+ " sh \"echo with nested docker\"\n"
+ " }\n"
+ " }\n"
+ " }\n"
+ "}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("with nested docker", b);
});
}
}