Skip to content

Commit

Permalink
Do not throw when stopping an already stopped container
Browse files Browse the repository at this point in the history
  • Loading branch information
garyparrot authored Jul 19, 2020
1 parent 0578b08 commit 05f8001
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions fixtures/docker-exit-immediately/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.10

MAINTAINER Cristian Greco

ENTRYPOINT ["echo", "This container will exit immediately."]
16 changes: 13 additions & 3 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import byline from "byline";
import dockerode, { ContainerInspectInfo } from "dockerode";
import log from "./logger";
import { Duration, TemporalUnit } from "node-duration";
import { Command, ContainerName, ExitCode } from "./docker-client";
import { Port } from "./port";
Expand Down Expand Up @@ -60,9 +61,18 @@ export class DockerodeContainer implements Container {
}

public stop(options: StopOptions): Promise<void> {
return this.container.stop({
t: options.timeout.get(TemporalUnit.SECONDS),
});
return this.container
.stop({
t: options.timeout.get(TemporalUnit.SECONDS),
})
.catch((error) => {
/* 304 container already stopped */
if (error.statusCode === 304) {
log.warn(`Container ${this.getId()} already stopped`);
} else {
throw error;
}
});
}

public remove(options: RemoveOptions): Promise<void> {
Expand Down
11 changes: 11 additions & 0 deletions src/generic-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,5 +317,16 @@ describe("GenericContainer", () => {

expect(response.status).toBe(200);
});

it("should exit immediately and stop without exception", async () => {
const message = "This container will exit immediately.";
const context = path.resolve(fixtures, "docker-exit-immediately");
const container = await GenericContainer.fromDockerfile(context).build();
const startedContainer = await container.withWaitStrategy(Wait.forLogMessage(message)).start();

await new Promise((res) => setTimeout(res, 1000));

await startedContainer.stop();
});
});
});

0 comments on commit 05f8001

Please sign in to comment.