Sign inSign up

kalithekitsune/jenkins-agent-docker

By kalithekitsune

•Updated 12 months ago

Jenkins agent that runs a Docker in Docker (DinD) environment for docker based pipelines.

Image
Integration & delivery
Developer tools
Monitoring & observability
0

818

kalithekitsune/jenkins-agent-docker repository overview

⁠Jenkins Agent with Docker-in-Docker (DinD)

This container provides a Jenkins agent environment with the Docker daemon pre-installed and configured. It allows you to run Docker commands within your Jenkins jobs, enabling containerized builds, tests, and deployments.

The container is designed to launch both the Docker daemon (as root) and the Jenkins agent (as a non-root jenkins user) securely and efficiently.

I'll try to setup weekly builds so that it stays updated;

⁠Functionality

  • Docker-in-Docker (DinD): The Docker daemon runs directly within the container, isolated from the host's Docker daemon.
  • Rootless Jenkins Agent: The Jenkins agent itself runs as the jenkins user, enhancing security by not running as root.
  • Docker Access for Jenkins: The jenkins user is added to the docker group, granting it the necessary permissions to interact with the Docker daemon.
  • Graceful Startup: An entrypoint script ensures that the Docker daemon is fully operational before the Jenkins agent starts, preventing race conditions.

⁠Usage

To use this container in your Jenkins environment, you must launch it with the --privileged flag. This is a requirement for running Docker-in-Docker (DinD) as it needs elevated permissions to manage container runtimes.

⁠Dockerfile

The Dockerfile uses the official Jenkins inbound agent as a base, installs Docker, and sets up the necessary user permissions and entrypoint.

# Use the official Jenkins agent image as the base
FROM jenkins/inbound-agent:latest

# Switch to the root user to install packages
USER root

# Install Docker
RUN curl -fsSL "https://get.docker.com/" | sh

# Add the 'jenkins' user to the 'docker' group to allow Docker access
RUN usermod -aG docker jenkins

# Create a volume for Docker's data directory to persist data
# and avoid potential issues with some storage drivers.
RUN mkdir -p /var/lib/docker
VOLUME /var/lib/docker

# Copy the entrypoint script into the container
COPY entrypoint.sh entrypoint.sh

# Set the entrypoint to our custom script
ENTRYPOINT ["sh", "entrypoint.sh"]

⁠entrypoint.sh

This script manages the startup process within the container. It first starts the Docker daemon in the background, waits for it to become fully available, and then switches to the jenkins user to launch the Jenkins agent process.

#!/bin/sh

# Start the Docker daemon in the background.
# 'nohup' ensures the process continues running even if the shell exits.
# Output and errors are redirected to a log file.
nohup /usr/bin/dockerd > /var/log/dockerd.log 2>&1 &

# Wait for the Docker daemon to become available.
# This loop prevents a race condition where the Jenkins agent might
# try to use Docker before the daemon is ready.
echo "Waiting for Docker daemon to start..."
attempts=0
max_attempts=30
while ! docker info > /dev/null 2>&1; do
  if [ ${attempts} -eq ${max_attempts} ]; then
    echo "Docker daemon failed to start after ${max_attempts} seconds." >&2
    cat /var/log/dockerd.log >&2
    exit 1
  fi
  attempts=$((attempts + 1))
  sleep 1
done
echo "Docker daemon started successfully."

# Switch to the 'jenkins' user and execute the standard jenkins-agent command,
# passing along all arguments provided to this script.
#
# - 'exec' replaces the current shell with the new command, making the
#   jenkins-agent the main process.
# - 'su jenkins -s /bin/sh -c' executes a command as the 'jenkins' user.
# - '"$@"' ensures all arguments passed to this entrypoint script are
#   forwarded to the jenkins-agent.
exec su jenkins -s /bin/sh -c '/usr/local/bin/jenkins-agent "$@"' -- jenkins-agent "$@"

Tag summary

Content type

Image

Digest

sha256:e77815e22…

Size

315.4 MB

Last updated

12 months ago

docker pull kalithekitsune/jenkins-agent-docker