Skip to content

Instantly share code, notes, and snippets.

@izzyleung
Last active August 23, 2024 07:08
Show Gist options
  • Save izzyleung/f0ee565fbaf2142cf713ce460600efa5 to your computer and use it in GitHub Desktop.
Save izzyleung/f0ee565fbaf2142cf713ce460600efa5 to your computer and use it in GitHub Desktop.
Docker in Docker-ized Jenkins agent

Docker in Docker for your Jenkins Docker agent

This is a reference implementation of running Docker in your Docker-ized Jenkins build agent(s)

Build the new image

$ docker build -t YOUR_IMAGE_TAG .

Running the container

$ docker run -d --privileged \
    --rm --name=dind_agent \
    -p NEW_PORT:22 \
    -e "JENKINS_AGENT_SSH_PUBKEY=[YOUR_PUBLIC_KEY]" YOUR_IMAGE_TAG

Please note, due to the nature of Docker in Docker, you MUST run the container with the --privileged parameter.

References:

FROM jenkins/ssh-agent:debian-jdk17
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y sudo curl && \
curl -fsSL https://get.docker.com > docker.sh && \
bash docker.sh && \
systemctl disable docker && \
rm -f docker.sh && \
echo '#!/usr/bin/env bash' >> /usr/local/bin/new_entry_point.sh && \
echo 'sudo service docker start' >> /usr/local/bin/new_entry_point.sh && \
echo '/usr/local/bin/setup-sshd' >> /usr/local/bin/new_entry_point.sh && \
chmod 755 /usr/local/bin/new_entry_point.sh && \
apt-get clean && rm -rf /var/lib/apt/lists/* && \
echo 'jenkins ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers && \
usermod -aG docker jenkins
ENTRYPOINT ["/usr/local/bin/new_entry_point.sh"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment