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-70132] Remove anonymous volumes when removing the container #286

Merged
merged 4 commits into from
Dec 1, 2022
Merged
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 @@ -199,7 +199,7 @@ public void stop(@NonNull EnvVars launchEnv, @NonNull String containerId) throws
*/
public void rm(@NonNull EnvVars launchEnv, @NonNull String containerId) throws IOException, InterruptedException {
LaunchResult result;
result = launch(launchEnv, false, "rm", "-f", containerId);
result = launch(launchEnv, false, "rm", "-f", "--volumes", containerId);
if (result.getStatus() != 0) {
throw new IOException(String.format("Failed to rm container '%s'.", containerId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Docker implements Serializable {

public void stop() {
docker.script.withEnv(["JD_ID=${id}"]) {
docker.script."${docker.shell(isUnix)}" 'docker stop "' + docker.asEnv(isUnix,'JD_ID') + '" && docker rm -f "' + docker.asEnv(isUnix, 'JD_ID') + '"'
docker.script."${docker.shell(isUnix)}" 'docker stop "' + docker.asEnv(isUnix,'JD_ID') + '" && docker rm -f --volumes "' + docker.asEnv(isUnix, 'JD_ID') + '"'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,31 @@ public void setup() throws Exception {

@Test
public void test_run() throws IOException, InterruptedException {
// Pin to a specific sha256 hash of the image to avoid any potential issues with the image changing in the future.
// Original image tag: docker:20.10.9-dind
String image = "docker.io/library/docker@sha256:d842418d21545fde57c2512681d9bdc4ce0e54f2e0305a293ee20a9b6166932b";
EnvVars launchEnv = DockerTestUtil.newDockerLaunchEnv();
String containerId =
dockerClient.run(launchEnv, "learn/tutorial", null, null, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), new EnvVars(),
dockerClient.run(launchEnv, image, null, null, Collections.<String, String>emptyMap(), Collections.<String>emptyList(), new EnvVars(),
dockerClient.whoAmI(), "cat");
Assert.assertEquals(64, containerId.length());
ContainerRecord containerRecord = dockerClient.getContainerRecord(launchEnv, containerId);
Assert.assertEquals(dockerClient.inspect(launchEnv, "learn/tutorial", ".Id"), containerRecord.getImageId());
Assert.assertEquals(dockerClient.inspect(launchEnv, image, ".Id"), containerRecord.getImageId());
Assert.assertTrue(containerRecord.getContainerName().length() > 0);
Assert.assertTrue(containerRecord.getHost().length() > 0);
Assert.assertTrue(containerRecord.getCreated() > 1000000000000L);
Assert.assertEquals(Collections.<String>emptyList(), dockerClient.getVolumes(launchEnv, containerId));

// Check that an anonymous volume was created mounted at /var/lib/docker
Assert.assertEquals(Collections.<String>singletonList("/var/lib/docker"), dockerClient.getVolumes(launchEnv, containerId));
String anonymousVolumeName = dockerClient.inspect(launchEnv, containerId, "range .Mounts }}{{ .Name }}{{ end");
Assert.assertEquals(64, anonymousVolumeName.length());

// Also test that the stop works and cleans up after itself
Assert.assertNotNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
dockerClient.stop(launchEnv, containerId);
Assert.assertNull(dockerClient.inspect(launchEnv, containerId, ".Name"));
// Check that the anonymous volume was removed
Assert.assertNull(dockerClient.inspect(launchEnv, anonymousVolumeName, ".Name"));
}

@Test
Expand Down