Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins in docker - how to change timezone

Tags:

docker

jenkins

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
like image 751
Lacer Avatar asked Mar 08 '19 01:03

Lacer


People also ask

How do I change the timezone in Jenkins?

To see the time zone currently set, go to jenkins_server/systemInfo and see the user. timezone system property. You may want to change the time zone displayed to match your own time zone. By going to your user configuration page, you can set the User Defined Time Zone to match your own.

How do I change the date and time in a Docker container?

The easiest way to change the time in a Docker container is to change the time using 'date' command after connecting to the container. Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.

How do I find the container time zone?

The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time. The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).


Video Answer


2 Answers

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
like image 187
ZhouX Avatar answered Nov 06 '22 18:11

ZhouX


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

like image 30
Vassago Avatar answered Nov 06 '22 18:11

Vassago