Running docker as sudo

I installed boot2docker and I ran:

./boot2docker start
 export DOCKER_HOST=tcp://localhost:4243

I started a tutorial from the Docker site.

I don't understand one thing - when I run:

sudo docker info
==>
Get http:///var/run/docker.sock/v1.11/info: dial unix /var/run/docker.sock: no such file or directory

However when I run without sudo it works:

MacCris:bin cristianc$ docker info
Containers: 2
Images: 10
Storage Driver: aufs
 Root Dir: /mnt/sda1/var/lib/docker/aufs
 Dirs: 14
Execution Driver: native-0.2
Kernel Version: 3.14.1-tinycore64
Debug mode (server): true
Debug mode (client): false

Any idea why running with sudo as in the tutorial does not work (at least for me)?


Solution 1:

The DOCKER_HOST environment variable isn't set. You can confirm this by doing:

> env

Then as sudo:

> sudo env

Without the DOCKER_HOST variable set, docker can't connect to the daemon.

Solution 2:

Try:

sudo DOCKER_HOST=$DOCKER_HOST docker run

This way the required environment variable will be available with sudo.

Solution 3:

If you want the DOCKER_HOST variable from your profile to be available when you run docker command with sudo, then you can add the following line to sudoers file. Open with:

sudo visudo

Add:

Defaults        env_keep += "DOCKER_HOST"