Skip to content

Commit 8fbfa8b

Browse files
committed
[JENKINS-41316] don’t override entrypoint but double check the command is well executed and warn user if this is not the case
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 8367ac4 commit 8fbfa8b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ public static class Execution extends AbstractStepExecutionImpl {
182182
}
183183

184184
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ "cat");
185+
final List<String> ps = dockerClient.listProcess(env, container);
186+
if (!ps.contains("cat")) {
187+
listener.error("The container started but didn't ran the expected command. Please double check your Entrypoint does execute the command passed as docker run argument. See https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/#entrypoint for entrypoint best practices.");
188+
throw new IOException("Failed to run container with CMD `cat`");
189+
}
190+
185191
DockerFingerprints.addRunFacet(dockerClient.getContainerRecord(env, container), run);
186192
ImageAction.add(step.image, run);
187193
getContext().newBodyInvoker().

src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,22 @@
3535

3636
import javax.annotation.CheckForNull;
3737
import javax.annotation.Nonnull;
38+
import java.io.BufferedReader;
3839
import java.io.ByteArrayOutputStream;
3940
import java.io.IOException;
41+
import java.io.Reader;
42+
import java.io.StringReader;
4043
import java.nio.charset.Charset;
4144
import java.text.ParseException;
4245
import java.text.SimpleDateFormat;
46+
import java.util.ArrayList;
4347
import java.util.Collection;
4448
import java.util.Collections;
4549
import java.util.Date;
4650
import java.util.Map;
4751
import java.util.List;
4852
import java.util.Arrays;
53+
import java.util.StringTokenizer;
4954
import java.util.concurrent.TimeUnit;
5055
import java.util.logging.Level;
5156
import java.util.logging.Logger;
@@ -131,6 +136,30 @@ public String run(@Nonnull EnvVars launchEnv, @Nonnull String image, @CheckForNu
131136
}
132137
}
133138

139+
public List<String> listProcess(@Nonnull EnvVars launchEnv, @Nonnull String containerId) throws IOException, InterruptedException {
140+
LaunchResult result = launch(launchEnv, false, "top", containerId);
141+
if (result.getStatus() != 0) {
142+
throw new IOException(String.format("Failed to run top '%s'. Error: %s", containerId, result.getErr()));
143+
}
144+
List<String> processes = new ArrayList<>();
145+
try (Reader r = new StringReader(result.getOut());
146+
BufferedReader in = new BufferedReader(r)) {
147+
String line;
148+
in.readLine(); // ps header
149+
while ((line = in.readLine()) != null) {
150+
final StringTokenizer stringTokenizer = new StringTokenizer(line, " ");
151+
if (stringTokenizer.countTokens() < 4) {
152+
throw new IOException("Unexpected `docker top` output : "+line);
153+
}
154+
stringTokenizer.nextToken(); // PID
155+
stringTokenizer.nextToken(); // USER
156+
stringTokenizer.nextToken(); // TIME
157+
processes.add(stringTokenizer.nextToken()); // COMMAND
158+
}
159+
}
160+
return processes;
161+
}
162+
134163
/**
135164
* Stop a container.
136165
*

0 commit comments

Comments
 (0)