12.14.1 • Published 2 years ago

@grucloud/docker-axios v12.14.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Node client library for Docker

A lean implementation of a node client targeting the Docker API.

npm i @grucloud/docker-axios
  • Based on axios for commnucation with the docker API: UNIX socket and HTTP
  • Implemented with the amazing functional programming library rubico, a better alternative to lodash and ramda.

Setup

Create a Docker client

Import the DockerClient from @grucloud/docker-axios.

Create a client with options. These options are forwarded to axios

const { DockerClient } = require("@grucloud/docker-axios");

const docker = DockerClient({
  baseURL: "http://localhost/v1.40",
  socketPath: "/var/run/docker.sock",
  timeout: 15e3,
});

Container API

Examples of the docker container API:

  • list
  • create
  • start
  • wait
  • delete

Create a container

Please refer to the offical ContainerCreate documentation for a detailed list of the parameter.

const { v4: uuidv4 } = require("uuid");
const path = require("path");

const containerImage = "grucloud-aws";
const localVolume = "volume";
const localVolumePath = path.resolve(localVolume);
const containerName = `${containerImage}-${uuidv4()}`;

const createParam = {
  name: containerName,
  body: {
    Image: containerImage,
    Cmd: ["help"],
    HostConfig: {
      Binds: [`${localVolumePath}:/app/output`],
    },
  },
};
const result = await docker.container.create(createParam);
assert(result.Id);

Start a container

Start a container by name, options in ContainerStart

const startParam = {
  name: containerName,
};
await docker.container.start(startParam);

Wait for a container

Wait for a container to finished.

const waitParam = {
  name: containerName,
};
const result = await docker.container.wait(waitParam);
assert.equal(result.StatusCode, 0);

List containers

List all containers, options defined in ContainerList

const result = await docker.container.list({});

List a container by name:

const result = await docker.container.list({
  filters: `{"name": ["${containerName}"]}`,
});
assert.equal(result.length, 1);

Get container detail

Get a container details, options defined in ContainerInspect

const result = await docker.container.get({ id: "container id" });
assert(result.State.Status);

Retrieve container logs

Obtaint the logs from the container by name. See all options at ContainerLogs

const logParam = {
  name: containerName,
  options: {
    stdout: 1,
    stderr: 1,
    //tail: 100,
    //follow: 0,
  },
};
const stream = await docker.container.log(logParam);
stream.on("data", (data) => {
  console.log(data.toString());
});
stream.on("close", () => {});
stream.on("error", () => {});

Destroy a container

Delete a container by name, options in ContainerDelete

const destroyParam = {
  name: containerName,
};
await docker.container.delete(destroyParam);
12.7.0

2 years ago

12.7.1

2 years ago

12.7.2

2 years ago

12.6.4

2 years ago

12.14.1

2 years ago

12.1.0

2 years ago

12.0.1

2 years ago

11.0.2

2 years ago

11.0.0

2 years ago

10.0.0

3 years ago

9.1.3

3 years ago

8.0.0

3 years ago

9.0.0

3 years ago

7.0.0

3 years ago

7.3.0

3 years ago

7.2.1

3 years ago

7.0.3

3 years ago

5.0.0

3 years ago

6.0.0

3 years ago

6.0.2

3 years ago

6.0.4

3 years ago

5.13.0

3 years ago

5.12.0

3 years ago

5.11.0

3 years ago

4.0.0

3 years ago

5.8.0

3 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.31.0

4 years ago

1.18.0

4 years ago

1.17.9

4 years ago

1.15.2

4 years ago

1.15.1

4 years ago

1.15.0

4 years ago