How to run Docker inside Jenkins which is running as container

I’m working on Centos7. I have a Docker container which is running Jenkins. In that Jenkins-container I have to build and run other Docker containers. But Jenkins doesn’t know docker. I’m able to execute a shell and install docker inside the container. But isn’t it possible to let the container use my docker-engine on the host? How can I use it?

What is the best option to install Docker inside a Jenkins-(docker)-container?

  • docker Connection refused
  • Unable to access Jupyter notebook in the web browser
  • AWS ECS Docker - nginx: [emerg] host not found in upstream
  • Running quick programs inside a docker container
  • Docker vfs as default storage driver for docker 1.10.0
  • change bluemix docker default subnet to a different value?
  • Docker - Elasticsearch - Failed to establish a new connection: [Errno 111] Connection refused',))
  • Docker In Docker - SSL problems
  • Dockerfile - CMD does not run the shell script
  • Will docker cost system resources to exchange data with system?
  • Unable to run flask app in debug mode using Docker
  • docker-machine on mac does not work with docker-engine protected by self-create CA
  • 2 Solutions collect form web for “How to run Docker inside Jenkins which is running as container”

    Generally, a container-in-container setup involves linking /var/run/docker.sock and docker itself.
    For example, in this thread:

    docker run --name jenkins --privileged=true -t -i --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/bin/docker -p 8080:8080 jenkins
    

    This is not exactly your case, since you don’t need to run Jenkins itself in a “cic” (container in container”).
    But that illustrates how you would run any container in a container, with docker available in it.

    Make sure the user in that container is part of the docker group (if you don’t want to use root), as in this jenkins/setup-docker-and-start-jenkins.sh script

    #!/bin/sh
    set -e
    
    JUSER="jenkins"
    
    DOCKER_GID=$(ls -aln /var/run/docker.sock  | awk '{print $4}')
    
    if ! getent group $DOCKER_GID; then
        echo creating docker group $DOCKER_GID
        addgroup --gid $DOCKER_GID docker
    fi
    
    if ! getent group $GID; then
        echo creating $JUSER group $GID
        addgroup --gid $GID $JUSER
    fi
    
    if ! getent passwd $JUSER; then
        echo useradd -N --gid $GID -u $UID $JUSER
        useradd -N --gid $GID -u $UID $JUSER
    fi
    
    DOCKER_GROUP=$(ls -al /var/run/docker.sock  | awk '{print $4}')
    if ! id -nG "$JUSER" | grep -qw "$DOCKER_GROUP"; then
        adduser $JUSER $DOCKER_GROUP
    fi
    
    chown -R $JUSER:$JUSER /var/jenkins_home/
    

    Note that this setup uses tini to launch Jenkins (as I described in “Jenkins does not run automatically after install in Docker container”)

    exec su $JUSER -c "/bin/tini -- /usr/local/bin/jenkins.sh"
    

    Again, those scripts are for using Jenkins in “cic”.
    In your case, you can use those scripts for the containers that your Jenkins will have to run.

    Using the official Jenkins docker image:

    docker run -d \
        -u root \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -v $(which docker):/usr/bin/docker:ro \
        -p 8080:8080 \
        --name jenkins \
        jenkins
    

    Then to verify everything is working:

    • create a new job
    • add a shell script as a build step with docker version as content

    If you run into the following error on CentOS 7:

    docker: error while loading shared libraries: libsystemd-journal.so.0: cannot open shared object file: No such file or directory

    then start the container with:

    docker run -d \
        -u root \
        -v /var/run/docker.sock:/var/run/docker.sock \
        -v $(which docker):/usr/bin/docker:ro \
        -v /usr/lib64/libsystemd-journal.so.0:/usr/lib/x86_64-linux-gnu/libsystemd-journal.so.0 \
        -v /usr/lib64/libsystemd-id128.so.0:/usr/lib/x86_64-linux-gnu/libsystemd-id128.so.0 \
        -v /usr/lib64/libdevmapper.so.1.02:/usr/lib/x86_64-linux-gnu/libdevmapper.so.1.02 \
        -v /usr/lib64/libgcrypt.so.11:/usr/lib/x86_64-linux-gnu/libgcrypt.so.11 \
        -v /usr/lib64/libdw.so.1:/usr/lib/x86_64-linux-gnu/libdw.so.1 \
        -p 8080:8080 \
        --name jenkins \
        jenkins
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.