3

I have been trying to call docker from my Node.js application, and to do so I am using node-docker-api as described in the npm module documentation https://github.com/AgustinCB/docker-api. To test if I am able to interact with docker from Node.js I am running a small sample application given as a example in the documentation. But I am getting error as { Error: connect ENOENT /var/run/docker.sock. The complete error message is shown below

dockerOperations.js:

'use strict';
const {Docker} = require('node-docker-api');
var Q = require('q');


var service = {}
service.runDockerCommand = runDockerCommand;
    function runDockerCommand() {
      console.log('inside runDockerCommand');
      var deferred = Q.defer();
      const docker = new Docker({ socketPath: '/var/run/docker.sock' });
      console.log(docker);

      docker.container.create({
        Image: 'ubuntu',
        name: 'test'
      })
        .then(container => container.start())
        .then(container => container.stop())
        .then(container => container.restart())
        .then(container => container.delete({ force: true }))
        .catch(error => console.log(error));

        return deferred.promise;
    }

Error

{ Error: connect ENOENT /var/run/docker.sock
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1170:14)

errno: 'ENOENT', code: 'ENOENT', syscall: 'connect', address: '/var/run/docker.sock' }

abhi
  • 1,920
  • 6
  • 24
  • 27
  • Do you have the docker service running on the host machine? Does `/var/run/docker.sock` exist on the host? What OS is your host machine? – Mike Gorski Apr 17 '18 at 01:13
  • @MikeGorski: It's a windows machine, docker service is running but I am not able to find /var/run/docker.sock , is there any specific directory that I can look into? – abhi Apr 17 '18 at 01:16
  • According to this https://docs.docker.com/docker-for-windows/faqs/#how-do-i-connect-to-the-remote-docker-engine-api it's `npipe:////./pipe/docker_engine` – Mike Gorski Apr 17 '18 at 02:06
  • 1
    Because of Windows file system, there is no such /var/run/docker.sock You need to map this path from docker to your Windows host – Algeriassic Apr 17 '18 at 02:10
  • @Algeriassic: how can I do that? – abhi Apr 17 '18 at 02:12
  • @MikeGorski: I tried npipe option, but I am getting the same error – abhi Apr 17 '18 at 18:20
  • @abhi: docker run -d -v \\.\pipe\docker_engine:/var/run/docker.sock blahblah... – Algeriassic Apr 18 '18 at 04:30

3 Answers3

4

I just faced the same issue. In order to solve it, you need to mount the docker socket of your host machine into a volume of your container. This should allow your script to access it to trigger containers. I personally use Docker Compose, here is an example how I did it, see line volumes:

version: "3.1"
services:
    graphql:
        container_name: mobydq-graphql
        restart: always
        image: mobydq-graphql
        build:
            context: ./graphql
        volumes:
            - //var/run/docker.sock:/var/run/docker.sock
        env_file:
            - ./.env
        depends_on:
            - db
        networks:
            - network
        command:
            [some command...]
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77
0

Docker on windows does not use unix sockets. You'll have to access the docker daemon over tcp. Try connecting to port 2375 over tcp.

https://docs.docker.com/engine/reference/commandline/dockerd/#examples

Shoan
  • 4,003
  • 1
  • 26
  • 29
  • I modified the socket path to 'tcp://0.0.0.0:2375', I am still getting the same error. Here is the line I modified : const docker = new Docker({ socketPath: 'tcp://0.0.0.0:2375' }); – abhi Apr 17 '18 at 18:17
0

This can solve. (node-docker-api@1.1.22)

const docker = new Docker({ socketPath: '//./pipe/docker_engine' })

(not npipe:////./pipe/docker_engine)

Chrg
  • 1