jenkins : Cannot connect to the Docker daemo - docker

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?

#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.
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/
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/

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)

Related

How to build Docker Container outside of dockerized Jenkins

I have a docker composed Jenkins container running on Ubuntu 20.04. When I push my code, a job is auto started in Jenkins. And a docker build operation is executed, after that a docker compose up -d operation is also executed. My Application is up and runing but not reachable...
When I run docker ps on Ubuntu, there is no container of my Application, because the docker container of my app was built inside the Jenkins Container, and docker compose up was also executed inside my jenkins container.
I want to achieve that my app runing side by side with Jenkins and other Container on Ubuntu not inside Jenkins Container. How I can achieve that?
Assuming you have an application that is dockerized. The Jenkins is also running on a docker container exposed at some port. Then you can open Jenkins and write a freestyle Job for deployment which would do the following:
Have a deploy folder where the shellscript is written as per below. This build repository could be within the same repo as the source code or somewhere else as well.
ssh -i key user#server '
# The command here are run within this ssh session.
#Checkout your code from version control
git clone your_repo
#build your docker services
cd your_repo
docker-compose build service1
docker-compose build service2
...
#run your built services
docker-compose up -d service1
docker-compose up -d service2
...
'
Now, the application should be deployed and accessible for you and Jenkins should be able to notify the same.

jenkins kubernetes plugin execution issues

I am running Jenkins on EKS with Kubernetes plugin.
I have one cloud setup, and a template running my own container image for alpine with docker ( to execute docker commands )
i have only 1 job currently that only does "docker service ls" as bash
i get the error
"/tmp/jenkins8475081645730667159.sh: line 2: docker: command not
found"
while going inside the container using exec and switching to "jenkins" user i am able to run "docker".
it looks like my pod contains both jnlp container and my alpine-docker container-when write to file , it will write it to the alpine container while if i run "docker" it will try to run it on the jnlp container, does this make any sense ? Thanks
you have to run docker from your container
In your pipeline
container('mycontainer') {
sh 'docker service ls'
}
You can't use a container other than the jnlp one if you are using freestyle jobs, only pipeline jobs

Jenkins with make and docker

I have been playing around with Jenkins, and I'm now able to connect github and set triggers. I want to build my code using make and docker, however when i execute make or docker in the shell, they are not found. How do I configure Jenkins' build step to run make and docker
I would install make and the docker daemon on your Jenkins server. This will allow you to build and push docker images from within your Jenkins build pipelines using the Executable Shell Build task. You will also be able to run make commands there as well.
docker build -t <USER>/<REPO_NAME>:<TAG> .
docker push <USER>/<REPO_NAME>:<TAG>
There are also Jenkins plugins available for building your docker images too.
I would NOT recommend running Jenkins using a Docker container, then running Docker inside that container. This is known as Docker in Docker(aka. DinD), and should be avoided for the reasons stated in this article.
You can install Docker on the same machine where your jenkins is running.
Or you can run a docker container which contains both jenkins and docker.
If you purpose is to learn jenkins, I suggest running Jenkins within a docker and Docker daemon on your host machine.
just have Docker installed on your host machine.
then issue the command which runs
docker run \
--rm -u root -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock --name myjenkinsserver jenkinsci/blueocean
then you are ready to go.
add a pipeline job as follows:
pipeline {
agent { docker 'gcc:latest' }
stages {
stage('build') {
steps {
sh 'make --version'
}
}
}
}
now you can run make commands.
In general, it is better to run jenkins jobs on Jenkins slave machines or in other terms, Jenkins agents. You can create custom Jenkins agents which include necessary tools, in your case, such as make.

Jenkins pipeline can't build docker image

I am attempting to build a docker image from a Dockerfile using a declarative pipeline in Jenkins. I've successfully added the 'jenkins' user to the docker group, and can run 'docker run hello-world' as the jenkins user manually. However, when I attempt to build through the pipeline, I can't even run 'docker run hello-world':
From the pipeline:
[workspace] Running shell script
+ whoami
jenkins
[workspace] Running shell script
+ groups jenkins
jenkins : jenkins docker
[workspace] Running shell script
+ docker run hello-world
docker: Got permission denied while trying to connect to the Docker
daemon socket at unix:///var/run/docker.sock: Post
http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/create: dial unix
/var/run/docker.sock: connect: permission denied.
Manually sshing into Jenkins and switching to the 'jenkins' user:
*********#auto-jenkins-01:~$ sudo su - jenkins -s/bin/bash
jenkins#auto-jenkins-01:~$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
Some other useful information: Jenkins is running from a VM.
Needed to give jenkins user group privileges to docker unix socket by editing /etc/default/docker and adding:
DOCKER_OPTS=' -G jenkins'

Cannot get Jenkins Docker slave to build docker images

I am currently experimenting with Docker in combination with Jenkins to streamline the CI/CD workflow for a new project. I do so on a Mac with Docker 1.12 installed.
This is what I do:
Use docker machine to create a new Docker server
Use the official Jenkins Docker image to spin up a Jenkins instance on that server
Install the "Yet Another Docker Plugin" and "CloudBees Docker Pipeline" plugins.
Add a "Docker Cloud" using the IP of the Docker server above and the third party Docker DinD image tehranian/dind-jenkins-slave
With this setup, I run a very simple pipeline job like this:
node('docker') {
docker.image('hseeberger/scala-sbt').inside {
stage 'Checkout'
echo 'We got here!'
}
}
Jenkins spins up a Docker instance as expected and executes the job. So the basic Docker setup is working as expected.
But the Docker command within the job fails. Log output looks something like this:
[Pipeline] node
Still waiting to schedule task
Docker-23ebf3d8dd4f is offline
Running on Docker-23ebf3d8dd4f in /home/jenkins/workspace/docker-test
[Pipeline] {
[Pipeline] sh
[docker-test] Running shell script
+ docker inspect -f . hseeberger/scala-sbt
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[Pipeline] sh
[docker-test] Running shell script
+ docker pull hseeberger/scala-sbt
Using default tag: latest
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon. Is the docker daemon running on this host?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Now when I browse around for solutions, it is usually mentioned that the Docker socket needs to be provided to the container as a volume, but that doesn't seem to work either.
Since the general setup seems to be working, wouldn't the slave simply have to do the same thing as the Jenkins plugin does to spin up the Docker slave in the first place? That is, use the URL of the Docker server to control it? Since I assume this is an extremely common use-case, there must be a Docker image for Jenkins Docker slaves that can do this out of the box, right? What am I missing?
You might need to set the docker env and use the content of docker-machine env node in your running shellscript.

Resources