Skip to content

Commit

Permalink
Merge pull request #286 from felipecrs/rm-volumes
Browse files Browse the repository at this point in the history
[JENKINS-70132] Remove anonymous volumes when removing the container
  • Loading branch information
jglick authored Dec 1, 2022
2 parents dc466a8 + e5250d3 commit 75d68c0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
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

0 comments on commit 75d68c0

Please sign in to comment.