0
votes

So I'm working with a slightly strange infrastructure: I have a openshift container platform that has a jenkins image from docker running inside it using the image openshift3/jenkins-2-rhel7

I'm trying to run docker build . command's within a jenkins pipeline and i'm getting a "Cannot connect to the Docker daemon" error. I don't understand why docker is installed on the machine yet not running and I don't currently have access to the openshift server other than cli and via the console. Does anyone have recommendations on how to get the docker build . command to run successfully for jenkins either with or without utilizing slaves?

    node("master"){
        withEnv(["PATH=${tool 'docker'}/bin:${env.PATH}"]) {   
            docker.withRegistry( 'dockertest') {
            git url: "https://github.com/mydockertag/example.git", credentialsId: 'dockertest'
            stage "build"
            sh "docker build -t mydockertag/example -f ./Dockerfile ."
            stage "publish"
        }
    }

After running the build command i get the following error:

+ docker build -t mydockertag/example -f ./Dockerfile .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the 
docker daemon running?
2
I believe you would usually set up a separate build configuration in OpenShift for the docker build and from the job in Jenkins trigger that separate build. You can possibly find more details searching through documentation on using pipelines in OpenShift at docs.openshift.com/container-platform/3.9/dev_guide/… - Graham Dumpleton

2 Answers

0
votes

There can be two reasons for the error "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?".

  1. Docker is running but the user executing the docker command does not have privileges to talk to '/var/run/docker.sock'. Try using 'sudo docker build'. If you do not wish to use 'sudo' everytime, you can add your user to the docker group by following the post docker installation steps here (https://docs.docker.com/install/linux/linux-postinstall/#manage-docker-as-a-non-root-user).

  2. The docker daemon is not up and running at all. You will have to start the docker daemon manually.

0
votes

By default, OpenShift Container Platform runs containers using an arbitrarily assigned user ID. For an image to support running as an arbitrary user, directories and files that may be written to by processes in the image should be owned by the root group and be read/writable by that group. Files to be executed should also have group execute permissions.

Adding the following to your Dockerfile sets the directory and file permissions to allow users in the root group to access them in the built image:

RUN useradd -g root -G sudo  -u 1001 user && \
    chown -R user:root /some/directory && \
    chgrp -R 0 /some/directory && \
    chmod -R g=u /some/directory
#Specify the user with UID
USER 1001

Refer section "Support Arbitrary User IDs" on the Guideline from Openshift