Skip to content

Instantly share code, notes, and snippets.

@coverboy
Last active Jul 24, 2021
Embed
What would you like to do?
Jenkins as a Docker Container (Agent Role)
# Explanation : https://blog.1234.co.kr/2021/07/24/jenkins-as-a-docker-container-agent-role/
FROM ubuntu:20.04
# First off, replace ubuntu repository for fast download.
# Check the official ubuntu repository mirror site
# https://launchpad.net/ubuntu/+archivemirrors
RUN sed --in-place 's/archive.ubuntu.com/mirror.kakao.com/g' /etc/apt/sources.list
RUN apt update && apt upgrade -y
# Jenkins server/agent mostly need git source integration!
RUN apt install git -y
#RUN sudo apt install build-essential -y
# Before Installing openjdk-11-jdk
# Below line is crucial. If you comment out below line,
# You will see tz-data setup screen, while building docker image.
# and, docker build can't not proceed any more!!
# Also, be sure to change timezone for your own timezone!!
RUN export DEBIAN_FRONTEND=noninteractive; export DEBCONF_NONINTERACTIVE_SEEN=true; echo 'tzdata tzdata/Areas select Asia' | debconf-set-selections; echo 'tzdata tzdata/Zones/Etc select Seoul' | debconf-set-selections; apt-get update -qqy && apt-get install -qqy --no-install-recommends tzdata && rm -rf /var/lib/apt/lists/*
# Below line is required for using futher apt command.
# Because you just removed /var/lib/apt/lists/* :-)
RUN apt update
# Every Jenkins agent must install jdk (depends on Jenkins Master Role Server)
# e.g) Master Server JDK Version 11 - Agent JDK Version must be 11
RUN apt install openjdk-11-jdk -y
# We will use Jenkins ssh agent
RUN apt install openssh-server -y
RUN adduser jenkins --shell /bin/bash --disabled-password
RUN mkdir -p /home/jenkins/jenkins_agent
RUN mkdir -p /home/jenkins/.ssh
# Generate your own ssh key
# e.g) ssh-keygen -t rsa -b 4096 -C "The access key for Jenkins slaves"
# and, replace ssh key with your own key
RUN echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDewU0Cz4Rm6GBGwTeZhFJdvrlsSv6cIEQVEUOnR5HXBtVwj7BEGSVl+s5S6CKCpltSDGKLbtyxraNzaRYRhX7JwS2ydAMknRMzvby/uuIOB29+duKtr2KficzMQJsdIOwLfqNt9TvcFBkUrYqrH9LyOoRyuVZnlvFwei340+0IWqVHZZJmOn6LratDK1+zwQtDJMGvj0Pby1hWRrTwSvZ3dNbrZSQK7yDsCNWubD9OCwlbQLOhdEGWC8J8ZN8VeOxwL4Nj0AR0Hp41BiYqVywDYGqjcswaCJCE5CF98pf814KYpeSuiyr0jdYhAlXe/rjYa5jduVRwKHhh7n+wOCAOLXIuvlgoSrXA9s96NvtuOVobOu3SlutY4XebFsufLpGojrBRLCCQXI3MJt6Z836+h8AquLNLF9CGFASU4UaZji7iDsVrsCQBwDgDYlfnnulJTvyqIiBr3eoqpqvQ2FoYV/cIYZatU8C2szFTiy5wPno0KIIhv1ej0zR31gY6IfM= The access key for Jenkins slaves' > /home/jenkins/.ssh/id_rsa.pub
RUN echo '-----BEGIN OPENSSH PRIVATE KEY-----' >> /home/jenkins/.ssh/id_rsa
RUN echo 'b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn' >> /home/jenkins/.ssh/id_rsa
RUN echo 'NhAAAAAwEAAQAAAYEA3sFNAs+EZuhgRsE3mYRSXb65bEr+nCBEFRFDp0eR1wbVcI+wRBkl' >> /home/jenkins/.ssh/id_rsa
RUN echo 'ZfrOUugigqZbUgxii27csa2jc2kWEYV+ycEtsnQDJJ0TM728v7riDgdvfnbira9in4nMzE' >> /home/jenkins/.ssh/id_rsa
RUN echo 'CbHSDsC36jbfU73BQZFK2Kqx/S8jqEcrlWZ5bxcHot+NPtCFqlR2WSZjp+i62rQytfs8EL' >> /home/jenkins/.ssh/id_rsa
RUN echo 'QyTBr49D28tYVka08Er2d3TW62UkCu8g7AjVrmw/TgsJW0CzoXRBlgvCfGTfFXjscC+DY9' >> /home/jenkins/.ssh/id_rsa
RUN echo 'AEdB6eNQYmKlcsA2Bqo3LMGgiQhOQhffKX/NeCmKXkrosq9I3WIQJV3v642GuY3blUcCh4' >> /home/jenkins/.ssh/id_rsa
RUN echo 'Ye5/sDggDi1yLr5YKEq1wPbPejb7bjlaGzrt0pbrWOF3mxbLny6RqI6wUSwgkFyNzCbemf' >> /home/jenkins/.ssh/id_rsa
RUN echo 'N+vofAKrizSxfQhhQElOFGmY4u4g7Fa7AkAcA4A2JX557pSU78qiIga93qKqar0NhaGFf3' >> /home/jenkins/.ssh/id_rsa
RUN echo 'CGGWrVPAtrMxU4sucD56NCiCIb9Xo9M0d9YGOiHzAAAFmL8/aAu/P2gLAAAAB3NzaC1yc2' >> /home/jenkins/.ssh/id_rsa
RUN echo 'EAAAGBAN7BTQLPhGboYEbBN5mEUl2+uWxK/pwgRBURQ6dHkdcG1XCPsEQZJWX6zlLoIoKm' >> /home/jenkins/.ssh/id_rsa
RUN echo 'W1IMYotu3LGto3NpFhGFfsnBLbJ0AySdEzO9vL+64g4Hb3524q2vYp+JzMxAmx0g7At+o2' >> /home/jenkins/.ssh/id_rsa
RUN echo '31O9wUGRStiqsf0vI6hHK5VmeW8XB6LfjT7QhapUdlkmY6foutq0MrX7PBC0Mkwa+PQ9vL' >> /home/jenkins/.ssh/id_rsa
RUN echo 'WFZGtPBK9nd01utlJArvIOwI1a5sP04LCVtAs6F0QZYLwnxk3xV47HAvg2PQBHQenjUGJi' >> /home/jenkins/.ssh/id_rsa
RUN echo 'pXLANgaqNyzBoIkITkIX3yl/zXgpil5K6LKvSN1iECVd7+uNhrmN25VHAoeGHuf7A4IA4t' >> /home/jenkins/.ssh/id_rsa
RUN echo 'ci6+WChKtcD2z3o2+245Whs67dKW61jhd5sWy58ukaiOsFEsIJBcjcwm3pnzfr6HwCq4s0' >> /home/jenkins/.ssh/id_rsa
RUN echo 'sX0IYUBJThRpmOLuIOxWuwJAHAOANiV+ee6UlO/KoiIGvd6iqmq9DYWhhX9whhlq1TwLaz' >> /home/jenkins/.ssh/id_rsa
RUN echo 'MVOLLnA+ejQogiG/V6PTNHfWBjoh8wAAAAMBAAEAAAGAMH4OQM1iOnDd/Kk1bGMk9yl8/j' >> /home/jenkins/.ssh/id_rsa
RUN echo 'k5Qk235Io0myb2dcQUFQQC0GBVBuVZoyzLzntep/bwQQBqZPey/H3lAv13w34db5rX0FGt' >> /home/jenkins/.ssh/id_rsa
RUN echo 'i3cDrw2FBp7Rw8snv73Nj4OVJOXvnqC7Iil4dIiNdnSdnSXGaUhtSLQ6fysfxz9R4jWG6o' >> /home/jenkins/.ssh/id_rsa
RUN echo 'NgN5BcKLXQ8BZN1CSDgwTfOaeNxxkqXQioMiILYgTUgPcgvpKd8vK6rPKvzRKDSgxgn6HB' >> /home/jenkins/.ssh/id_rsa
RUN echo 'cK8eK8i+Ombm+OOQuQDwt79W7nDyNxJbLaSHhHWsYFLee095l+WEOmBMdxlXcaR00fCAz/' >> /home/jenkins/.ssh/id_rsa
RUN echo 'MQjarmO8n+TUYxDx6HF682Yy+M2m1xW+A5oT3jvPTr3KTgT2joG412VlpCH9SdF5XMK7AC' >> /home/jenkins/.ssh/id_rsa
RUN echo 'EB00Xpm1DbCA+hKX8a7d4E1ghgSsuTQ3Qs5BwnJ9M1HEbkYTnBBHBssl+hXzdM+HwXufrL' >> /home/jenkins/.ssh/id_rsa
RUN echo 'ju2/nXvvC0tbTtCti371FQG6K86ORmmLXf8xnlCTBmNfDefNLaQFdi3ZKAXilgOwmZAAAA' >> /home/jenkins/.ssh/id_rsa
RUN echo 'wA7dQ01AyaI2PQI8CpZVEplc+V7SGbnsd8KnTvfttwmXp7Op4uMPbYGHCX19xW4/JRgi3F' >> /home/jenkins/.ssh/id_rsa
RUN echo 'XzCPIYTWuNsbvyquOPCN6tsqHAA2s2Flmz8GBUkxjhrjq0bSxoyHSJvvP+W9u/y0KZaqTk' >> /home/jenkins/.ssh/id_rsa
RUN echo 'UdIKsraq5ph8efdfgsiXX0pohiMejUvwaS2mzCbgZtNjpQBqU8AVaCP5B9RCnk2NxTVyW3' >> /home/jenkins/.ssh/id_rsa
RUN echo '9+Ag5TUHy5Zpxh7gztUegUkt6GTAMrPZNbs8GIT6t9PxOSxQAAAMEA/CBzmTqAFlJuIee0' >> /home/jenkins/.ssh/id_rsa
RUN echo 'FTrGDL8WUNJNlIMLvabTh4t1/NSh7y311tx+b0LN6rl6Hvla6odhzVLtTCA35GfKAQbKKa' >> /home/jenkins/.ssh/id_rsa
RUN echo 'HqPA/RHhFHMec7GeYmZ0PvGSKvFGyyYZkO3FAgQdGiVKVnspHi/m9Gc4B4X44ZmGgpIQp/' >> /home/jenkins/.ssh/id_rsa
RUN echo 'i7rtZRCTFDw53qt/JNms7r6GbusGYxj6VfO8sqFW5tdCVwBzw6DysKy8Wq31/FzMk8G4je' >> /home/jenkins/.ssh/id_rsa
RUN echo 'Ij1TsCGNKKnYCg3TJW3goALRYb90D9AAAAwQDiLVaQgMxoxM77fhl+3dUNPA9KdqI9jFXA' >> /home/jenkins/.ssh/id_rsa
RUN echo 'EhBD/nn+yvmrCq3uxx8z0krAjZ3EtptcvGUIEZnAko9McgZRFiRcPKJBg4CR9Cce38los/' >> /home/jenkins/.ssh/id_rsa
RUN echo '1r3Ly0ECdppyrjuKHgCdvw9wWf2ysJzgHZXQVUP9HGPoQjuFWv6vLefYhkX7s2tVgyqRF5' >> /home/jenkins/.ssh/id_rsa
RUN echo '4VboyRt4L9orUvHLStM2r5ckjlv70o6vH6fKpa2fLkqxmIcLAoIDo/VN1weIg3zvXMRgEI' >> /home/jenkins/.ssh/id_rsa
RUN echo '+juPwCXsMMGa8AAAAhVGhlIGFjY2VzcyBrZXkgZm9yIEplbmtpbnMgc2xhdmVzAQI=' >> /home/jenkins/.ssh/id_rsa
RUN echo '-----END OPENSSH PRIVATE KEY-----' >> /home/jenkins/.ssh/id_rsa
RUN cat /home/jenkins/.ssh/id_rsa.pub > /home/jenkins/.ssh/authorized_keys
RUN chown -R jenkins:jenkins /home/jenkins/
# Gradle Install start
# I'm using gradle with spring boot. So, if you don't need gralde skip this part.
RUN apt install wget -y
RUN apt install zip unzip -y
RUN mkdir -p /opt/gralde
RUN /bin/bash -c "wget https://services.gradle.org/distributions/gradle-7.1.1-bin.zip --directory-prefix /opt/gradle"
RUN unzip -d /opt/gradle /opt/gradle/gradle-7.1.1-bin.zip
RUN sed --in-place 's/.$/:\/opt\/gradle\/gradle-7.1.1\/bin\"/' /etc/environment
ENV PATH=${PATH}:/opt/gradle/gradle-7.1.1/bin
ENTRYPOINT service ssh start && bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment