Search code examples
jenkinsdockerboot2docker

How to run a Docker host inside a Docker container?


I have a Jenkins container running inside Docker and I want to use this Jenkins container to spin up other Docker containers when running integration tests etc.

So my plan was to install Docker in the container but this doesn't seem to work so well for me. My Dockerfile looks something like this:

FROM jenkins
MAINTAINER xxxx

# Switch user to root so that we can install apps
USER root

RUN apt-get update 

# Install latest version of Docker
RUN apt-get install -y apt-transport-https
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
RUN sh -c "echo deb https://get.docker.com/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
RUN apt-get update
RUN apt-get install -y lxc-docker

# Switch user back to Jenkins
USER jenkins

The jenkins image is based on Debian Jessie. When I start bash terminal inside container based on the generated image and do for example:

docker images

I get the following error message:

FATA[0000] Get http:///var/run/docker.sock/v1.16/images/json: dial unix /var/run/docker.sock: no such file or directory. Are you trying to connect to a TLS-enabled daemon without TLS?

I suspect that this could be because the docker service is not started. But my next problem arise when I try to start the service:

service docker start

This gives me the following error:

mount: permission denied

I've tracked the error in /etc/init.d/docker to this line:

mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup

So my questions are:

  1. How do I actually start a Docker host inside a container? Or is this something that should be avoided?
  2. Is there something special I need to do if I'm running Mac and boot2docker?
  3. Perhaps I should instead link to the Docker on the host machine as described here?

Update: I've tried the container as user root and jenkins. sudo is not installed.


Solution

  • 1.- The first container you start (the one you launch other one inside) must be run with the --privileged=true flag.

    2.- I think there is not.

    3.- Using the privileged flag you don't need to mount the docker socket as a volume.

    Check this project to see an example of all this.