Question

can not run docker command inside the jenkins

Posted February 4, 2020 3.4k views
Linux Basics

I am new to jenkins. when I build jenkins job with docker –version command. it says “docker command not found”

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hello,

You would need to install Docker inside your Jenkins container. There are two ways to do that:

  • If your Jenkins container is already running, what you could do is attach to the container with the following command:
docker exec -it jenkins_container_id bash

Then while inside the container run the following commands:

apt-get update && apt-get install -y docker.io 
  • Another thing you could do is to share the Docker binary from your host so that it is accessible inside your Jenkins container, this is not great in terms of security:
docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):$(which docker)  -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

Hope that this helps!
Regards,
Bobby

  • Thank you for such a fast reply!
    stiil throw an error like below

    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

    Though I tried to add jenkins user to docker group
    It prompt me another error!
    jenkins@6a7b22660ddd:/$ usermod -aG docker jenkins
    usermod: group ‘docker’ does not exist

    If I ran jenkins as root it works for me!
    please help me

    • Hello,

      Did you install Docker or share the socket and the binary?

      Regards,
      Bobby

      • I did share the socket and binary
        I ran Like this!
        docker run -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):$(which docker) -p 8080:8080 -p 50000:50000 jenkins/jenkins:lts

        IT thrown an error
        Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

        If I run as root works fine

        • Hi @msfayas001,

          Thanks for sharing the additional details. It seems like that the Docker group is not available inside your Jenkins container.

          Have you tried adding the docker group inside your container:

          • First attach to your container

          • Then add the Docker group:

          groupadd docker
          
          • After that follow the steps from the guide above.

          Let me know how it goes!

          • I try that command groupadd docker

            But got the following error:

            jenkins@3484d7fd3830:~$ groupadd docker
            groupadd: Permission denied.
            groupadd: cannot lock /etc/group; try again later.
            jenkins@3484d7fd3830:~$ 
            

rafael, to run groupadd docker you have to be a root
try
docker exec -it -u 0 {containername} bash

Submit an Answer