17

I have this React app that I want to run on an Apache HTTP Docker container.

So I created a Dockerfile that works with sudo docker build and sudo docker run <name>

FROM httpd:2.4
COPY ./dist/ /usr/local/apache2/htdocs/

I created a docker-compose.yml

version: '3'
services:
  frontend:
    build: .
    ports:
     - "8080:80"
    container_name: frontend

But when I run sudo docker-compose build I get this error:

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.

What is the problem?

Victor Ferreira
  • 6,151
  • 13
  • 64
  • 120

7 Answers7

57

Check your privileges, Following command solved my problem :

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

It happens when you try to start docker as non super user and it couldn't get access to it own sockets.

Max Sherbakov
  • 1,817
  • 16
  • 21
19

run with sudo:

sudo docker-compose up -d

max3d
  • 1,437
  • 15
  • 16
  • 6
    Instead of using `sudo` follow: https://docs.docker.com/install/linux/linux-postinstall/ – itsazzad Nov 27 '19 at 13:36
  • 1
    Following the link mentioned by itsazzad, don't forget to logout and login again to recognize the new group membership. – dougB Jun 06 '20 at 04:07
7

My solution was to simply restart the box (EC2 Amazon Linux2). Chased my tail for 30 mins with permissions and groups, but a simple reboot did the trick.

semtex41
  • 249
  • 1
  • 7
  • Thank you for your sugestion. I was also chasing my tails for hours, and after rebooting the server it started to work. Why any tutorial or readme file don't say to restart? – dchang Nov 07 '18 at 17:52
  • Thank god I saw your answer. I spent the last 2 hours trying to figure out what was wrong. Just needed to restart as well. Thanks a tonne. – abhijeetviswa May 23 '20 at 16:50
6

Before running any Docker command, you need to run the Docker daemon on the host machine:

sudo systemctl start docker
brianolive
  • 1,573
  • 2
  • 9
  • 19
  • it is running. I can run any Docker command, like `ps`, `build` etc. And i can run docker-compose in another project right now. But when I run only docker-compose in this HTTPD project, I get this error. – Victor Ferreira Jun 22 '18 at 01:42
  • I see. It is likely a permissions issue then. See [here](https://github.com/docker/compose/issues/4181) – brianolive Jun 22 '18 at 01:49
  • same symptom in a Jenkins environment. docker commands work, but docker-compose failes with the message in the subject. As user jenkins from the CLI I can do all docker commands (via /var/run/docker.sock), but calling docker-compose from the Jenkins job will cause the problem. – rhoerbe Jun 28 '18 at 16:57
  • @rhoerbe how did you fix your issue with the Jenkinsfile? – Vaibhav Desai Apr 03 '19 at 22:10
  • On Debian 11 works. – EsmaeelE Jun 20 '22 at 07:51
6

I had this, and discovered that my user was not in the docker group, and so didn't have permission.

You can check this by running groups. If docker isn't in the list that's printed, this may be your problem.

You can add your user to the docker group with:

sudo usermod -aG docker yourUserName

After adding my user to the docker group, I had to log out and back in again for the changes to take effect.

EDIT: I did this on a fresh install, and discovered that I needed to reboot before it worked. If you're not sure why it failed, try rebooting!

meshy
  • 8,470
  • 9
  • 51
  • 73
1

I've had the same error, but in my case it was an invalid docker-compose file: image name was my_app:. E.g

  my_app:
    build:
      context: .
      dockerfile: ./my_app/Dockerfile
    image: my_app:

The error is very unhelpful and took me a long time to figure out, as it was not explicitly written like that in the file, but instead the part after : was an empty variable.

Leaving this here in case someone else stumbles on this issue.

Justas
  • 811
  • 1
  • 11
  • 22
0

for me helped

export DOCKER_HOST=127.0.0.1:2375
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88