skip to Main Content

i wanted to create a docker image with jenkins but Cannot connect to the Docker daemon .

this is my pipeline

pipeline {
    agent any
    options { buildDiscarder(logRotator(numToKeepStr:'5'))}
    environment {DOCKERHUB_CREDENTIALS = credentials('tfkben-dockerhub')}
    stages {
            stage('build'){  steps {  sh 'docker build -t tfkben/ben:latest .' }  }
            stage('Login'){  steps {  sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin ' }}
            stage('Push'){  steps {  sh 'docker push tfkben/ben:latest'}   }                                 
         }                           
            post { always { sh 'docker logout' }}

}
my Dockerfile :

FROM python:3.11-rc-bullseye

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt

COPY . .

CMD [ "python3", "manage.py", "runserver", "0.0.0.0:8000"]
and this is the error message :
  • docker build -t tfkben/ben:latest .
    Cannot connect to the Docker daemon at tcp://docker:2376. Is the docker daemon running?

2

Answers


  1. It is likely due to your Jenkins agent not having a Docker daemon running within it.

    If the agent itself is running as a Docker container, then you need to ensure that it uses an image that has “Docker-in-Docker” (dind)

    Login or Signup to reply.
  2. @Toufik Benkhelifa It seems you don’t have docker installed in your jenkins agent. Where is your Jenkins agent is resided? There are couple of possibilities here.

    1. If you are using any Linux distros OS(Ubuntu, Redhat, centos etc) as Jenkins agent, then you can install docker daemon explicitly in that agent.

    Reference: https://docs.docker.com/engine/install/

    1. If you are running the agent as a docker container, then you can do something similar to below

    Lets say you have both Jenkins master and Jenkins agent running as docker nodes.

    Where,

    • Jenkins Master is actually running within Docker as a Docker container
    • Jenkins Agent is running within Docker as a Docker container
    • Now the Jenkins Master is communicating with the host Docker via Docker Demon in tcp://host.docker.internal:2375
    • Once the Jenkins Master is connected with Docker Demon, it can then safely communicate with any containers running within that Docker host via the Demon, since it will have all the networking information to talk with different containers, in this case it’s going to be a Docker container running Jenkins agent.

    Now, Link Jenkins Master with Docker Host Daemon. In order to do that, you need to use "Docker plugin for Jenkins" https://plugins.jenkins.io/docker-plugin/

    The aim of this docker plugin is to be able to use a Docker host to dynamically provision a docker container as a Jenkins agent node, let that run a single build, then tear-down that node, without the build process (or Jenkins job definition) requiring any awareness of docker.

    Once the plugin is installed, all we need to do is to configure Jenkins to add new cloud from Jenkins -> Manage -> System configuration and add new cloud as ‘Docker’

    Finally, as mentioned earlier, the Docker demon will be running on

    tcp://host.docker.internal:2375
    

    which needs to be the Docker Host URI

    Reference : https://blog.executeautomation.com/running-jenkins-build-agent-within-docker-container-part-a/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search