Create a container for a Jenkins worker in Docker

Introduction

In Jenkins you can create agents as docker containers. To do this you need to install the Docker plugin through the Plugin Manager.

An example of a docker file for such an agent you can study below

Banner image

Create a file with the Docker settings - Dockerfile and edit it, for example with the editor vim

touch Dockerfile
vi Dockerfile

FROM ubuntu:22.04 LABEL maintainer="www.eth1.ru" ENV DEBIAN_FRONTEND=noninteractive # Make sure the package repository is up to date. RUN apt-get update && \ apt-get install -y --no-install-recommends apt-utils && \ apt-get update && \ apt-get upgrade RUN apt-get -qy full-upgrade && \ apt-get install -qy git && \ # Install a basic SSH server apt-get install -qy openssh-server && \ sed -i 's|session required pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd && \ mkdir -p /var/run/sshd && \ # Install JDK 11 (latest stable edition) apt-get install -qy openjdk-11-jdk && \ # Install maven apt-get install -qy maven && \ # Cleanup old packages apt-get -qy autoremove # Add user jenkins to the image RUN useradd -ms /bin/bash jenkins # Set password for the jenkins user (you may want to alter this). RUN echo "jenkins:jenkins" | chpasswd && \ mkdir /home/jenkins/.m2 # ADD settings.xml /home/jenkins/.m2/ # Copy authorized keys COPY .ssh/authorized_keys /home/jenkins/.ssh/authorized_keys RUN chown -R jenkins:jenkins /home/jenkins/.m2/ && \ chown -R jenkins:jenkins /home/jenkins/.ssh/ # Standard SSH port EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]

You can build the image with the command build

docker build -t jenkins-docker-slave:1.0 .

-t is short for --tag, it sets the name and tag in the format 'name:tag'

the tag may be omitted - it is optional

. means - in the current directory

This image can be pushed to dockerhub and give Jenkins a link to the repository.

Article author: Andrei Olegovich