Jenkins All in Docker 2

(this post if part of the material I cover in my devops course)

In the previous post, we have created a docker:dind container, a smart container that has docker installed inside, can store docker images, and run internal containers.
In this post we'll use it as a jenkins agent from a Jenkins controller (also a container), so that Jenkins could run docker agent stages and pipelines.

Running a Jenkins controller (and agent) container

We now run a Jenkins container that'll use the docker:dind container that we have run in the previous post.

  • First, create an image for it, because a regular jenkins/jenkins image does not have the docker cli tool. (We want jenkins to run cli commands that will connect to docker:dind container)
  • Save a Dockerfile with this content:
    (it also adds Blue Ocean that we will not use here.):
 1FROM jenkins/jenkins:2.426.2-jdk17
 2USER root
 3RUN apt-get update && apt-get install -y lsb-release
 4RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
 5  https://download.docker.com/linux/debian/gpg
 6RUN echo "deb [arch=$(dpkg --print-architecture) \
 7  signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
 8  https://download.docker.com/linux/debian \
 9  $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
10RUN apt-get update && apt-get install -y docker-ce-cli
11USER jenkins
  • Create an image:
    (I have called mine jenkins_with_docker):
1docker build . -t jenkins_with_docker:one
  • Now, run a container.
    The many parameters are taken from the tutorial:
 1docker run \
 2  --name jenkins_docker \
 3  --restart always \
 4  --detach \
 5  --network jenkins \
 6  --env DOCKER_HOST=tcp://docker:2376 \
 7  --env DOCKER_CERT_PATH=/certs/client \
 8  --env DOCKER_TLS_VERIFY=1 \
 9  --publish 8080:8080 \
10  --publish 50000:50000 \
11  --volume jenkins-data:/var/jenkins_home \
12  --volume jenkins-docker-certs:/certs/client:ro \
13  jenkins_with_docker:one
  • Note DOCKER_HOST environment configuration, that specifies the docker location.
    It uses the network-alias we have defined in the previous post

Test you Jenkins docker abilities

  • Exec into your new container:
1docker exec -it jenkins_docker sh
  • Try various docker commands:
    • docker images should show you the python image from the previous post
    • You should be able to run a Python container in the same way

Connect to Jenkins

  • While still in your jemkins exec sessions, recover the unlock password at:
    /var/jenkins_home/secrets/initialAdminPassword
  • Now, set your browser (same machine) to http://localhost:8080/
  • When prompted, insert the password to unlock jenkins
  • We'll make sure that this jenkins configuration can run docker agents in later posts.

Jenkins - first time from the GUI

  • Click on the Install suggested plugins
  • Choose an admin user name (e.g your first name), choose a pasword (and save it somewhere)
  • The Jenkins URL will be http://localhost:8080/, and we'll be running Jenkins from this machine.
  • Hit the Start using jenkins button.
  • Enjoy