我正在编写一个使用dockernode作为Docker客户端的节点程序。该程序创建一个容器,其容量在启动容器时绑定到主机上的目录。一开始,我尝试打印共享卷的内容以证明它正常工作。但是,我一直在(ls: /tmp/app: No such file or directory
。
这是我的代码......
var Docker = require('dockerode'),
docker = new Docker(),
mkdirp = require('mkdirp'),
volume = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/' + Date.now();
function handleError(action, err) {
if (err) {
console.error('error while ' + action + '...');
console.error(err);
}
}
mkdirp.sync(volume);
docker.createContainer({
Image: 'ubuntu',
Volumes: {
'/tmp/app': {}
}
}, function(err, container) {
handleError('building', err);
container.start({
Binds: [volume + ':/tmp/app']
}, function(err, data) {
handleError('starting', err);
container.exec({
AttachStdout: true,
AttachStderr: true,
Tty: false,
Cmd: ['/bin/ls', '/tmp/app']
}, function(err, exec) {
handleError('executing `ls /tmp/app`', err);
exec.start(function(err, stream) {
handleError('handling response from `ls /tmp/app`', err);
stream.setEncoding('utf8');
stream.pipe(process.stdout);
});
});
});
});
我在没有exec的情况下成功完成了这项操作,我在其中创建容器,附加到它,使用ls
命令启动它,等待它完成,然后将其删除并将其删除。但我希望使用exec,这样一旦容器运行,我就可以发出多个命令。我一直试图通过dockerode库和Docker远程API文档中的示例将它拼凑在一起。我只是不知道自己哪里出错了。
供参考,这里是没有exec的代码......
docker.createContainer({
Image: 'ubuntu',
Cmd: ['/bin/ls', '/tmp/app'],
Volumes: {
'/tmp/app': {}
}
}, function(err, container) {
console.log('attaching to... ' + container.id);
container.attach({stream: true, stdout: true, stderr: true, tty: true}, function(err, stream) {
handleError('attaching', err);
stream.pipe(process.stdout);
console.log('starting... ' + container.id);
container.start({
Binds: [volume + ':/tmp/app']
}, function(err, data) {
handleError('starting', err);
});
container.wait(function(err, data) {
handleError('waiting', err);
console.log('killing... ' + container.id);
container.kill(function(err, data) {
handleError('killing', err);
console.log('removing... ' + container.id);
container.remove(function(err, data) {
handleError('removing', err);
});
});
});
});
});
答案 0 :(得分:0)
我曾经在同一个问题上苦苦挣扎过一段时间,但我找到了解决办法。似乎Remote API不接受带有参数的命令作为一个字符串,但您必须将每个参数拆分为Cmd属性数组中的新标记;例如,使用gcc:
"Cmd":["/bin/bash","-c","gcc -Wall -std=c99 hello.c -o hello.bin"]
经过此修改后,它可以正常工作。
官方文档可能更适合远程API配置。