-3

I am trying to run docker inside an Ubuntu docker container to use it as a docker build agent for Jenkins.

I am getting the following error when I try to run the docker build command to create a docker image.

Error in Jenkins

ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Docker file for Docker build agent

FROM ubuntu:latest
RUN apt update

USER root

RUN apt install -y git

RUN apt-get install \
    ca-certificates \
    curl \
    gnupg -y

RUN mkdir -m 0755 -p /etc/apt/keyrings

RUN curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

RUN echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  tee /etc/apt/sources.list.d/docker.list > /dev/null

RUN apt-get update

RUN apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

RUN curl -fsSL https://deb.nodesource.com/setup_19.x | bash - &&\
apt-get install -y nodejs

The pipeline is supposed to :

  1. clone a repository
  2. build the react app
  3. build a Docker image and push it to docker registry.

How can I get this setup running ? Or is there a better approach in achieving this ?

I have to use a docker build agent as the all the tools necessary are not always directly install in the host machine.

George Jose
  • 166
  • 1
  • 1
  • 11
  • 1
    you need to look using docker DIND (Docker In Docker) however this requires you running the outside docker container as privillaged which has risks. You can read more about DIND here https://www.howtogeek.com/devops/how-and-why-to-run-docker-inside-docker/ – Chris Doyle Mar 31 '23 at 22:28
  • [Is it ok to run docker from inside docker?](https://stackoverflow.com/questions/27879713/is-it-ok-to-run-docker-from-inside-docker) (TL;DR: only sorta) also describes the preferred mechanism (bind-mounting the host's Docker socket). [Jenkins: Can't connect to Docker daemon](https://stackoverflow.com/questions/38105308/jenkins-cant-connect-to-docker-daemon) has a couple of more complete setups (the accepted answer unfortunately has a significant security issue). A container only runs one process, so if it's running Jenkins then it's not running a (nested) Docker daemon. – David Maze Apr 01 '23 at 10:54

1 Answers1

0

A less complicated approach would be to install docker on one your normal agents and then use the docker engine on this machine to build your docker images, let me present a similar example Jenkinsfile, where this approach has been used: ''' pipeline { agent any environment{ DOCKER_REG_CRED = credentials('Nexus-Creds') DOCKER_REG = "" } stages {

    stage('Building Docker Image') {
        agent {
            node {
                label 'jenkins-opensource-linux'
            }
        }
        steps {
            script {
                try {
      script {
        sh 'docker build -t ${DOCKER_REG}/docker-release/reactapp:${buildName} .'
        sh "docker login -u ${DOCKER_REG_CRED_USR} -p ${DOCKER_REG_CRED_PSW} ${DOCKER_REG}"
        sh 'docker push ${DOCKER_REG}/docker-release/lttechapp:${buildName}'
      }
         } catch (error) {
                    println error
                }
            }
        }
        post {
            success {
              script {
                cleanWs()
              }
            }
          }
    }
    
}

} ''' My agent jenkins-opensource-linux is a linux Vm being used with docker engine installed on it. Make sure you setup docker to be used without root. This should be easy enough to implement.