Cannot connect to the Docker daemon when running with sudo

15,060

Solution 1

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

Solution 2

You have to set environment variables with:

eval "$(docker-machine env default)"

More about it here.

Solution 3

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.

Solution 4

I have also tried the same but did not work.

later I have tried these steps on AWS CLI

$ sudo nano /etc/docker/daemon.json
{
"dns": ["8.8.8.8", "8.8.4.4"]
}
$ sudo service docker restart
$ docker pull hello-world
Share:
15,060
WestCoastProjects
Author by

WestCoastProjects

R/python/javascript recently and before that Scala/Spark. Machine learning and data pipelines apps.

Updated on June 18, 2022

Comments

  • WestCoastProjects
    WestCoastProjects almost 2 years

    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"