How to run docker container from within a docker container

Hi,

I’m experimenting with running a docker container from within another container using the dockerode npm package. Does anyone have guidance on how to do this?

I am getting an error:

Error: getaddrinfo ENOTFOUND docker
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'docker'
}

If i execute my index.js file it runs perfectly fine, but when i try to turn the node js app executing the index.js method into a container then it gives the above error. This is my index.js file:

const Docker = require('dockerode')
const streams = require('memory-streams')

const docker = new Docker()

console.log({
  docker
});

// you may specify a timeout (in ms) for all operations, allowing to make sure you don't fall into limbo if something happens in docker
// var docker = new Docker({host: 'http://127.0.0.1', port: 2375, timeout: 100});

const stdout = new streams.WritableStream()
const stderr = new streams.WritableStream()

const imageName = "sandboxrunner";

console.log("Sandbox container starting...");
console.log(`Running image: ${imageName}`);

const runDocker = () => new Promise((resolve, reject) => {
  docker.run(
      imageName,
      [],
      [stdout, stderr], {
        Tty: false,
        HostConfig: {
          AutoRemove: true,
          Binds: [
            `/var/run/docker.sock:/var/run/docker.sock`,
            `/usr/bin/docker:/usr/bin/docker`
          ]
        }
      },

    )
    .then(([res, container]) => {
      console.log(res)
      console.log(stdout.toString())
      console.log('stderr: %j', stderr.toString())
      return;
    }).then(function () {
      resolve();
    })
    .catch((error) => console.log(error))
});

runDocker().then(function () {
  console.log("finished!!!!!!!");
  process.exit(0);
});

and this is my docker file to build the app running the index.js file:

FROM docker
WORKDIR '/app'
COPY ./package.json ./
RUN apk add npm
RUN npm install 
COPY . .
CMD ["npm","run","start"]