How to properly start Docker inside Jenkins that is also running in Docker

I’m trying to run Docker inside a Jenkins container that is also running in Docker (i.e. Docker in Docker). What I want to know is how to properly start the Docker service when booting Jenkins. The only solution I’ve found today is to build my own Jenkins image based on the official Jenkins image but change the jenkins script loaded by the entry point to also start up Docker:

# I've added this line just before Jenkins is started from the script:
sudo service docker start
# I've also removed "exec" from the original file which used "exec java $JAVA_TOPS ..." but that didn't work
java $JAVA_OPTS -jar /usr/share/jenkins/jenkins.war $JENKINS_OPTS "$@"

This works when I run (using docker run) a new container but the problem is that if I do (docker start) on stopped container the Docker service is not started.

  • Files in the folder don't appear on the host after mounting to container
  • Docker Compose - Share named volume between multiple containers
  • Docker compose not mounting volumes
  • Dockerize ASP Classic on IIS
  • Possible to use Docker machine with continuous integration tools?
  • Docker swarm mode: scale down a node and remove services
  • I strongly suspect that this is not the right way to start my Docker service. My plan is to perhaps use supervisord to start Jenkins and Docker separately (I suppose container linking is out of the question since Docker should be executed as a service on the same container that Jenkins is running on?). My concern with this approach is that I’m going to lose the EntryPoint specified in the Jenkins Dockerfile which allows me to pass arguments to the Jenkins container when starting the container, for example:

    docker run -p 8080:8080 -v /your/home:/var/jenkins_home jenkins -- <jenkins_arguments>
    

    Does anyone have any recommendations on a good way to solve this preferably by not forking the official Jenkins image?

  • How to rebuild docker container in docker-compose.yml?
  • Persistent Storage with Docker in Production - Which Solution and Why? [closed]
  • Getting pygame running in docker
  • (docker-compose + nginx load balancing) + Jenkins?
  • Efficiently using multiple docker containers in a single host
  • Creating an environmental variable within Docker container when starting up
  • One Solution collect form web for “How to properly start Docker inside Jenkins that is also running in Docker”

    I’m pretty you cannot do that.

    Docker in Docker doesn’t mean you have to run docker inside docker with 3 level : host > First level container > Second Level Container

    In fact, you just need to share docker with host, and this is your host who will run others containers.

    To do that, you have to mount volume with -v parameter

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

    with this command, when you will docker run inside you jenkins container, the docker client will communicate with docker deamon from your host in order to run new container.

    To do that, you should run your jenkins container with privileged

    --privileged
    

    To resume, here is the full command line

    docker run -d -v /var/run/docker.sock:/var/run/docker.sock --privileged myimage
    

    And you you don’t need to create a new jenkins image for that.

    Hoping to have helped you

    http://container-solutions.com/running-docker-in-jenkins-in-docker/

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.