219

I am getting an error message saying I can't connect to the docker daemon. I have looked into other people's answers who have had similar issues but it hasn't helped. I am running the version of Ubuntu 15.10. I will try to provide all the info I have.

root@# docker-compose -f docker-compose-deps.yml up -d
ERROR: Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.

Docker Version

root@# sudo docker     version
Client:
Version:      1.9.1
API version:  1.21
Go version:   go1.4.2
Git commit:   a34a1d5
Built:        Fri Nov 20 13:20:08 UTC 2015
OS/Arch:      linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Docker-Compose Version

root@# docker-compose --version
docker-compose version 1.5.2, build 7240ff3

This is what happens if I try to stop or start the service...

root@# sudo service docker stop
stop: Unknown instance: 
root@# sudo service   docker start
docker start/running, process 5375

If I run ps aux | grep docker

root@# ps aux | grep docker
root      4233  0.0  0.0  13692  2204 pts/15   S+   10:27   0:00 grep --color=auto docker

Any help would be greatly appreciated. Let me know if you may need anymore information.

daveskylark
  • 2,724
  • 4
  • 16
  • 30
  • 2
    Please do not vandalize your posts. Once you've posted a question, you have licensed the content to the Stack Overflow community at large (under the CC-by-SA license). If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](http://meta.stackoverflow.com/questions/323395/what-is-the-proper-route-for-a-dissociation-request). – Bugs Jul 27 '17 at 15:00

23 Answers23

548

I had the same error, after 15 min of debugging. Turns out all it needs is a sudo :)

Check out Manage Docker as a non-root user to get rid of the sudo prefix.

Alexandre Huat
  • 806
  • 10
  • 16
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83
  • 74
    They need a better error =/ This question has 13,000 views – Jonathan Apr 05 '17 at 22:18
  • 6
    the real fix is creating the docker group, while the `sudo` is just a workaround/hack, in my opinion. as the group creating part is not explained here, only a link is given, and the content of that link changed, i consider this a link-only-answer, and it is now essentially useless (for the real fix part). could you maybe add the description of how to add that group? – hoijui Sep 05 '17 at 07:34
  • 5
    I think ``sudo`` is a bad idea as well. See the Docker docs for managing Docker as a non-root user: https://docs.docker.com/engine/installation/linux/linux-postinstall/#manage-docker-as-a-non-root-user – jwhitlock Nov 02 '17 at 15:20
  • 11
    Basically, all you need to do is `sudo usermod -aG docker $USER`, and it will work afterwards. – user2340939 Jan 11 '18 at 15:32
  • Worked for me with Win10 + vagrant + hyperv! – Niklas Rosencrantz May 23 '18 at 11:16
  • 1
    Don't forget this:Log out and log back in so that your group membership is re-evaluated. If testing on a virtual machine, it may be necessary to restart the virtual machine for changes to take effect. On a desktop Linux environment such as X Windows, log out of your session completely and then log back in. – Suge May 14 '19 at 04:31
  • @Jonathan About 60K now! – ShahinSorkh Jun 05 '19 at 07:41
169

I had this problem and did not want to mess things up using sudo. When investigating, I tried to get some info :

docker info

Surprinsingly, I had the following error :

Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http:///var/run/docker.sock/v1.38/info: dial unix /var/run/docker.sock: connect: permission denied

For some reason I did not have enough privileges, the following command solved my problem :

sudo chown $USER /var/run/docker.sock

Et voilà !

madjaoue
  • 5,104
  • 2
  • 19
  • 31
  • How about after restarting docker again? We have to do this chown again? – Light.G Oct 24 '18 at 12:26
  • No, it should work when you restart docker. But I'm not sure what happens if you restart your machine. – madjaoue Oct 24 '18 at 12:45
  • By default, Docker would hold `docker.sock` with a non-root group, `docker` as default. But we could change that by `-G` flag during Docker daemon’s initiating. In my case, I add a group for `$USERNAME`, means add docker group, then reboot my machine(ubuntu 16.04, PC). It works well. And this is recommended in docs of docker. – Light.G Oct 25 '18 at 01:20
  • 5
    Works for me too :) – Kamil J. Sep 10 '19 at 16:06
  • 1
    Have to do this after each reboot :( – Adam Aug 31 '20 at 20:16
  • 2
    @Adam as mentioned in [Docker doc](https://docs.docker.com/engine/install/linux-postinstall/), you can solve this with (1st) `sudo groupadd docker` then (2) `sudo usermod -aG docker $USER` – madjaoue Oct 08 '20 at 09:46
  • OK answer, but `sudo docker-compose up` is much simpler, I think. Note: in order to see what `sudo chown $USER /var/run/docker.sock` does, both before and after the `chown` command I ran `ls -l /var/run/docker.sock`. - Result _before_: . `srw-rw---- 1 root docker 0 Oct 29 06:39 /var/run/docker.sock` . - Result _after_: . `srw-rw---- 1 ubuntu docker 0 Oct 29 06:39 /var/run/docker.sock` . **Conclusion:** the `chown` command changed the owner of `/var/run/docker.sock` from `root` to `$USER` (in my case `ubuntu`). New problem: `fetch http://dl-cdn.alpinelinux[...]` seemingly takes forever. SIGH! – Henke Oct 29 '20 at 07:40
  • nice solution in my problem – harmain Apr 11 '22 at 09:42
43

It appears your issue was created by an old Docker bug, where the socket file was not recreated after Docker crashed. If this is the issue, then renaming the socket file should allow it to be re-created:

$ sudo service docker stop
$ sudo mv /var/lib/docker /var/lib/docker.bak
$ sudo service docker start

Since this bug is fixed, most people getting the error Couldn't connect to Docker daemon are probably getting it because they are not in the docker group and don't have permissions to read that file. Running with sudo docker ... will fix that, but isn't a great solution.

Docker can be run as a non-root user (without sudo) that has the proper group permissions. The Linux post-install docs has the details. The short version:

$ sudo groupadd docker
$ sudo usermod -aG docker $USER
# Log out and log back in again to apply the groups
$ groups  # docker should be in the list of groups for your user
$ docker run hello-world  # Works without sudo

This allows users in the docker group to run docker and docker-compose commands without sudo. Docker itself runs a root, allowing some attacks, so you still need to be careful with what containers you run. See Docker Security Documentation for more details.

jwhitlock
  • 4,572
  • 4
  • 39
  • 49
  • This question is specific to the Linux install, and the fix involves changes on the Linux host system to fix it. You'll get better information if you post a similar question with the same detail but for Windows. To solve yourself, I recommend a close reading of the [Docker for Windows documentation](https://docs.docker.com/docker-for-windows/) – jwhitlock May 24 '18 at 12:42
16

I had the same issue. After taking notes and analyzing some debugging results, finally, I solved what can be the same error. Start the service first,

service docker start

Don't forget to include your user to the docker group.

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
R A
  • 161
  • 1
  • 4
  • I was running as root when I encountered this error. Seemed like `sudo` fixed it for others -_- Glad to find that it was indeed this, that I forgot to do. So simple! – Will Mar 25 '18 at 04:56
13

just try with sudo. It seems like permission issue!

sudo docker-compose -f docker-compose-deps.yml up -d

it worked for me.

Demobilizer
  • 663
  • 9
  • 12
12

One way to resolve this would be to first add your user to the docker group by running the following

sudo usermod -aG docker $USER

IMPORTANT: Remember to log out of your system (not just your terminal) and back in for this to take effect!

arunavkonwar
  • 887
  • 9
  • 10
11

You should adding your user to the "docker" group with something like:

sudo usermod -aG docker ${USER}

Contropist
  • 401
  • 4
  • 9
9

in my case it is because the ubuntu permission,

  1. List item

check permission by

docker info 

if they print problem permission, enter image description here then use

sudo chmod -R 777 /var/run/docker.sock
joe-khoa
  • 465
  • 1
  • 5
  • 10
  • 1
    "chmod 777" is never the correct solution. If it is, you have one-in-a-thousand exception that confirms this rule. – Richlv May 24 '21 at 11:25
8

I got this error when there were files in the Dockerfile directory that were not accessible by the current user. docker could thus not upload the full context to the daemon and brought the "Couldn't connect to Docker daemon at http+docker://localunixsocket" message.

cweiske
  • 30,033
  • 14
  • 133
  • 194
  • 2
    Thanks! This solved it for me. Easy to check, since all docker commands worked, but docker-compose did not. Also docker-compose worked in another directory. Very misleading error message, BTW. – Csongor Fagyal Aug 30 '17 at 21:37
  • 1
    This should really be higher up – Arlen Anderson Jan 09 '18 at 19:25
  • 1
    Thanks! worked for me. Using `docker build .` will yield any permission problems. – everyman Feb 05 '18 at 15:27
  • Only thing that worked - why are Docker error messages so bad? Thanks! – coderMe Feb 11 '20 at 13:36
  • this solved my problem too. when I checked my folders, I saw some files/folders that is owned by another user. once I fixed the issue by chowning file's user/group, that solved my problem. – spetsnaz Oct 09 '21 at 19:22
3

From the output of "ps aux | grep docker", it looks like docker daemon is not running. Try using below methods to see what is wrong and why docker is not starting

  1. Check the docker logs

$ sudo tail -f /var/log/upstart/docker.log

  1. Try starting docker in debug mode

$ sudo docker -d -D

Manish R
  • 2,312
  • 17
  • 13
3

Another reason why this error can show up: for me it was a malformed image-path definition in the docker-compose.yml:

  service:
    image: ${CONTAINER_REGISTRY_BASE}/my-service
   ...

Lookis ok'ish first, but i had CONTAINER_REGISTRY_BASE=eu.gcr.io/my-project/ set on the env. Apparently the // in the image path caused this error.

docker-compose: v.1.21.2
docker: 18.03.1-ce
leberknecht
  • 1,526
  • 15
  • 27
2

In my case your docker service might be stopped

Command to start docker service:

$ sudo systemctl start docker

Command to verify if it start:

$ sudo docker run hello-world

John Jang
  • 2,567
  • 24
  • 28
2

I used Ubuntu 16.04 and found this problem too when I used docker-compose. I fixed it by running this command.

$ sudo systemctl start docker
$ sudo docker-compose build
KHACHORNCHIT
  • 2,222
  • 23
  • 19
1

Is there slight possibility you deleted default machine? But, first check if all files are there (OSX, similar on other systems)

brew install docker docker-compose docker-machine xhyve docker-machine-driver-xhyve
brew link docker docker-compose docker-machine xhyve docker-machine-driver-xhyve

sudo chown root:wheel /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
sudo chmod u+s /usr/local/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve

Also, install Docker App, as it much easier to maintain containers:

brew cask reinstall docker

ans start Docker app from finder (wait until service is fully started)

Then, check instalation with:

docker-machine ls

if no machines are present in list, create one and start it:

docker-machine create default
docker-machine start default

After this, build, compose and all other commands should work properly.

rapttor
  • 396
  • 3
  • 7
1

Final Solution: First run this command: sudo chown $USER /var/run/docker.sock Then run this command: docker-compose up -d --build

Ashraf Gardizy
  • 357
  • 4
  • 9
0

In my case a have the same error when I try to docker-compose build and my solution was just add sudo

sudo docker-compose build
Andrey Topoleov
  • 1,591
  • 15
  • 20
0

For me the fix was to install a newer version (1.24) of docker-compose using this article.

The previous version (1.17) was installed from ubuntu's default repository, but after installing a newer version I managed to launch the container. Hope it helps somebody.

Artem Ilin
  • 353
  • 2
  • 19
0

I think it's because of right of access, you just have to write

sudo docker-compose-deps.yml up
0

In my case problem was with the inappropriate image tag name -backend - started with a short leading dash:

FAILED:

version: '2.4'

services:
  my-service:
    container_name: my.backend
    image: imagename:-backend
    build:
      context: .
    

Error message:

ERROR: Couldn't connect to Docker daemon - you might need to run `docker-machine start default`.

WORKS: with my-backend

version: '2.4'

services:
  my-service:
    container_name: my.backend
    image: imagename:my-backend
    build:
      context: .
    
Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34
0

It helped me to sudo chown -Rv someuser.someuser ~someuser/docker_compose_dir/, where someuser is the user I run docker-compose under. After that, docker-compose went smoothly.

Eugene Gr. Philippov
  • 1,908
  • 2
  • 23
  • 18
0

Worth adding here that if you have docker-desktop installed you can run:

docker context list

This should give you the docker url for the engine running with docker-desktop.
You can then set the DOCKER_HOST environment variable for compose to use.
In my case it was by adding it at the bottom of ~/.bashrc:

export DOCKER_HOST="unix:///home/rouanubuntu/.docker/desktop/docker.sock"

This points docker-compose to the engine running with the desktop UI which is useful.

Then run source ~/.bashrc or relogin to load it.

0

For me it was a missing environment variable - reading the WARNING actually helped.. In case it also helps somebody else:

docker-compose.yml:

version: '2.4'

services:
  app:
    image: my/app:${BUILD}
    build:
      args:
        GIT_BUILD_INFO: ${GIT_BUILD_INFO}

Bash history in Ubuntu 20.04 to replicate the issue:

$ docker-compose -p energycontrol -f docker-compose-jenkins.yml build

WARNING: The BUILD variable is not set. Defaulting to a blank string.
WARNING: The GIT_BUILD_INFO variable is not set. Defaulting to a blank string.
Building app
ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?

If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable.
$ sudo service docker status
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-06-20 11:31:14 CEST; 4min 9s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 2504 (dockerd)
      Tasks: 56
     Memory: 268.3M
     CGroup: /system.slice/docker.service
             └─2504 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

Jun 20 11:31:30 dozer dockerd[2504]: time="2023-06-20T11:31:30.901226992+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:31:35 dozer dockerd[2504]: time="2023-06-20T11:31:35.860433546+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:31:43 dozer dockerd[2504]: time="2023-06-20T11:31:43.235052163+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:31:53 dozer dockerd[2504]: time="2023-06-20T11:31:53.702991779+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:32:08 dozer dockerd[2504]: time="2023-06-20T11:32:08.434441406+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:32:35 dozer dockerd[2504]: time="2023-06-20T11:32:35.534456987+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:33:28 dozer dockerd[2504]: time="2023-06-20T11:33:28.203608203+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:34:30 dozer dockerd[2504]: time="2023-06-20T11:34:30.647314927+02:00" level=info msg="ignoring event" container=1c188fbed9835609677392e4e8ab14f0d0ffefce0cb5ed29362377c05c3ffc67 module=libcontainerd namespace=moby topic=/tasks/delete type="*events.TaskDelete"
Jun 20 11:34:46 dozer dockerd[2504]: time="2023-06-20T11:34:46.218537851+02:00" level=error msg="Handler for POST /v1.35/build returned error: invalid reference format"
Jun 20 11:35:12 dozer dockerd[2504]: time="2023-06-20T11:35:12.058911161+02:00" level=error msg="Handler for POST /v1.35/build returned error: invalid reference format"

After setting the environment variables, everything worked out:

$ export BUILD="temp"
$ export GIT_BUILD_INFO="something"
$ docker-compose -f docker-compose.yml build
Building app
[...]
Successfully built f0b57a1cfbaa
spiegelm
  • 65
  • 7
-3

I found this and it seemed to fix my issue.

GitHub Fix Docker Daemon Crash

I changed the content of my docker-compose-deps.yml file as seen in the link. Then I ran docker-compose -f docker-compose-deps.yml up -d. Then I changed it back and it worked for some reason. I didn't have to continue the steps in the link I provided, but the first two steps fixed the issue for me.

daveskylark
  • 2,724
  • 4
  • 16
  • 30
  • 2
    ubuntu 16.04 : just add sudo: sudo docker-compose up -d – Moccine Feb 04 '18 at 00:05
  • It’s not enough as an answer. If you want to accept your own answer, you must put up the very root reason of your issue. Otherwise, it would be a real misleading for others. BTW, there are other answers here, did they help? Since I did not see any comments by you. – Light.G Oct 24 '18 at 12:32