Search code examples
dockerdocker-composedockerfiledocker-swarmdocker-machine

ERROR: Could not connect to Docker daemon at http+docker://localhost - is it running?


This is my dockerfile


FROM ubuntu:latest

RUN apt-get update \
    && apt-get install -y git

RUN mkdir api 

WORKDIR ./api

RUN git clone --branch develop https://link

WORKDIR ./api/api/


RUN apt-get install -y docker.io
RUN apt-get -y install curl


RUN curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)"  -o /usr/local/bin/docker-compose
RUN mv /usr/local/bin/docker-compose /usr/bin/docker-compose
RUN chmod +x /usr/bin/docker-compose



RUN docker-compose up 


I want to docker-compose up inside docker image. However,

It gives ERROR: Couldn't connect to Docker daemon at http+docker://localhost - is it running? and If it's at a non-standard location, specify the URL with the DOCKER_HOST environment variable

How can I solve this problem I searched but none of them worked?


Solution

  • I'd suggest rethinking the entire approach of this Dockerfile: you can't run the Docker daemon in a Dockerfile and you can't start any sort of background process. A shell script that runs on the host might be a better match.

    Running any sort of daemon inside a Dockerfile mostly doesn't work; at the end of each RUN instruction all running processes are terminated. Creating a Docker image doesn't preserve any running processes, just the final filesystem and metadata like the default CMD to run when you start a container. So even if docker-compose up worked, the results of that wouldn't be persisted in your image.

    Running a Docker daemon inside a Docker container is difficult and generally discouraged. (Sharing the host's Docker socket has significant security implications but is the preferred approach.) Either way requires some additional permissions, that again just aren't available inside a Dockerfile.

    The other red flag for me here is the RUN git clone line. Because of Docker's layer caching, it will be happy to say "oh, I've already RUN git clone so I don't need to repeat that step" and you won't wind up with current code. Feeding credentials for remote git repositories into a Dockerfile is also tricky. I'd also recommend running source control commands exclusively on the host and not a Dockerfile.

    The standard approach here would be to commit a docker-compose.yml file to the top of your repository, and run git clone and docker-compose up directly from the host. You can't use a Dockerfile as a general-purpose automation tool.