Joel Murphy
Joel Murphy

Reputation: 2512

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

Upvotes: 1

Views: 609

Answers (2)

Alex Zemlyakov
Alex Zemlyakov

Reputation: 171

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

Upvotes: 1

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

The --privileged flag would normally be used when you run your docker container that is running your NodeJS code above, not when starting your new container that does the ls. However, you shouldn't have to use --privileged in this case since your new container doesn't need it. You're probably just missing the docker socket bind mount when running your NodeJS container:

docker run --name nodeJS -v /var/run/docker.sock:/var/run/docker.sock ...

Upvotes: 1

Related Questions