Skip to content

Commit

Permalink
Support copying directories to a started container (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianrgreco authored Jul 19, 2023
1 parent 61a8066 commit b0e06f5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 2 deletions.
19 changes: 19 additions & 0 deletions docs/features/containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ const container = await new GenericContainer("alpine")
.start();
```

Or after it starts:

```javascript
const container = await new GenericContainer("alpine").start();

container.copyFilesToContainer([{
source: "/local/file.txt",
target: "/remote/file1.txt"
}])
container.copyDirectoriesToContainer([{
source: "/localdir",
target: "/some/nested/remotedir"
}])
container.copyContentToContainer([{
content: "hello world",
target: "/remote/file2.txt"
}])
````

An optional `mode` can be specified in octal for setting file permissions:

```javascript
Expand Down
36 changes: 36 additions & 0 deletions src/generic-container/generic-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,18 @@ describe("GenericContainer", () => {
await container.stop();
});

it("should copy file to started container", async () => {
const source = path.resolve(fixtures, "docker", "test.txt");
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();

await container.copyFilesToContainer([{ source, target }]);

expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining("hello world"));

await container.stop();
});

it("should copy directory to container", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp";
Expand Down Expand Up @@ -323,6 +335,18 @@ describe("GenericContainer", () => {
await container.stop();
});

it("should copy directory to started container", async () => {
const source = path.resolve(fixtures, "docker");
const target = "/tmp";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();

container.copyDirectoriesToContainer([{ source, target }]);

expect((await container.exec("cat /tmp/test.txt")).output).toEqual(expect.stringContaining("hello world"));

await container.stop();
});

it("should copy content to container", async () => {
const content = "hello world";
const target = "/tmp/test.txt";
Expand Down Expand Up @@ -353,6 +377,18 @@ describe("GenericContainer", () => {
await container.stop();
});

it("should copy content to started container", async () => {
const content = "hello world";
const target = "/tmp/test.txt";
const container = await new GenericContainer("cristianrgreco/testcontainer:1.1.14").withExposedPorts(8080).start();

container.copyContentToContainer([{ content, target }]);

expect((await container.exec(["cat", target])).output).toEqual(expect.stringContaining(content));

await container.stop();
});

it("should honour .dockerignore file", async () => {
const context = path.resolve(fixtures, "docker-with-dockerignore");
const container = await GenericContainer.fromDockerfile(context).build();
Expand Down
11 changes: 10 additions & 1 deletion src/generic-container/started-generic-container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RestartOptions, StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
import Dockerode from "dockerode";
import { ContentToCopy, ExecResult, FileToCopy, Labels } from "../docker/types";
import { ContentToCopy, DirectoryToCopy, ExecResult, FileToCopy, Labels } from "../docker/types";
import { inspectContainer, InspectResult } from "../docker/functions/container/inspect-container";
import { BoundPorts } from "../bound-ports";
import { containerLog, log } from "../logger";
Expand Down Expand Up @@ -135,6 +135,15 @@ export class StartedGenericContainer implements StartedTestContainer {
log.debug(`Copied files to container`, { containerId: this.container.id });
}

public async copyDirectoriesToContainer(directoriesToCopy: DirectoryToCopy[]): Promise<void> {
log.debug(`Copying directories to container...`, { containerId: this.container.id });
const tar = archiver("tar");
directoriesToCopy.forEach(({ source, target }) => tar.directory(source, target));
tar.finalize();
await putContainerArchive({ container: this.container, stream: tar, containerPath: "/" });
log.debug(`Copied directories to container`, { containerId: this.container.id });
}

public async copyContentToContainer(contentsToCopy: ContentToCopy[]): Promise<void> {
log.debug(`Copying content to container...`, { containerId: this.container.id });
const tar = archiver("tar");
Expand Down
6 changes: 5 additions & 1 deletion src/modules/abstract-started-container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RestartOptions, StartedTestContainer, StopOptions, StoppedTestContainer } from "../test-container";
import { ContentToCopy, ExecResult, FileToCopy, Labels } from "../docker/types";
import { ContentToCopy, DirectoryToCopy, ExecResult, FileToCopy, Labels } from "../docker/types";
import { Readable } from "stream";

export class AbstractStartedContainer implements StartedTestContainer {
Expand Down Expand Up @@ -67,6 +67,10 @@ export class AbstractStartedContainer implements StartedTestContainer {
return this.startedTestContainer.copyFilesToContainer(filesToCopy);
}

public async copyDirectoriesToContainer(directoriesToCopy: DirectoryToCopy[]): Promise<void> {
return this.startedTestContainer.copyDirectoriesToContainer(directoriesToCopy);
}

public async copyContentToContainer(contentsToCopy: ContentToCopy[]): Promise<void> {
return this.startedTestContainer.copyContentToContainer(contentsToCopy);
}
Expand Down
1 change: 1 addition & 0 deletions src/test-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export interface StartedTestContainer {
getNetworkId(networkName: string): string;
getIpAddress(networkName: string): string;
copyArchiveFromContainer(path: string): Promise<NodeJS.ReadableStream>;
copyDirectoriesToContainer(directoriesToCopy: DirectoryToCopy[]): Promise<void>;
copyFilesToContainer(filesToCopy: FileToCopy[]): Promise<void>;
copyContentToContainer(contentsToCopy: ContentToCopy[]): Promise<void>;
exec(command: string | string[]): Promise<ExecResult>;
Expand Down

0 comments on commit b0e06f5

Please sign in to comment.