Error: connect ENOENT /var/run/docker.sock when using node-docker-api
Asked Answered
F

4

6

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' }

Ferbam answered 17/4, 2018 at 1:0 Comment(7)
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?Allopatric
@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?Ferbam
According to this docs.docker.com/docker-for-windows/faqs/… it's npipe:////./pipe/docker_engineAllopatric
Because of Windows file system, there is no such /var/run/docker.sock You need to map this path from docker to your Windows hostGalcha
@Algeriassic: how can I do that?Ferbam
@MikeGorski: I tried npipe option, but I am getting the same errorFerbam
@abhi: docker run -d -v \\.\pipe\docker_engine:/var/run/docker.sock blahblah...Galcha
U
6

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...]
Unfruitful answered 27/1, 2020 at 8:41 Comment(0)
K
2

This can solve. ([email protected])

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

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

Krakow answered 26/5, 2022 at 3:14 Comment(0)
Y
1

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

Yowl answered 17/4, 2018 at 6:25 Comment(1)
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' });Ferbam
D
1

Ok so i was having the exact same issue i'm on ubuntu 22.04.4 LTS. I've installed docker-desktop using the .deb package on the docker website. Running docker context ls shows all the docker.sock files and the one currently in use/available on the system is denoted by *

➜  ~ docker context ls
NAME              DESCRIPTION                               DOCKER ENDPOINT                                  ERROR
default           Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                      
desktop-linux *   Docker Desktop                            unix:///home/user/.docker/desktop/docker.sock   

as you can see docker-desktop context is in use. Some applications try to access the default context /var/run/docker.sock like npm dockerode a quick fix to that is when creating an instance of dockerode use the current context path. For example:

const Docker = require("dockerode");
const docker = new Docker({ socketPath: '/home/user/.docker/desktop/docker.sock' });

and should fix the problem or alternatively change the context using the docker context use default command. If you wanna konw more docker-contexts here's the link to official docs: https://docs.docker.com/engine/context/working-with-contexts/

Depend answered 6/8 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.