Jolly23
Jolly23

Reputation: 919

How to install docker in docker container?

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.

Upvotes: 60

Views: 129703

Answers (7)

DevMJ
DevMJ

Reputation: 3061

1st Way: Install docker on container and start dockerd process

a.Create container in privileged mode

sudo docker container run -it --name uob_20.04 --privileged=true <image:tag> /bin/bash

b.Install Docker, give access and start dockerd process in background

    sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin docker-buildx-plugin
    sudo chmod ugo+rw /var/run/docker.sock
    sudo nohup dockerd > /dev/null 2>&1 &

2nd way: If you have docker installed in your host then you can mount that volume to container as follow while starting the container

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker <image:tag>

Upvotes: 0

Shubham Saini
Shubham Saini

Reputation: 1

Just do this:

RUN apt-get update -y && apt-get install -y jq
RUN apt-get install -y apt-transport-https ca-certificates curl software-properties-common
RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
RUN apt-get update -y && apt-get install -y docker-ce

Upvotes: -1

dux2
dux2

Reputation: 1910

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

Upvotes: 13

Felipe Desiderati
Felipe Desiderati

Reputation: 2982

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 ...

Upvotes: 20

jonashackt
jonashackt

Reputation: 14429

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/

Upvotes: 2

DarmVillegas
DarmVillegas

Reputation: 1

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 

Upvotes: -4

Hendrikvh
Hendrikvh

Reputation: 545

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

Upvotes: 6

Related Questions