docker build and publish plugin usage - jenkins

I have jenkins running as Docker container, I tried to install jenkins build and publish plugin here and copied Dockerfile inside jenkins workspace, but whenever I run the build, it gives me:
Started by user Jenkins Admin
Building in workspace /var/lib/jenkins/jobs/workspace
[workspace] $ docker build -t index.docker.io/test/openshift:latest --pull=true /var/lib/jenkins/jobs/test/workspace
ERROR: Cannot run program "docker" (in directory "/var/lib/jenkins/jobs/workspace"): error=2, No such file or directory
java.io.IOException: Cannot run program "docker" (in directory "/var/lib/jenkins/jobs/workspace"): error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at hudson.Proc$LocalProc.<init>(Proc.java:244)
at hudson.Proc$LocalProc.<init>(Proc.java:216)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:803)
at hudson.Launcher$ProcStarter.start(Launcher.java:381)
Build step 'Docker Build and Publish' marked build as failure
Finished: FAILURE
could you please tell me why is that so?

Inside a Docker container you have no access to the docker-binary by default (hence the error message No such file or directory).
If you want to use Docker within a Docker container, you need to either use DinD (Docker-in-Docker) or DooD (Docker-outside-of-Docker).
The first is a separate Docker installation within your Jenkins-container, the second only mounts the hosts Docker installation via volumes.
Further reading about DinD in general and in regards to Jenkins:
https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
https://github.com/killercentury/docker-jenkins-dind
https://github.com/tehranian/dind-jenkins-slave
Further reading about DooD in general and in regards to Jenkins:
http://container-solutions.com/running-docker-in-jenkins-in-docker/
https://hub.docker.com/r/axltxl/jenkins-dood/
Update
The information on using the Workflow plugin below is no longer correct.
I have since written a plugin called docker-swarm-slave that offers a build-wrapper you can configure for a job which automatically provisions a Docker-container for a build, if you use my jenkins-dood-image or are running directly on bare metal.
Documentation unfortunately is rather sparse, but maybe it is useful to somebody.
I have a similar use-case: I want to be able to automatically start a Docker-container with a specified image running a Jenkins Swarm client that will take over the build.
My jenkins-dood-image contains a script docker-slave which lets me automatically provision a Docker-Swarm-slave and execute what I need on it using the Workflow-plugin with a script like the following:
node('master') {
stage 'Create docker-slave'
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'swarm-login', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
sh 'docker-slave --job-name $JOB_NAME --build-number $BUILD_NUMBER -i pitkley/python-swarm:3.4 -u $USERNAME -p $PASSWORD -- -labels "${JOB_NAME}_${BUILD_NUMBER}"'
}
stage 'Execute on docker-slave'
node("${env.JOB_NAME}_${env.BUILD_NUMBER}") {
sh 'hostname'
}
stage 'Remove docker-slave'
sh 'docker-slave --job-name $JOB_NAME --build-number $BUILD_NUMBER --rm'
}
(This assumes you need credentials to authenticate which are saved with a short-ID of swarm-credentials.)

Related

Unable to generate Docker image using JKube plugin when executing k8s:build

I'm using Jkube maven plugin to generate a Docker image via a Jenkins pipeline on AWS EC2 instance under Ubuntu.
When pipeline executes mvn clean install k8s:build I'm getting this error :
[ERROR] Failed to execute goal org.eclipse.jkube:kubernetes-maven-plugin:1.3.0:build (default-cli) on project social-carpooling-frontend: Execution default-cli of goal org.eclipse.jkube:kubernetes-maven-plugin:1.3.0:build failed: No <dockerHost> given, no DOCKER_HOST environment variable, no read/writable '/var/run/docker.sock' or '//./pipe/docker_engine' and no external provider like Docker machine configured -> [Help 1]
And this is the Jenkins pipeline :
pipeline {
agent any
stages {
stage('Docker Check Stage') {
steps {
sh '/home/bitnami/downloads/apache-maven-3.8.1/bin/mvn clean install k8s:build -Premote'
}
}
}
}
When I log using ssh to this machine and execute docker -v it says Docker version 20.10.0, build 7287ab3
So Docker is really installed and daemon is started, but when I trigger it via maven it seems not to find it !
Any ideas ?
The problem was due to the user who's running maven command which doesn't have access to docker.sock
The solution is to modify the read/write permission on docker.sock this way :
sudo chmod 776 /var/run/docker.sock

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.

docker container as build slave for jenkins

I have installed Jenkins and docker in same machine Ubuntu.
Ubuntu 17.10
Jenkins ver. 2.73.3
Docker version 17.09.0-ce.
I am trying to set up jenkin docker containers as slaves for running my automation test suite.
I was able to correctly, setup the docker plugin in Jenkins for spinning up docker containers and added a docker template with a image that i created for setting up docker enviroment.
The image has been built on docker hosted in Ubuntu.
Problem is now when i run a job from Jenkins. It gives a error message
(pending—Jenkins doesn’t have label docker-slave)
Jenkins pending image
When I check the Jenkins logs in Ubuntu machine I see the following error message
com.github.dockerjava.api.exception.NotFoundException: {"message":"pull access
denied, "message":may require 'docker login'"}
In the ubuntu machine, i have already given done docker login.
The image that i am trying to build containers from is locally on ubuntu, not pushed to any repository, so why is trying to pull the image.
Also, what is the permission issue that i need to resolve. When build a job, from jenkins, its the jenkins user building the container. Do I need to add something else there.
In your Jenkinsfile you can use credentials with the credentials plugin:
// Login to registry
withCredentials([usernamePassword(credentialsId: 'YOUR_ID_AD_DEFINED_IN_JENKINS', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "docker login --username=$USER --password=$PASS your.registry.here.com"
}
// Build with correct pom version
sh "docker build -t your.registry.here.com/your_image_here/your_image_name:${POM_VERSION} ."
// Push
sh "docker push your.registry.here.com/your_image_here/your_image_name:${POM_VERSION}"
//pull
sh "docker pull your.registry.here.com/your_image_here/your_image_name:${POM_VERSION}"
The ${POM_VERSION} is just a rip from my own code and can ofcourse be any version you like
you also need to define the credentials in your Jenkins server. A description can be found here Working with jenkins credentials
Check your docker template configuration and select "Never pool" for "pool strategy".

jenkins pipeline docker build on docker agent

I've got a jenkins declarative pipeline build that runs gradle and uses a gradle plugin to create a docker image. I'm also using a dockerfile agent directive, so the entire thing runs inside a docker container. This was working great with jenkins itself installed in docker (I know, that's a lot of docker). I had jenkins installed in a docker container on docker for mac, with -v /var/run/docker.sock:/var/run/docker.sock (DooD) per https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/. With this setup, the pipeline docker agent ran fine, and the docker build command within the pipeline docker agent ran fine as well. I assumed jenkins also mounted the docker socket on its inner docker container.
Now I'm trying to run this on jenkins installed on an ec2 instance with docker installed properly. The jenkins user has the docker group as its primary group. The jenkins user is able to run "docker run hello-world" successfully. My pipeline build starts the docker agent container (based on the gradle image with various things added) but when gradle attempts to run the docker build command, I get the following:
* What went wrong:
Execution failed for task ':docker'.
> Docker execution failed
Command line [docker build -t config-server:latest /var/lib/****/workspace/nfig-server_feature_****-HRUNPR3ZFDVG23XNVY6SFE4P36MRY2PZAHVTIOZE2CO5EVMTGCGA/build/docker] returned:
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
Is it possible to build docker images inside a docker agent using declarative pipeline?
Yes, it is.
The problem is not with Jenkins' declarative pipeline, but how you're setting up and running things.
From the error above, looks like there's a missing permission which needs to be granted.
Maybe if you share what your configuration looks like and how your're running things, more people can help.

build docker image with dockerfile by jenkins

I've been blocked since yesterday and need some help. I tried to use both jenkins docker-plugin and docker-plugin-step to build docker image from a dockerfile.
However, when I try to run the jenkins job, it gives me the following exception:
Docker Build
Docker Build : build with tag jdubois/jhipster-docker:Gateway_Jenkins_V1 at path /home/javateam/DockerfileGateway
ERROR: Build step failed with exception
java.lang.NullPointerException: config was not specified
at shaded.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:226)
at com.github.dockerjava.core.DockerClientImpl.<init>(DockerClientImpl.java:36)
at com.github.dockerjava.core.DockerClientImpl.getInstance(DockerClientImpl.java:52)
at com.github.dockerjava.core.DockerClientBuilder.getInstance(DockerClientBuilder.java:29)
at com.nirima.jenkins.plugins.docker.builder.DockerBuilderPublisher$Run$1.invoke(DockerBuilderPublisher.java:144)
at com.nirima.jenkins.plugins.docker.builder.DockerBuilderPublisher$Run$1.invoke(DockerBuilderPublisher.java:139)
at hudson.FilePath.act(FilePath.java:991)
at hudson.FilePath.act(FilePath.java:969)
at com.nirima.jenkins.plugins.docker.builder.DockerBuilderPublisher$Run.buildImage(DockerBuilderPublisher.java:139)
at com.nirima.jenkins.plugins.docker.builder.DockerBuilderPublisher$Run.run(DockerBuilderPublisher.java:89)
at com.nirima.jenkins.plugins.docker.builder.DockerBuilderPublisher.perform(DockerBuilderPublisher.java:180)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.build(MavenModuleSetBuild.java:915)
at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.doRun(MavenModuleSetBuild.java:866)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:537)
at hudson.model.Run.execute(Run.java:1744)
at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:531)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:374)
Build step 'Build / Publish Docker Containers' marked build as failure
Finished: FAILURE
The following is the dockerfile content:
#Basic images for Gateway MS
FROM jdubois/jhipster-docker:Gateway_Jenkins_V1
#Fetch last Gateway MS version from gitlab and run it
RUN cd /home/jhipster/DockerJenkinsGateway && \
git pull origin master
# expose the working directory, the Tomcat port, the BrowserSync ports, the SSHD port, and run SSHD
VOLUME ["/jhipster"]
EXPOSE 8080 3000 3001 22
CMD /usr/sbin/sshd -D
What am doing wrong??
Another option is to use the shell provided by Jenkins as a build step, and just build the Docker image using the command-line arguments you would normally use in that shell script.
That's what we do on our build server and it gives us fine grained control over things like versioning and which image/tag to push to Dockerhub.
If you are using Jenkins 2.0 or above then Jenkins give you an option to build pipeline as the code and you can easily integrate docker and its repositories.
Go to --> new item --> select multi branch pipeline option and you will see the option to connect to docker via Jenkins.
Let me know in case of more details.

Resources