Socrates
Socrates

Reputation: 9564

Run host Docker from within Jenkins Docker

Is it possible to create and run Docker containers for CI/CD from within a running Jenkins Docker Container? So basically access Docker on the host server from within a running container.

On my host server (Ubuntu 19.04) Docker (Docker version 19.03.3) is installed. By running the following command I create a Jenkins Container that I give permissions to Docker (so I thought):

mkdir /home/myuser/Desktop/jenkins_home
docker run -dit --name jenkins -v /home/myuser/Desktop/jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -p 8080:8080 jenkins/jenkins:lts

Within Jenkins I create a Pipeline that loads a Jenkinsfile from Git that looks like this:

pipeline {
    agent {
        docker {
            image 'ubuntu:19.04'
            args '-u root:sudo -p 3000:3000'
        }
    }
    stages {
        stage('Install') {
            steps {
                sh 'apt-get update'
                sh 'apt-get install -y curl'
                sh 'curl -sL https://deb.nodesource.com/setup_13.x | sh -'
                sh 'curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -'
                sh 'echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list'
                sh 'apt-get update'
                sh 'apt-get install -y nodejs yarn'
            }
        }
        stage('Build') {
            steps {
                sh './build.sh'
            }
        }
    }
}

When I run the Pipeline it crashes when trying to instruct Docker to pull the ubuntu:19.04 Docker image. The error is docker: not found.

Somewhere a connection between my Jenkins Container and the host Docker access files is misconfigured. What configuration is necessary to run Docker commands on the host server from within the Docker Container?

Upvotes: 0

Views: 206

Answers (1)

danielhg
danielhg

Reputation: 31

If you want to create and run Docker containers for CI/CD from Jenkins container, This can be achieved creating a shell command on Jenkins job that runs an ssh command on Docker host.

This needs as requirements that Jenkins container ssh public key is authorized on Docker host, so authorized_keys file should exist on Docker host. To use the same ssh keys inside Jenkins container can be used a bind mount with ssh keys on Jenkins containers. Example using docker-compose:

volumes:
- /home/user/.ssh/id_rsa:/var/jenkins_home/.ssh/id_rsa 
- /home/user/.ssh/id_rsa.pub:/var/jenkins_home/.ssh/id_rsa.pub

This is an example content of a shell command used to launch and update containers on Docker host from a Jenkins job:

cat ./target/stack/installer-*.tar | ssh root@${DOCKER_HOST} \
        /home/user/Build-Server/remote-installer.sh

In the command above an installer is launched on Docker host. As result new containers are deployed/updated on Docker host. The remote-installer.sh script receive the file from standard input and unpack it using tar command.

TEMPDIR=`mktemp -d`
echo "unarchiving to $TEMPDIR"
tar xv -C "$TEMPDIR"
...

This works for both cases having Docker containers on same server as Jenkins container or having Docker containers and Jenkins container on different servers.

Upvotes: 0

Related Questions