Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 575
abhi Avatar asked Sep 04 '26 06:09

abhi


1 Answers

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...]
like image 150
Alexis.Rolland Avatar answered Sep 06 '26 19:09

Alexis.Rolland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!