255

I installed docker-machine 0.1.0 and docker-compose 1.1.0 on Mac OS 10.8.5.
Docker-machine is running normally and able to connect by docker-machine ssh.

$ docker-machine ls
NAME   ACTIVE   DRIVER       STATE     URL                         SWARM
dev    *        virtualbox   Running   tcp://192.168.99.100:2376   

However can't connect from docker-compose.

$ docker-compose up

Couldn't connect to Docker daemon at http+unix://var/run/docker.sock - is it running?

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

My Dockerfile and docker-compose.yml is here.

Dockerfile

FROM centos:centos7
DOCKER_HOST tcp://192.168.99.100:2376

docker-compose.yml

web:
  build: .

Why can't connect? Any ideas?

6
  • 85
    Does docker-machine run as root? I just came here with the same problem on Ubuntu and it was the permissions on the socket - sudo did the trick.
    – SLD
    Mar 17, 2015 at 14:49
  • 9
    For those on Linux, you might just need to be added to the docker group.
    – Batandwa
    Mar 2, 2016 at 1:29
  • 12
    just run : sudo docker-compose up May 10, 2018 at 12:13
  • 1
    I had the same issue. You need to restart docker. sudo service docker restart or sudo service docker start I solved it with this. May 7, 2020 at 17:50
  • 1
    For those using Ubuntu, you should use sudo and you should not add users to the docker group. View the comments in the accepted answer: askubuntu.com/questions/477551/…
    – kas
    May 31, 2020 at 7:32

34 Answers 34

350

Simple solution for me: sudo docker-compose up


UPDATE 2016-3-14: At some point in the docker install process (or docker-compose ?) there is a suggestion and example to add your username to the "docker" group. This allows you to avoid needing "sudo" before all docker commands, like so:

~ > docker run -it ubuntu /bin/bash
root@665d1ea76b8d:/# date
Mon Mar 14 23:43:36 UTC 2016
root@665d1ea76b8d:/# exit
exit
~ > 

Look carefully at the output of the install commands (both docker & the 2nd install for docker-compose) and you'll find the necessary step. It is also documented here: https://subosito.com/posts/docker-tips/

Sudo? No!

Tired of typing sudo docker everytime you issue a command? Yeah, there is a way for dealing with that. Although naturally docker is require a root user, we can give a root-equivalent group for docker operations.

You can create a group called docker, then add desired user to that group. After restarting docker service, the user will no need to type sudo each time do docker operations. How it looks like on a shell commands? as a root, here you go:

> sudo groupadd docker
> sudo gpasswd -a username docker
> sudo service docker restart 

Done!

UPDATE 2017-3-9

Docker installation instructions have been updated here.

Post-installation steps for Linux

This section contains optional procedures for configuring Linux hosts to work better with Docker.

Manage Docker as a non-root user

The docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The docker daemon always runs as the root user.

If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

To create the docker group and add your user:

# 1. Create the docker group.
$ sudo groupadd docker

# 2. Add your user to the docker group.
$ sudo usermod -aG docker $USER

# 3. Log out and log back in so that your group membership is re-evaluated.

# 4. Verify that you can run docker commands without sudo.
$ docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

4
  • 1
    Likewise for some reason sudo is the only way to get it working :O :O :O Mar 14, 2016 at 15:54
  • 23
    Instead of logging out ... you can use command: newgrp docker Nov 6, 2017 at 2:35
  • 1
    Re: "Simple solution for me: sudo docker-compose up". When I try that, I get: sudo: docker-compose: command not found
    – Mike B
    May 7, 2020 at 20:54
  • this works for me. after adding my user to the group docker I could run applications. Feb 1, 2021 at 14:23
103

The Docker machine is running. But you need to export some environment to connect to the Docker machine. By default, the docker CLI client is trying to communicate to the daemon using http+unix://var/run/docker.sock (as shown in the error message).

Export the correct environment variables using eval $(docker-machine env dev) and then try again. You can also just run docker-machine env dev to see the environment variables it will export. Notice that one of them is DOCKER_HOST, just as the error message suggests you may need to set.

3
  • 3
    You can put that command (eval "$(docker-machine env default)") in your bin/postactivate to ensure it's always up-to-date.
    – Dannid
    Feb 26, 2016 at 17:20
  • 1
    Where do I find the file bin/postactivate ?
    – lszrh
    Mar 17, 2016 at 18:37
  • 1
    Or you forgot to install docker: $ eval $(docker-machine env dev) Command 'docker-machine' not found, but can be installed with: sudo snap install docker
    – R OMS
    Oct 29, 2019 at 2:14
71

If you started docker using sudo , then you should run docker-compose up with sudo Like: sudo docker-compose up

1
  • 1
    It worked for me when I ran sudo docker-compose build instead of docker-compose build. Jun 4, 2020 at 14:38
65

I solved the issue in Ubuntu 20.0.4 by

sudo chmod 666 /var/run/docker.sock

and then

sudo service docker start && docker-compose up -d

Ref.

5
  • 6
    Wrong answer. Do more research. You are giving everyone access to the socket. askubuntu.com/questions/477551/…
    – Benjamin
    Oct 8, 2020 at 2:25
  • 2
    @LiveWireBT It's the solution, has solved the problem. Could explain what's the best practice here?
    – M. Rostami
    Oct 13, 2020 at 12:37
  • Thank you very much. I was starting to pull out my hair
    – M4X_
    Jan 19, 2021 at 21:09
  • This solved my issue is 666 a bad thing for the socket ?
    – Olivier JM
    Jan 21, 2021 at 21:38
  • this solved my issue thanks Jan 25, 2022 at 9:53
32

When you get an Error:

ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running?

The Solution would be:

  1. First, try verifying Docker Service is Up and Running as expected on your remote/local machine:sudo service docker status

    if Not - run sudo service docker start or sudo systemctl start docker (depends on some linux versions, see Docker get started instructions).

  2. Second, start docker as sudo sudo docker-compose up

0
31

By default the docker daemon always runs as the root user, therefore you need to prepend sudo to your Docker command(s).

If you don’t want to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.

To create the docker group and add your user:

  1. Create the docker group.

    $ sudo groupadd docker

  2. Add your user to the docker group.

    $ sudo usermod -aG docker $USER

  3. Log out and log back in so that your group membership is re-evaluated.

  4. Verify that you can docker commands without sudo.

    $ docker run hello-world

This command downloads a test image and runs it in a container. When the container runs, it prints an informational message and exits.

The steps outlined above comes from the official Docker documentation.

3
  • How we can do this on windows installed Docker ?
    – Jeff Cook
    Sep 9, 2018 at 14:07
  • Besides that, what I did to fix docker-compose was setting the user executing docker-compose as the owner of /usr/local/bin/docker-compose and then restart de service
    – Sam
    Apr 15, 2019 at 15:03
  • group membership can be refreshed with su - $USER to save logging off & on Jun 5, 2019 at 11:50
19

sudo systemctl start docker - to start the Docker service.

sudo docker-compose up after that.

I have Fedora 26, and trying to solve the same issue I eventually entered Docker Compose on Fedora Developers' page and then Docker on Fedora Developers' page, which helped me.

Probably, docker service considered by community to start with the system and run in background all the time, but for me it was not so obvious, and that's the reason I can think of why there's no popular answer like this one.

On the Fedora Developers' page there's instruction how to enable Docker to start with the system:

sudo systemctl enable docker

1
  • Replace systemctl with launchctl if you're running on mac. Nov 15, 2019 at 10:28
12

a quick way to solve this

Just add sudo before your docker command, like


sudo docker-compose up

9

if you are using linux

try sudo docker-compose up

8

Run this as a root. sudo docker-compose up --build

1
  • worked on Ubuntu 20.0 . Thank you Sep 28, 2021 at 14:32
7

If you are on Linux you may not have docker-machine installed since it is only installed by default on Windows and Mac computers.

If so you will need to got to: https://docs.docker.com/machine/install-machine/ to find instructions for how to install it on your version of Linux.

After installing, retry running docker-compose up before trying anything listed above.

Hope this helps someone. This worked for my devilbox installation in Fedora 25.

5

while running docker-compose pull - i was getting below error

ERROR: Couldn't connect to Docker daemon at http+docker://localhost

is it running?

solution -

sudo service docker start 

issue resolved

5

I know it's silly, but in my case I just tried with sudo and it worked like a charm.

in your case, just try: $ sudo docker-compose up

5

Easy way to fix this issue:
Question:
enter image description here

Solution:

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

Answer:
enter image description here

4

Anyone checked log ?

In my case error message in /var/log/upstart/docker.log was:

Listening for HTTP on unix (/var/run/docker.sock) 
[graphdriver] using prior storage driver "aufs" 
Running modprobe bridge nf_nat failed with message: , error: exit status 1 
Error starting daemon: Error initializing network controller: Error creating default "bridge" network: can't find an address range for interface "docker0" 

Worth to mentioned I had vpn turned on, so: $ sudo service openvpn stop $ sudo service docker restart $ docker-compose up|start $ sudo service openvpn start was the solution.

1
  • Yeah, this was the case for me too, but in case someone doesn't like this solution, here are some other solutions as well which do not require stopping/restarting the vpn service: stackoverflow.com/questions/43720339/…
    – aderchox
    Aug 4, 2020 at 8:35
4

if you are using docker-machine then you have to activate the environment using env variable. incase you are not using docker-machine then run your commands with sudo

4

Apart from adding users to docker group, to avoid typing sudo repetitively, you can also create an alias for docker commands like so:

alias docker-compose="sudo docker-compose"
alias docker="sudo docker"
0
4

There might be privilege issues, Following command work out for me.

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

Thanks to Max for solution: https://stackoverflow.com/posts/60282354/revisions

3

Answer from @srfrnk works for me.

In my situation, I had the next docker-compose.yml file:

  nginx:
    build:
      context: ./
      dockerfile: "./docker/nginx.staging/Dockerfile"
    depends_on:
      - scripts
    environment:
      NGINX_SERVER_NAME: "some.host"
      NGINX_STATIC_CONTENT_OPEN_FILE_CACHE: "off"
      NGINX_ERROR_LOG_LEVEL: debug
      NGINX_BACKEND_HOST: scripts
      NGINX_SERVER_ROOT: /var/www/html
    volumes:
      - ./docker-runtime/drupal/files:/var/www/html/sites/default/files:rw
    ports:
      - 80:80

./docker-runtime owner and group - is root when the other files owner - my user. When I tried to build nginx

Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running?

I added ./docker-runtime to .dockerignore and it is solved my problem.

3
1 . sudo service docker stop 
2 . sudo service docker status
3 . sudo service docker start
4 . docker-compose build <app_name\service_name>
1
  • steps 1-3 and then also docker-compose pull worked for me. Sep 21, 2021 at 0:53
2

I've had the same symptoms.

Only diff that it happened only during docker-compose build docker ps worked. Happened with version 2.x as well as 3.x. Restarted docker service, then the machine... Even re-installed docker + docker-compose.

Tried everything but nothing helped.

Finally I tried building the Dockerfile "manually" by using docker build.

Apparently I had a permission issue on a file/folder inside the Docker context. It was trying to read the context when starting the build and failed with a proper error message. However this error message did not propagate to docker-compose which only shows Couldn't connect to Docker daemon at http+unix://var/run/docker.sock - is it running?

Having found that the solution was simply adding the file/folder to the .dockerignore file since it wasn't needed for the build. Another solution might have been to chown or chmod it.

Anyway maybe this could help someone coming across the same issue that really has nothing to do with docker and the misleading error message being displayed.

1
  • I had the same problem: file permissions in a mounted volume. In my case I had an SSL cert that was owned by root that had been put in a Docker volume. chown / chmod fixed it. Terrible error message but they're working on it. See github.com/docker/compose/issues/5318
    – bcattle
    Mar 21, 2018 at 18:38
2

$sudo docker-compose up

I did follow the steps as it is in the above answer to add $USER to group docker. i didn't want to add a new group docker because in my docker installation a group named docker automatically created.

Docker CE is installed and running. The docker group is created but no users are added to it. You need to use sudo to run Docker commands. Continue to Linux postinstall to allow non-privileged users to run Docker commands and for other optional configuration steps.

but using docker-compose up didn't work either. It gave the same previous error. So in my case(Ubuntu 18.10) sudo docker-compose up fixed the issue.

ps: @Tiw thanks for the advice.

1
  • 1
    Maybe it's better put the solution first, and those explaining below it. Also you can make use those buttons on the editor to format your answer better.
    – Til
    Mar 3, 2019 at 5:18
1

try this:

sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

1

I have resolved the issue using following steps

$ sudo service docker status
$ sudo service docker start
$ sudo docker-compose up

now docker-compose up is working

1

logging out then in again helped me. In my case it was fresh install, I created a new user, new group then added the user to the docker group.

exit exit

then ssh into the server again

1

WORKING!!
I tried with below commands and its working

service docker restart
docker-compose -f docker_compose.yaml down
docker-compose -f docker_compose.yaml up
1

A Solution that worked with me was to change the image tag to lowercase letters.

change the image tag in docker-compose.yml file to include repository IP and image name in lowercase letters.

e.g : image: 192.168.1.5/imagename:v1

2
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 16, 2022 at 16:16
  • Since I cannot edit at the moment: In the docker-compose.yml, one of the fields under a service is called image, which tells docker-compose what name to give to the docker image of the service when building. If you put uppercase letters on the image field, Docker tells you that it can't connect to the Docker daemon, which is a very misleading message. You can check what the problem actually is by running sudo service docker status Feb 22, 2023 at 14:14
1

sudo dockerd started the docker service in my case, I was using wsl2 in windows.

0

For me, I started upgrading Docker and cancelled midway. I didn't notice that Docker was not running (if it was, there is an icon at the top nav bar of my Macbook, by the battery remaining, time, etc). Once I launched it, and finished the upgrade, docker-compose up worked again!

0

My setup has got two cases for this error:

  • __pycache__ files created by root user after I run integration tests inside container are inaccessible for docker (tells you original problem) and docker-compose (tells you about docker host ambiguously);
  • microk8s blocked my port until I stopped it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.