0

I'm trying to build and then run a docker image on Jenkins. I have set up Jenkins on ubuntu on an AWS ec2 server. When I try to build I get this error:

docker permission error on jenkins ubuntu ec2

For reference, I have also attached my JenkinsFile.

pipeline {
    agent any
    stages {
        stage('Start') {
        steps {
            echo 'Starting to build the docker-react-app.'
        }
        }
        stage('building docker image') {
        steps {
            sh 'sudo docker build -t docker-react-app .'
        }
        }
        stage('runing docker image') {
        steps {
            sh 'sudo docker run -dp 3001:3000 docker-react-app'
        }
        }
    }
}

I am using Jenkins with the default administrator account.

I have also added the Jenkins user to the docker group. But it is hasn't solved the issue. I have also verified that by running:

cat /etc/group | grep docker

Which outputs:

docker:x:998:ubuntu,jenkins

Let me know if you need any further information.

Hanzla
  • 214
  • 5
  • 15
  • does the solution I provided works for you – Altaf May 26 '21 at 03:58
  • Does this answer your question? [How to fix "dial unix /var/run/docker.sock: connect: permission denied" when group permissions seem correct?](https://stackoverflow.com/questions/51342810/how-to-fix-dial-unix-var-run-docker-sock-connect-permission-denied-when-gro) – samm Jun 26 '21 at 09:28
  • https://stackoverflow.com/a/71085404/4791684 – h8n Feb 11 '22 at 19:18

2 Answers2

1

The error says that your current user can’t access the docker engine, because you’re lacking permissions to access the unix socket to communicate with the engine.
In order to solve this issue :
Run below command in any of your shell:
sudo usermod -a -G docker $USER
NOTE : do a complete restart of machine and also jenkins.
After this step : Completely log out of your account and log back in.

For more info : https://docs.docker.com/engine/install/linux-postinstall/

Altaf
  • 2,838
  • 1
  • 16
  • 8
  • I have already tried that. You can also see the output in my question. – Hanzla May 26 '21 at 05:56
  • @Hanzla :ok you did a restart also? and if you type ```docker version```, does it give you correct output? – Altaf May 26 '21 at 06:44
  • What is the current user you are running with : ```echo $USER``` – Altaf May 26 '21 at 07:03
  • My current user is `ubuntu` – Hanzla May 26 '21 at 13:47
  • @Hanzla : So you executed this command ```sudo usermod -a -G docker $USER``` and you executed ```sudo usermod -a -G docker jenkins``` and to confirm, you did a restart also(machine restart) – Altaf May 26 '21 at 14:57
-1

Try to run your pipeline scripts via jenkins user on your target server Maybe you should add jenkins user to root group