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
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)
@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.
Reference: https://docs.docker.com/engine/install/
Lets say you have both Jenkins master and Jenkins agent running as docker nodes.
Where,
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
which needs to be the Docker Host URI
Reference : https://blog.executeautomation.com/running-jenkins-build-agent-within-docker-container-part-a/