Running tasks in a Docker container in Jenkins


Can Jenkins work the same as GitLab Runner + Docker executor :

  1. Deploy a container from an image.
  2. Clone the git repository inside the container, execute arbitrary commands.
  3. Select artifacts and save them outside the container, collapse the container (complete its work).

Question : Need a step by step Jenkins setup guide to complete the above three steps.

Found Docker Plugin . It seems that he knows how to deploy the Jenkins slave in the docker, connect it to the master and dispose of it after use. This option definitely does not suit me, because I need to drag Java and other things that are not needed there into the assembly environment. The base image of jenkins-slave weighs 100 times more than alpine , which is generally beyond reasonable.

docker images                                                                                                              
REPOSITORY              ...     SIZE
alpine                  ...     3.97 MB
evarga/jenkins-slave    ...     368 MB

I also found the Docker build step plugin. One of the features is "create new container from image". Perhaps this is exactly what I need, but I do not understand how to complete steps 2 and 3.


In Jenkins you can run a bash/batch command, for bash it would be something like this:

docker run -it -d my_image  # запускам image в detached моде
docker exec $(docker ps -a -q --filter ancestor=my_image) bash -c "cd your_path; your_command" # запускаем Вашу команду в контейнере.

list=$(docker exec  $(docker ps -a -q --filter ancestor=my_image)  bash -c 'ls /path/to/artifacts/inside/container') # создаем список артефактов, которые мы будем вытаскивать из контенера
for i in $list; do  docker cp $(docker ps -a -q --filter ancestor=my_image):$i ./path/to/outside container ; done # вытаскиваем артефакты согласно списку

for i in $(docker ps -a -q --filter ancestor=my_image); do docker rm $(docker stop $i); done # удалям контейнеры, если они работают
Scroll to Top