Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot connect to the Docker daemon when running with sudo

Tags:

docker

macos

My Docker service is up and running. However when attempting to use Docker by running it with sudo, e.g.:

12:40:26/~ $ sudo docker pull fluxcapacitor/pipeline
Using default tag: latest

I have got the following error:

Warning: failed to get default registry endpoint from daemon (Cannot connect to 
the Docker daemon. Is the docker daemon running on this host?). Using system 
default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Note that I had already followed the answers on Mac OS X sudo docker Cannot connect to the Docker daemon. Is the docker daemon running on this host?

as follows:

docker-machine start default

12:40:36/~ $ docker-machine start default
Starting "default"...
Machine "default" is already running.

docker ps

12:41:20/~ $ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

So what more needs to be done?

This is:

$ docker --version
Docker version 1.11.2, build b9f10c9

on El Capitan.

Output of docker-machine env default

$ eval "$(docker-machine env default)"

$ docker-machine env default
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.100:2376"
export DOCKER_CERT_PATH="/Users/macuser/.docker/machine/machines/default"
export DOCKER_MACHINE_NAME="default"
like image 427
WestCoastProjects Avatar asked Jun 12 '16 19:06

WestCoastProjects


3 Answers

The following command exports a few environment variables that the subsequent docker commands use:

eval "$(docker-machine env default)"

However, if you launch docker with sudo, the exported environment variables are not accessible by the docker executable. You could potentially get it to work by passing -E flag to sudo, e.g.:

sudo -E docker pull fluxcapacitor/pipeline

But much easier option is to use docker without root like:

docker pull fluxcapacitor/pipeline
like image 88
khattam Avatar answered Oct 23 '22 17:10

khattam


You have to set environment variables with:

eval "$(docker-machine env default)"

More about it here.

like image 2
Alexander Guz Avatar answered Oct 23 '22 18:10

Alexander Guz


I had same probelem on my MAC, when attempted

# eval "$(docker-machine env default)"

got this error

Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.99.100:2376": x509: certificate is valid for 192.168.99.101, not 192.168.99.100
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.

to regenerate certificates, find out the docker-machines available;

# docker-machine ls

Output of avalable docker machines (omitted others)

NAME          ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
default       -        virtualbox   Running   tcp://192.168.99.100:2376           Unknown   Unable to query docker version: Get https://192.168.99.100:2376/v1.15/version: x509: certificate is valid for 192.168.99.101, not 192.168.99.100

Generate certificates for this default docker-machine

# docker-machine regenerate-certs default

and then setup docker-machine env to default docker-machine;

# eval "$(docker-machine env default)"

and it works normally after that.

like image 1
khawarizmi Avatar answered Oct 23 '22 17:10

khawarizmi