7

Question:

  • How do I set the timezone in jenkins in a docker container so that when you reboot the server it retains the setting?

So i'm running RHEL 7 on AWS with docker and jenkins (https://hub.docker.com/r/jenkinsci/blueocean/).

This article talks about how to change the timezone https://wiki.jenkins.io/display/JENKINS/Change+time+zone

essentially two ways

  1. run System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'America/New_York') in the script console which works but if you reboot the server or jenkins it reverts back
  2. add it to /etc/sysconfig/jenkins, unfortunately that directory does not exist as it's running in a container

However i have created a volume for the settings folder this way

docker run -d -p 8080:8080 -v $PWD/jenkins:/var/jenkins_home:z -t jenkinsci/blueocean 

this makes the home directory "jenkins-blue"

the directory listing is

4 -rw-r--r--.   1 <omitted user>  1647 Feb 27 17:00 config.xml
4 -rw-r--r--.   1 <omitted user>  3982 Feb 27 16:57 copy_reference_file.log
4 -rw-r--r--.   1 <omitted user>   966 Feb 27 17:04 credentials.xml
0 drwxr-xr-x.   3 <omitted user>    16 Feb 27 17:06 fingerprints
4 -rw-r--r--.   1 <omitted user>   156 Feb 27 16:57 hudson.model.UpdateCenter.xml
4 -rw-r--r--.   1 <omitted user>  1230 Feb 27 17:07 hudson.plugins.emailext.ExtendedEmailPublisher.xml
4 -rw-r--r--.   1 <omitted user>   370 Feb 27 16:58 hudson.plugins.git.GitTool.xml
4 -rw-------.   1 <omitted user>  1712 Feb 27 16:57 identity.key.enc
0 drwxr-xr-x.   2 <omitted user>    41 Feb 27 16:57 init.groovy.d
4 -rw-r--r--.   1 <omitted user>    94 Feb 27 16:58 jenkins.CLI.xml
4 -rw-r--r--.   1 <omitted user>     7 Feb 27 17:00 jenkins.install.InstallUtil.lastExecVersion
4 -rw-r--r--.   1 <omitted user>     7 Feb 27 17:00 jenkins.install.UpgradeWizard.state
4 -rw-r--r--.   1 <omitted user>   183 Feb 27 17:00 jenkins.model.JenkinsLocationConfiguration.xml
4 -rw-r--r--.   1 <omitted user>   171 Feb 27 16:57 jenkins.telemetry.Correlator.xml
0 drwxr-xr-x.   3 <omitted user>    18 Feb 27 17:00 jobs
0 drwxr-xr-x.   4 <omitted user>    37 Feb 27 16:58 logs
4 -rw-r--r--.   1 <omitted user>   907 Feb 27 16:58 nodeMonitors.xml
0 drwxr-xr-x.   2 <omitted user>     6 Feb 27 16:58 nodes
20 drwxr-xr-x. 106 <omitted user> 16384 Feb 27 16:59 plugins
4 -rw-r--r--.   1 <omitted user>   129 Feb 28 17:35 queue.xml
4 -rw-r--r--.   1 <omitted user>    64 Feb 27 16:57 secret.key
0 -rw-r--r--.   1 <omitted user>     0 Feb 27 16:57 secret.key.not-so-secret
4 drwx------.   4 <omitted user>  4096 Feb 27 17:14 secrets
0 drwxr-xr-x.   2 <omitted user>   182 Feb 27 16:59 updates
0 drwxr-xr-x.   2 <omitted user>    24 Feb 27 16:58 userContent
0 drwxr-xr-x.   3 <omitted user>    57 Feb 27 17:00 users
4 drwxr-xr-x.  11 <omitted user>  4096 Feb 27 16:57 war
0 drwxr-xr-x.   2 <omitted user>     6 Feb 27 16:58 workflow-libs
0 drwxr-xr-x.   4 <omitted user>    34 Feb 27 17:06 workspace

6 Answers 6

15

You may apply a TZ environment into your docker-compose like this:

image: jenkins/jenkins:alpine
container_name: jenkins
hostname: jenkins
restart: always
environment:
  - TZ=Asia/Shanghai
volumes:
  - ./jenkins/data:/var/jenkins_home
  - ./jenkins/bin/jenkins.war:/usr/share/jenkins/jenkins.war
2
  • 2
    and without docker compose just add -e TZ=Asia/Shanghai
    – csanchez
    Nov 8, 2019 at 9:36
  • 1
    This works when using the jenkins classic UI to view log files with time stamp lines. However, it fails when viewing the same log files using the blueocean UI. I guess blueocean doesn't honor TZ?
    – Gary
    Mar 10, 2021 at 19:11
6

Run it in System Management -> Script command line

System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 
'Asia/Shanghai')

https://wiki.jenkins.io/display/JENKINS/Change+time+zone

BTW, I add this in my Timed tasks:

TZ=Asia/Shanghai
40 15 * * 1-5

OR

docker run ... -e JAVA_OPTS=-Duser.timezone=Asia/Shanghai

https://github.com/jenkinsci/docker/issues/45

0
4

Dockerfile

FROM jenkins/jenkins:2.176.1

USER root

# Configure Timezone
RUN \
rm -f /etc/localtime && \
ln -s /usr/share/zoneinfo/America/Los_Angeles /etc/localtime && \
echo 'America/Los_Angeles' > /etc/timezone && \
dpkg-reconfigure --frontend noninteractive tzdata
1
  • This worked for me! Do necessarily need the echo 'America/Los_Angeles' > /etc/timezone? Jan 10, 2020 at 22:45
0

Simply Map the volume while running jenkins container

-v /etc/timezone:/etc/timezone:ro

0

environment:

  • TZ=Asia/Shanghai This work for me
0

I could set the Time Zone from Jenkins and the Docker container, by adding the following in the Dockerfile:

ENV TZ=America/Los_Angeles
...
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.