7

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.

Prashant Gupta
  • 788
  • 8
  • 26
Javed IN
  • 302
  • 1
  • 4
  • 12

3 Answers3

15

For security-wise, I go pro for GreatDharmatma and Pankaj Kumar.

If you want to skip the logging back and access instantly you can assign rw permission to other users

sudo chmod o+rw /var/run/docker.sock
Amartis Glady
  • 181
  • 1
  • 2
13

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)

Pankaj Kumar
  • 151
  • 1
  • 3
  • 9
GreatDharmatma
  • 627
  • 5
  • 12
  • 1
    Remember that the ability to run any Docker commands at all implies unlimited root access on the host; requiring `sudo` for it is not actually a bad idea. – David Maze Sep 17 '18 at 10:37
  • @DavidMaze Agreed. But it becomes a little cumbersome to use sudo and enter the password repeatedly. Adding user to the group is a more elegant method. – GreatDharmatma Sep 17 '18 at 10:38
2

Without changing the socket rights maybe just creating a new user node for nodejs then add him to the docker group

sudo useradd -g node docker

Minaro
  • 173
  • 2
  • 11