0

I'm new to Docker and I have some difficulties to understand how I should use it.

For now, I'm wondering if that makes sense to attempt sending commands to a docker machine on my computer from the client side script of a javascript web app using an SDK like Dockerode.

I installed Docker CE for windows (17.06.0-ce) and Docker Toolbox, and I ran a container on the default machine using the docker terminal. Now I'm wondering if the commands I typed could be sent from a web app using NodeJS. I tried using this code:

import Docker from 'dockerode';

const docker = new Docker({host: 'myDefaultMachineHost'});

export function createLocalDb () {

    docker.pull('someImageFromDockerHub', function (err, stream) {
      if (err) console.log("Catch : " + err.toString());
          stream.pipe(process.stdout, {end: true});

      stream.on('end', function() {
        //run the container
      }).catch(function (err) {
            console.log("Catch : " + err.toString());
        });
    });
}

But that doesn't work(stream.pipe throws an error). Am I misunderstanding the context in which I'm supposed to use dockerode ?

Thanks for your explanations !

yobb
  • 145
  • 1
  • 9

1 Answers1

0

In short: You need change your code to this const docker = new Docker({socketPath: '/var/run/docker.sock'}); and add docker socket inside your container.

Theory:

You have docker socket inside your local machine. You should add this socket inside your docker container. The volume is your solution.

Image for visualization this issue:

enter image description here

Implementation with arguments

This is simple task for Linux/Mac user. They can do

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

On Windows you need run

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

More details in this question.

Implementation with Dockerfile

Also, you can add to your Dockerfile VOLUME instruction.

On Linux/Mac it should be line like this:

VOLUME /var/run/docker.sock /var/run/docker.sock

I don't know who it will be on Windows, I use Mac.

galkin
  • 5,264
  • 3
  • 34
  • 51