Jenkins DinD pipeline - docker

Hello i want to build some maven projects in docker.
While the build process some more docker containers will be spawned so i want to isolate them inside... docker.
for Example:
Develop-branch[database-container, core-container]
Feature(1..n)[database-container, core-container]
I tried to use Jenkins docker plugin and configured the linux-server as cloud using unix socket.
For the building container i use docker:dind and installed inside maven java and git.
FROM docker:dind
RUN apk add openjdk11 maven git
compailing is working but if it comes to the
build stepp calling docker i got the Error:
[ERROR] DOCKER> Cannot create docker access object [No such file or directory]

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 build Docker container on remote host with dockerfile

I'm quite new to Jenkins and spent 2 whole days not twisting my head (and google and stackoverflow) around, how to get a docker container built on a remote host (from Jenkins host perspective).
My setup:
Docker runs on a MacOS machine (aka the "remote host")
Jenkins runs as docker container on this machine
Bitbucket Cloud runs at Atlassian
PyCharm is my development tool - running on the MacOS machine
Everything works fine so far. Now, I want Jenkins to build a docker container (on the "remote host") containing my python demo.
I'm using a dockerfile in my project:
FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD ["test.py"]
ENTRYPOINT ["python3"]
I'm trying to build a jenkinsfile, I'm expecting to do 2 things
Pull the repo
Build the docker image with the help of the dockerfile on the "remote host"
Docker is installed as plugin and configured.
Docker is installed via Jenkins configuration.
Docker remote host is set up in "Cloud" setup in Jenkins - connection works (with the help of socat running as docker container)
Docker Host ist set to the remote host IP and port 2376
I'm using a jenkins pipeline project.
Most promising threat about using remote hosts is of course https://www.jenkins.io/doc/book/pipeline/docker/#using-a-remote-docker-server
But using docker.withServer('tcp://192.168.178.26:2376') (in my case, locally, no credentials because not reachable from outside), I had no luck at all.
Most common error message: hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.docker.workflow.Docker.withServer() is applicable for argument types: (java.lang.String, java.lang.String) values: [tcp://192.168.178.26:2376]
If I try to let Jenkins build it inside it's own container with its own docker, it tells me /var/jenkins_home/workspace/dockerbuild#tmp/durable-6e12255b/script.sh: 1: /var/jenkins_home/workspace/dockerbuild#tmp/durable-6e12255b/script.sh: docker: not found
Strange, as I thought, docker was installed. But I want to build at remote host anyway.
In my eyes the most promising jenkinsfile is the following - but to be honest, I am totally lost at the moment and really need some help:
node {
checkout scm
docker.withServer('tcp://192.168.178.26:2376')
def customImage = docker.build("my-image:${env.BUILD_ID}")
customImage.inside {
sh 'make test'
}
I appreciate any hint and am greatful for your help.
Best regards
Muhackl

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.

Calling docker commands from jenkins job as step running outside docker VM

I want to run a docker image create command from a Jenkins job which is running native on my machine with VM running Docker.
I've installed docker build step plugin and in manage Jenkins page command fails when I try to configure docker using url and version, it says:
Test Connection Something went wrong, cannot connect to
http://192.168.99.100:2376, cause: null
I've got this docker url from the docker-machine env command.
In the version field, I tried both REST API version and Docker version, but no success.
Is it possible to call Docker from outside the docker VM and from a Jenkins job? If yes how to configure it in Jenkins?

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