jenkinsdockercentos

Jenkins: Can't connect to Docker daemon


I am running Jenkins and Docker on a CentOS machine. I have a Jenkins job that pulls a Github repo and builds a Docker image. When I try running the job I get the error:

+ docker build -t myProject .
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Build step 'Execute shell' marked build as failure
Finished: FAILURE

This problem occurs even though I have added jenkins to my docker usergroup via sudo usermod -aG docker jenkins and restarted my machine. How do I fix this?

By the way, if try changing the command to sudo docker build -t myProject . I just get the error sudo: sorry, you must have a tty to run sudo


Solution

  • After the installation of Jenkins and Docker. Add jenkins user to dockergroup (like you did)

    sudo gpasswd -a jenkins docker
    

    Edit the following file

    vi /usr/lib/systemd/system/docker.service
    

    And edit this rule to expose the API :

    ExecStart=/usr/bin/docker daemon -H unix:// -H tcp://localhost:2375
    

    Do not create a new line with ExecStart, simply add the commands at the end of the existing line.

    Now it's time to reload and restart your Docker daemon

    systemctl daemon-reload
    systemctl restart docker
    

    Then restart jenkins, you should be able to perform docker commands as jenkins user in your jenkins jobs

    sudo service jenkins restart