Jenkins: running docker commands on a docker slave - jenkins

I'm using the Kubernetes Jenkins plugin to orchestrate jenkins slaves
I want to run all the jobs in Docker (build docker images and execute tests/builds in docker).
example jenkins job:
docker run -e NEXUS_USERNAME=${NEXUS_USERNAME} -e NEXUS_PASSWORD=${NEXUS_PASSWORD} common-dropwizard:latest mvn deploy
I am using the jenkinsci/jnlp-slave from here: https://hub.docker.com/r/jenkinsci/jnlp-slave/
Unfortunately, the slave image doesn't appear to support running docker. My question is what is the best approach to accomplish this?
thanks

You need to install docker client and mount the docker socket so you can access the Docker host. Then you can interact with that Docker host
https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/
https://github.com/jenkinsci/docker-workflow-plugin/tree/master/demo

Related

How to forbidden docker run command in docker daemon also how to restrict individual user access to docker daemon

Objective:
I have 200+ projects using docker builds they run docker in their own docker daemon.To reduce cost i setup a central docker build server where i have to allow all projects to build docker images securely
Description
I created the setup with jenkins docker pipeline by installing docker plugin in jenkins and connected to my docker host via docker API.when i run build it launch docker host as jenkins slave container and allow to run docker build
Issue
Setup works fine for building docker image but my concern is with security
how to securely allow 200+ projects to connect docker daemon?
How to restrict access of each users based on roles?
How to forbidden docker run command in docker daemon? they are restricted to run docker run
Platform i use:
Jenkins running in redhatopenshift
docker host in a linux box
Can any suggest me the steps to fix this security hole
Regards
Ashif

Is there any configuration to run docker inside a jenkins container?

I am trying to build an image with docker and then upload it to the docker hub, after passing the quality tests I receive the following error: docker: not found, how can I communicate my docker service (localhost) with the container of jenkins.
Important: I have docker desktop installed locally and I have installed jenkins in a local container also in windows 10 pro.
Error: https://imgur.com/q1SrKGe
Pipeline: https://imgur.com/nQWL1HR
You have 2 options to do this:
Install Docker inside your Jenkins Container and also add a bind mount for the Docker socket from your host. Otherwise your Docker Daemon inside your Container wont work. On Linux this socket is /var/run/docker.sock, so the bind mount would look like -v /var/run/docker.sock:/var/run/docker.sock.
Use a different slave agent for the Building Image Stage where you have docker installed. For e.g. you could use Docker-in-Docker (https://hub.docker.com/_/docker) as a Slave Agent for Jenkins (connected via ssh) and run your docker build inside this slave agent.

Running docker command in a Java application executing in a docker container

I am creating a Spring Boot monitoring agent that collects docker metrics. The agent can be attached through POM dependency to any client Spring Boot application that runs inside a docker container.
In the agent, I am trying to programatically run docker stats
But, it fails to execute because the docker container doesn't have docker client installed in it.
So how can I run docker commands in docker container? Please note, I can't make changes to the Dockerfile of client.
You may execute docker commands within the container by defining the docker socket in the container.
run the container and mount the 'docker.sock' in the following manner:
docker run -v /var/run/docker.sock:/var/run/docker.sock ...
so mainly you have to mount docker.sock to order to run docker commands within container.

How to run build on a docker container when jenkins is in a docker container

I have a jenkins single instance running in a docker container. The host is AWS 16.04.3 LTS (Xenial Xerus) system. I want the jenkins run my build inside a docker container but since jenkins is already running inside a docker. I don't want a nested docker container running in the build. How can I make jenkins launch a docker container on the host instead of itself container?
I found a solution for that is to mount this directory on jenkins container:
docker run -v /var/run/docker.sock:/var/run/docker.sock

jenkins exectue shell docker in slave node without sudo

I`m using jenkins and slave node(ssh connect).
I want to exectue shell.
docker ps
this is error
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
so this slave node connect to ssh (jenkins connect user = ssh connect users) after exec command
docker ps
normal operation...
ssh connection and jenkins slave node(ssh connection) different??
why?? jenkins error???? help me...
Docker Daemon is not started on slave, this is a docker deployment issue. Some tips are here. Please make sure to first manually tryout docker info and docker ps on slave machine to ensure proper docker functioning.

Resources