Search code examples
node.jsdockerubuntu

After executing following code of dockerode npm getting error "connect EACCES /var/run/docker.sock" on ubuntu 14.04


I am executing following code to get list of all container using npm dockerode on Ubuntu 14.04 machine. Docker container are running properly.

var Docker = require('dockerode');
var docker = new Docker({socketPath: '/var/run/docker.sock'});

docker.listContainers({all: true}, function(err, containers) {
console.log('err ' + err);
console.log('ALL: ' + containers);
});

But getting

Error connect EACCES /var/run/docker.sock

thanks in advance.


Solution

  • This happens because you don't have sufficient permissions to access Docker. There are two solutions:

    1. Run the command with sudo. (not recommended)

    2. Add current user to the docker group using this command:

      $ sudo usermod -aG docker $USER
      $ newgrp docker
      

    Logout and log back in once you run this command and try running your code again. (recommended)