Search code examples
node.jsunixexecdockerboot2docker

Running a docker container inside another docker container?


I'm trying to create a contained docker environment inside another contained docker environment and call it through NodeJS's exec function.

I've read through the Docker docs and as far as I can see, this is achieved by passing the --privileged flag when starting a container.

As I'm developing on a mac, I have boot2docker installed to make docker work correctly, so I'm not sure exactly how to set this --privileged flag.

This is the code I've written so far:

var st = 'docker run virtual_machine ls'

//log the statement in console (test)
console.log(st);

//execute the Docker command
child = exec(st,
function (error, stdout, stderr) {
  console.log('stdout: ' + stdout);
  console.log('stderr: ' + stderr);
  if (error !== null) {
      console.log('exec error: ' + error);
  }
});

But this is the output (error) I'm getting:

stdout: 
stderr: time="2015-02-15T18:27:15Z" level="fatal" msg="Post http:///var/run/docker.sock/v1.17/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?" 

exec error: Error: Command failed: time="2015-02-15T18:27:15Z" level="fatal" msg="Post http:///var/run/docker.sock/v1.17/containers/create: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?"

How can I make this exec call work? Is it as simple as setting a flag? If so, how do I do this within boot2docker?

Thanks


Solution

  • For example,

    docker run --rm \
      -v /var/run/docker.sock:/run/docker.sock \
      -v $(which docker):/bin/docker \
      --privileged \
      node:0.12 node my_script.js