我使用Dockerode来使用以下run()方法触发Docker容器的执行。我怎样才能在分离模式下运行?
// 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
}
);发布于 2020-02-07 05:03:11
我们可以效仿他们的例子,这里
基本上,您需要创建一个容器,手动启动它,然后在内部运行exec来运行我们自己的脚本。
贷给https://github.com/apocas/dockerode/issues/106
编辑1:演示如何将该示例应用于用例的示例:
// 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);
});
});您还可以查看他们的自述文件,在那里他们可以使用createContainer和attach。
https://stackoverflow.com/questions/60106857
复制相似问题