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

Fix Windows #147

Merged
merged 6 commits into from
Oct 21, 2020
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
@@ -1,4 +1,4 @@
version: "3"
version: "3.5"

services:
container:
Expand Down
3 changes: 2 additions & 1 deletion fixtures/docker-compose/docker-compose-with-name.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
version: "3"
version: "3.5"

services:
db:
container_name: custom_container_name
Expand Down
2 changes: 1 addition & 1 deletion fixtures/docker-compose/docker-compose-with-network.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: "3.5"

services:
container:
Expand Down
2 changes: 1 addition & 1 deletion fixtures/docker-compose/docker-compose-with-volume.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: "3.5"

services:
container:
Expand Down
2 changes: 1 addition & 1 deletion fixtures/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "3"
version: "3.5"

services:
container:
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"get-port": "^5.1.1",
"glob": "^7.1.6",
"node-duration": "^1.0.4",
"slash": "^3.0.0",
"stream-to-array": "^2.3.0",
"tar-fs": "^2.1.0"
},
Expand Down
11 changes: 10 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type InspectResult = {

type ExecInspectResult = {
exitCode: ExitCode;
running: boolean;
entrypoint: string;
arguments: string[];
};

interface Exec {
Expand Down Expand Up @@ -151,6 +154,12 @@ class DockerodeExec implements Exec {

public async inspect(): Promise<ExecInspectResult> {
const inspectResult = await this.exec.inspect();
return { exitCode: inspectResult.ExitCode === null ? -1 : inspectResult.ExitCode };

return {
exitCode: inspectResult.ExitCode === null ? -1 : inspectResult.ExitCode,
running: inspectResult.Running,
entrypoint: inspectResult.ProcessConfig.entrypoint,
arguments: inspectResult.ProcessConfig.arguments,
};
}
}
24 changes: 18 additions & 6 deletions src/docker-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Dockerode, { Network, PortMap as DockerodePortBindings } from "dockerode"
import { Duration, TemporalUnit } from "node-duration";
import streamToArray from "stream-to-array";
import tar from "tar-fs";
import slash from "slash";
import { BoundPorts } from "./bound-ports";
import { Container, DockerodeContainer, Id } from "./container";
import { Host } from "./docker-client-factory";
Expand All @@ -10,6 +11,7 @@ import { log } from "./logger";
import { PortString } from "./port";
import { RepoTag } from "./repo-tag";
import { PullStreamParser } from "./pull-stream-parser";
import { Readable } from "stream";

export type Command = string;
export type ContainerName = string;
Expand Down Expand Up @@ -172,7 +174,6 @@ export class DockerodeClient implements DockerClient {
}

public start(container: Container): Promise<void> {
log.info(`Starting container with ID: ${container.getId()}`);
return container.start();
}

Expand All @@ -183,11 +184,22 @@ export class DockerodeClient implements DockerClient {
attachStderr: true,
});

const stream = await exec.start();
const output = Buffer.concat(await streamToArray(stream)).toString();
const { exitCode } = await exec.inspect();
const stream = (await exec.start()).setEncoding("utf-8") as Readable;

return { output, exitCode };
return await new Promise((resolve) => {
let output = "";
stream.on("data", (chunk) => (output += chunk));

const interval = setInterval(async () => {
const { running, exitCode } = await exec.inspect();

if (!running) {
clearInterval(interval);
stream.destroy();
resolve({ output, exitCode });
}
}, 100);
});
}

public async buildImage(
Expand All @@ -198,7 +210,7 @@ export class DockerodeClient implements DockerClient {
): Promise<void> {
log.info(`Building image '${repoTag.toString()}' with context '${context}'`);
const dockerIgnoreFiles = await findDockerIgnoreFiles(context);
const tarStream = tar.pack(context, { ignore: (name) => dockerIgnoreFiles.has(name) });
const tarStream = tar.pack(context, { ignore: (name) => dockerIgnoreFiles.has(slash(name)) });
const stream = await this.dockerode.buildImage(tarStream, {
dockerfile: dockerfileName,
buildargs: buildArgs,
Expand Down
2 changes: 1 addition & 1 deletion src/docker-compose-environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("DockerComposeEnvironment", () => {

it("should throw error when compose file is malformed", async () => {
await expect(new DockerComposeEnvironment(fixtures, "docker-compose-malformed.yml").up()).rejects.toThrowError(
`Version in "./docker-compose-malformed.yml" is invalid - it should be a string.`
`Version in ".${path.sep}docker-compose-malformed.yml" is invalid - it should be a string.`
);
});

Expand Down
3 changes: 2 additions & 1 deletion src/docker-ignore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { existsSync, promises as fs } from "fs";
import os from "os";
import glob from "glob";
import path from "path";

Expand All @@ -12,7 +13,7 @@ export const findDockerIgnoreFiles = async (context: string): Promise<Set<string

const dockerIgnorePatterns = (await fs.readFile(dockerIgnoreFilePath, { encoding: "utf-8" }))
.toString()
.split("\n")
.split(os.EOL)
.map((dockerIgnorePattern) => path.resolve(context, dockerIgnorePattern));

const dockerIgnoreMatches: string[][] = await Promise.all(
Expand Down
8 changes: 8 additions & 0 deletions src/generic-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class GenericContainer implements TestContainer {
autoRemove: this.daemonMode,
});

log.info(`Starting container ${this.repoTag} with ID: ${container.getId()}`);
await dockerClient.start(container);

if (!this.daemonMode) {
Expand Down Expand Up @@ -241,6 +242,13 @@ export class GenericContainer implements TestContainer {
log.info("Container is ready");
} catch (err) {
log.error("Container failed to be ready");

if (this.daemonMode) {
(await container.logs())
.on("data", (data) => containerLog.trace(`${container.getId()}: ${data}`))
.on("err", (data) => containerLog.error(`${container.getId()}: ${data}`));
}

try {
await container.stop({ timeout: new Duration(0, TemporalUnit.MILLISECONDS) });
await container.remove({ removeVolumes: true });
Expand Down
2 changes: 1 addition & 1 deletion src/reaper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Reaper {
.withName(`ryuk-${sessionId}`)
.withExposedPorts(8080)
.withWaitStrategy(Wait.forLogMessage("Started!"))
.withBindMount(dockerClient.getSocketPath() || "/var/run/docker.sock", "/var/run/docker.sock")
.withBindMount("/var/run/docker.sock", "/var/run/docker.sock")
.withDaemonMode()
.withPrivilegedMode()
.start();
Expand Down