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.

  • Can I pull an image without Internet when the image exists in docker registry-mirror?
  • Import a heroku postgres database dump into a docker database container with boot2docker
  • nginx: use environment variables
  • Where to keep Dockerfile's in a project?
  • Where to put test files for webdriverIO testing - using docker container?
  • cadvisor, elasticsearch, docker: no Elasticsearch node available
  • 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?

  • Piping stdout on windows to docker container running linux
  • Easiest way to connect with PuTTY to an existing docker container
  • Does putting ARG at top of Dockerfile prevent layer re-use?
  • ElasticBeanstalk with Docker: how to use create-environment from aws cli
  • Add a volume to Docker, but exclude a sub-folder
  • /bin/sh: 1: ./configure: not found - dockerfile
  • 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.