57

This is my Dockerfile:

FROM golang
# RUN cat /etc/*release
RUN apt-get update
RUN apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

RUN apt-get update
RUN apt-get -y install docker-ce
RUN docker run hello-world

The golang Dockerfile is official, it bases on the

Debian GNU/Linux 8 (jessie)

So I wrote down this Dockerfile by checking the install steps from Docker Install Tutor(Debian)

But the output is

Step 8/8 : RUN docker run hello-world
 ---> Running in b183b8cc5d10
docker: Cannot connect to the Docker daemon at 
unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

How to solve this problem? I want to establish docker containers in the host docker container.

Jolly23
  • 879
  • 1
  • 8
  • 13

6 Answers6

18

I had a similar problem trying to install Docker inside a Bamboo Server image. To solve this:

  1. first remove the line: RUN docker run hello-world from your Dockerfile
  2. The simplest way is to just expose the Docker socket, by bind-mounting it with the -v flag or mounting a volume using Docker Compose:

docker run -v /var/run/docker.sock:/var/run/docker.sock ...

Felipe Desiderati
  • 2,414
  • 3
  • 24
  • 42
  • 3
    This is preferable to running Docker-in-Docker in many cases, for the reasons referenced in jonashackt's link: https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/ – kiprainey May 21 '19 at 14:28
  • 2
    This does not work because it requires you to already have built the container (e.g. `docker run`). If the Dockerfile recipe includes steps that invoke Docker (`docker build ...`), then this will not help. – user5359531 Feb 28 '20 at 20:31
  • 1
    Bind mounts are not a recommended way to do this since this creates a dependence on the host system – Sanil Khurana Aug 27 '21 at 07:13
  • resolved my problem – nobjta_9x_tq Jul 17 '23 at 09:07
6

In your .dockerfile add this line to install Docker:

RUN curl -fsSL https://get.docker.com | sh

After build is done, when running your container, add a volume mapping to the host Docker socket with the -v switch , e.g.:

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock my-container

Then, from within the container shell, check the connection by running:

# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
8bf420851572   my-image               "bash"                   8 minutes ago   Up 8 minutes                               my-container
dux2
  • 1,770
  • 1
  • 21
  • 27
5

Use Docker-in-Docker for this task. They have already solved many of the problems for you.

Hendrikvh
  • 535
  • 4
  • 10
3

The easiest way is to use the official Docker-in-Docker images from https://hub.docker.com/_/docker/ with the :dind tag (which is the successor of the project Hendrikvh already mentioned).

You definitely need to use the --priviledged flag also:

docker run --privileged --name yourDockerContainerNameHere -d docker:dind

With that your Docker-in-Docker experiments should work - but be aware of the many stumbleblocks that could be in your way: https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/

jonashackt
  • 12,022
  • 5
  • 67
  • 124
0
   //create container in privileged mode
    sudo docker container run -it --name uob_20.04 --privileged=true <dockerhub-image> /bin/bash
   //give access
    sudo chmod ugo+rw /var/run/docker.sock
    sudo nohup dockerd > /dev/null 2>&1 &
    //check docker installation
    docker images
DevMJ
  • 2,810
  • 1
  • 11
  • 16
-3

Try with starting docker service before of executing any docker command. Add this line

RUN bash service docker start

to your Dockerfile above of this line:

RUN docker run hello-world 
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64