Below is my docker file that i used to build the image, which in turn will be used as dynamic docker agent in jenkins. If a new job is triggered in jenkins, the containers are up and but the virtualenv is not activated.
I've tried the below on a centos7 image where the virtualenv is activated on container startup. I've also posted the container startup logs below.
FROM ubuntu:bionic
LABEL MAINTAINER="Boomi-DCP"
SHELL ["/bin/bash", "-c"]
# Install a basic SSH server and other packages
RUN apt-get update -q && apt-get install --no-install-recommends -y -qq software-properties-common gpg-agent build-essential ca-certificates \
curl default-libmysqlclient-dev git jq libbz2-dev libdb5.3-dev libexpat1-dev libffi-dev libgdbm-dev liblzma-dev libmysqlclient-dev libncurses5-dev \
libncursesw5-dev libreadline-dev libsqlite3-dev libssl-dev locales mysql-client openssh-client openssh-server openssl sudo tar tzdata unzip uuid-runtime \
wget zlib1g-dev python3-sphinx build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev && \
rm -rf /var/lib/apt/lists/* && apt-get clean autoclean -q && apt-get autoremove --yes && rm -rf /var/lib/apt/lists/* /var/lib/cache
# Install OpenJDK1.8
ARG APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=1
RUN wget -O- https://apt.corretto.aws/corretto.key | apt-key add - && add-apt-repository 'deb https://apt.corretto.aws stable main'
RUN apt-get update && apt-get install java-common
RUN cd /tmp && wget https://corretto.aws/downloads/resources/8.282.08.1/java-1.8.0-amazon-corretto-jdk_8.282.08-1_amd64.deb && dpkg --install java-1.8.0-amazon-corretto-jdk_8.282.08-1_amd64.deb
RUN mkdir -p /etc/alternatives/java_sdk && ln -s /usr/lib/jvm/java-1.8.0-amazon-corretto/bin /etc/alternatives/java_sdk
# Repo to install nodejs and npm packages
RUN curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
RUN apt install -y nodejs
# This is needed to get sshd working
RUN sed -i 's|session required pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd
RUN mkdir -p /var/run/sshd
# Python 3.8.1 is needed for gCloud SDK along with alternatives settings.
RUN cd /tmp && wget --user $ARTIFACTORY_USER --password $ARTIFACTORY_PASSWORD $ARTIFACTORY_BASE_URI/python/$PYTHON_TARBALL.tgz && \
tar -xvf $PYTHON_TARBALL.tgz
RUN cd /tmp/$PYTHON_TARBALL && ./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" && make && make altinstall
RUN ln -sfn /usr/local/bin/python3.8 /usr/bin/python && python --version
## Install gCloud SDK
RUN cd /usr/local && wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.zip && unzip google-cloud-sdk.zip && rm google-cloud-sdk.zip
RUN cd /usr/local && google-cloud-sdk/install.sh --usage-reporting=true --path-update=true --bash-completion=true --rc-path=/.bashrc --disable-installation-options
# Alternatives settings to use gcloud globally by any user
RUN /usr/bin/update-alternatives --install /usr/bin/gcloud gcloud /usr/local/google-cloud-sdk/bin/gcloud 2
RUN gcloud components install kubectl
RUN cd /tmp && wget --user $ARTIFACTORY_USER --password $ARTIFACTORY_PASSWORD $ARTIFACTORY_BASE_URI/third-party/${VIRTUALENV_VERSION} && \
pip3.8 install ${VIRTUALENV_VERSION}
RUN cd /usr/local && python3.8 -m venv unifi_virtualenv
RUN cd /tmp/${SETUPTOOLS_TARBALL} && source /usr/local/unifi_virtualenv/bin/activate && \
python setup.py install
# Pip environment. Needed for builds to support licensing of Unifi software
# Use six version 1.10.0 due to an issue with unifi using version 1.11.0
# google-api-python-client is needed for upgrade jobs of metastore
RUN source /usr/local/unifi_virtualenv/bin/activate && pip install wheel==0.34.2 && pip install six==1.10.0 && pip install pgpy==0.5.2 && pip install python-dateutil==2.7.2
# Set Java home so that builds use the same version of Java as the one use during runtime
# This environment variable is required for gradle builds as some projects are dependent on this env variable.
ENV GCLOUD_HOME /usr/local/google-cloud-sdk/bin
ENV PATH=/usr/local/google-cloud-sdk/bin:/opt/jdk1.8.0_131/bin:$PATH:/home/jenkins/bin
ENV TERM vt102
# Add user jenkins to the image
RUN adduser jenkins
# Set password for the root and jenkins user (you may want to alter this).
RUN echo "root:root" | chpasswd
RUN echo "jenkins:jenkins" | chpasswd
RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
# Set variables in /etc/profile
RUN echo "export JAVA_HOME=/etc/alternatives/java_sdk" >> /etc/profile
RUN echo "export GCLOUD_HOME=/usr/local/google-cloud-sdk/bin" >> /etc/profile
# Copy public key to authorized_keys file. This is to allow the master jenkins server to ssh into the slaves
RUN echo "ServerAliveInterval 300" >> /etc/ssh/ssh_config
RUN sed -i 's/#Port 22/Port 2223/' /etc/ssh/sshd_config
RUN sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config
# Make jenkins user owner of virtual environment
RUN chown -R jenkins:jenkins /usr/local/unifi_virtualenv
# While building the image, uncomment the above line and place your workaround.tar.gz in the same directory.
RUN chmod +x /usr/bin/startup.sh
USER jenkins
RUN echo "source /usr/local/unifi_virtualenv/bin/activate" >> /home/jenkins/.bashrc
RUN source /usr/local/unifi_virtualenv/bin/activate
ENTRYPOINT ["/usr/bin/startup.sh"]
Ubuntu Docker container startup log.
[03/14/22 01:30:15] [SSH] The remote user's environment is:
BASH=/bin/bash
BASHOPTS=cmdhist:complete_fullquote:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_EXECUTION_STRING=set
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="20" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.4.20(1)-release'
DIRSTACK=()
EUID=1000
GROUPS=()
HOME=/home/jenkins
HOSTNAME=7f9050fd5ae1
HOSTTYPE=x86_64
IFS=$' \t\n'
LOGNAME=jenkins
MACHTYPE=x86_64-pc-linux-gnu
MAIL=/var/mail/jenkins
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
PIPESTATUS=([0]="0")
PPID=32
PS4='+ '
PWD=/home/jenkins
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SSH_CLIENT='10.240.0.154 55120 2223'
SSH_CONNECTION='10.240.0.154 55120 172.17.0.4 2223'
TERM=dumb
UID=1000
USER=jenkins
_=']'
Checking Java version in the PATH
openjdk version "1.8.0_282"
OpenJDK Runtime Environment Corretto-8.282.08.1 (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM Corretto-8.282.08.1 (build 25.282-b08, mixed mode)
[03/14/22 01:30:16] [SSH] Checking java version of /home/jenkins/jdk/bin/java
Couldn't figure out the Java version of /home/jenkins/jdk/bin/java
bash: /home/jenkins/jdk/bin/java: No such file or directory
[03/14/22 01:30:17] [SSH] Checking java version of /etc/alternatives/java_sdk/bin/java
[03/14/22 01:30:17] [SSH] /etc/alternatives/java_sdk/bin/java -version returned 1.8.0_282.
[03/14/22 01:30:17] [SSH] Starting sftp client.
[03/14/22 01:30:18] [SSH] Copying latest remoting.jar...
[03/14/22 01:30:30] [SSH] Copied 1,522,173 bytes.
Expanded the channel window size to 4MB
[03/14/22 01:30:30] [SSH] Starting agent process: cd "/home/jenkins" && /etc/alternatives/java_sdk/bin/java -jar remoting.jar -workDir /home/jenkins -jar-cache /home/jenkins/remoting/jarCache
Mar 14, 2022 1:30:31 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /home/jenkins/remoting as a remoting work directory
Mar 14, 2022 1:30:31 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /home/jenkins/remoting
<===[JENKINS REMOTING CAPACITY]===>channel started
Remoting version: 4.11.2
This is a Unix agent
Evacuated stdout
Agent successfully connected and online
Centos contianer startup log.
[03/13/22 20:31:15] [SSH] The remote user's environment is:
BASH=/usr/bin/bash
BASHOPTS=cmdhist:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_EXECUTION_STRING=set
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="2" [2]="46" [3]="2" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='4.2.46(2)-release'
DIRSTACK=()
EUID=1000
GROUPS=()
HOME=/home/jenkins
HOSTNAME=6a884f871ec4
HOSTTYPE=x86_64
IFS=$' \t\n'
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LOGNAME=jenkins
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/mail/jenkins
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/unifi_virtualenv/bin:/usr/local/bin:/usr/bin
PIPESTATUS=([0]="0")
PPID=23
PS1='(unifi_virtualenv) '
PS4='+ '
PWD=/home/jenkins
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SSH_CLIENT='10.240.0.154 53946 2223'
SSH_CONNECTION='10.240.0.154 53946 172.17.0.2 2223'
TERM=dumb
UID=1000
USER=jenkins
VIRTUAL_ENV=/usr/local/unifi_virtualenv
_=/usr/local/unifi_virtualenv/bin/activate
_OLD_VIRTUAL_PATH=/usr/local/bin:/usr/bin
_OLD_VIRTUAL_PS1=
deactivate ()
{
if [ -n "${_OLD_VIRTUAL_PATH:-}" ]; then
PATH="${_OLD_VIRTUAL_PATH:-}";
export PATH;
unset _OLD_VIRTUAL_PATH;
fi;
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ]; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}";
export PYTHONHOME;
unset _OLD_VIRTUAL_PYTHONHOME;
fi;
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ]; then
hash -r;
fi;
if [ -n "${_OLD_VIRTUAL_PS1:-}" ]; then
PS1="${_OLD_VIRTUAL_PS1:-}";
export PS1;
unset _OLD_VIRTUAL_PS1;
fi;
unset VIRTUAL_ENV;
if [ ! "${1:-}" = "nondestructive" ]; then
unset -f deactivate;
fi
}
Checking Java version in the PATH
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (build 25.282-b08, mixed mode)
[03/13/22 20:31:16] [SSH] Checking java version of /home/jenkins/jdk/bin/java
Couldn't figure out the Java version of /home/jenkins/jdk/bin/java
bash: /home/jenkins/jdk/bin/java: No such file or directory
[03/13/22 20:31:17] [SSH] Checking java version of /etc/alternatives/java_sdk/bin/java
[03/13/22 20:31:17] [SSH] /etc/alternatives/java_sdk/bin/java -version returned 1.8.0_282.
[03/13/22 20:31:17] [SSH] Starting sftp client.
[03/13/22 20:31:18] [SSH] Copying latest remoting.jar...
[03/13/22 20:31:29] [SSH] Copied 1,522,173 bytes.
Expanded the channel window size to 4MB
[03/13/22 20:31:30] [SSH] Starting agent process: cd "/home/jenkins" && /etc/alternatives/java_sdk/bin/java -jar remoting.jar -workDir /home/jenkins -jar-cache /home/jenkins/remoting/jarCache
Mar 13, 2022 8:31:30 PM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /home/jenkins/remoting as a remoting work directory
Mar 13, 2022 8:31:30 PM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /home/jenkins/remoting
<===[JENKINS REMOTING CAPACITY]===>channel started
Remoting version: 4.11.2
This is a Unix agent
Evacuated stdout
Agent successfully connected and online
Please let me know what ami doing wrong.