1

I am using Dockerode to trigger the execution of a Docker container with the following run() method. How can I run it in detach mode please?

// Instantiate Docker
var Docker = require("dockerode");
var docker = new Docker({ socketPath: "/var/run/docker.sock" });

// Run Docker container
docker.run(
    "mobydq-scripts",
    ["python", "run.py", authorization, "test_data_source", dataSourceId.toString()],
    process.stdout,
    { name: "mobydq-test-data-source", HostConfig: { AutoRemove: true, NetworkMode: "mobydq_network" } },
    function(err, data, container) {
        // Do nothing
    }
);
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77

1 Answers1

0

We can follow their example here

Basically, you will need to create a container, start it manually and exec inside to run our own script.

Credit to https://github.com/apocas/dockerode/issues/106

EDIT 1: To show an example of how to apply the example to your use case:

// Instantiate Docker
var Docker = require("dockerode");
var docker = new Docker({ socketPath: "/var/run/docker.sock" });

function runExec(container) {

  var options = {
    Cmd: ["python", "run.py", authorization, "test_data_source", dataSourceId.toString()],
    AttachStdout: true,
    AttachStderr: true
  };

  container.exec(options, function(err, exec) {
    if (err) return;
    exec.start(function(err, stream) {
      if (err) return;

      container.modem.demuxStream(stream, process.stdout, process.stderr);

      exec.inspect(function(err, data) {
        if (err) return;
        console.log(data);

        // Your code continue here
      });
    });
  });
}


docker.createContainer({
  Image: 'mobydq-scripts',
  Tty: true,
  Cmd: ['/bin/bash', '-c', 'tail -f /dev/null'],
  name: "mobydq-test-data-source",
  HostConfig: { AutoRemove: true, NetworkMode: "mobydq_network" }
}, function(err, container) {
  container.start({}, function(err, data) {
    runExec(container);
  });
});

You can also take a look at their README where they createContainer and attach to it.

Nguyen Lam Phuc
  • 1,411
  • 1
  • 7
  • 12