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?

  • How to access a docker container from another machine than its windows host?
  • Launching run docker from cloud-init incorrectly prefixes “docker.io” to image name
  • Docker secure connection with ssh port forwarding
  • Can't connect to mySql docker container with JDBC
  • Docker Compose does not bind ports
  • not able to deploy k8s pod when tried through jenkins job on GCE
  • Boot2Docke init: boot2docker init error in run: Failed to initialize machine “boot2docker-vm”: exit status 1
  • Pycharm 2017 docker support for debugging
  • Dockerfile for NGINX Web server
  • Best practices for managing SSH keys with Azure container service deployments
  • Docker port mapping not working, “connection refused”
  • Connect nginx on host with wsgi unicorn inside docker container
  • 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.