Docker中的Jenkins-如何更改时区

6mw9ycah  于 2023-10-17  发布在  Jenkins
关注(0)|答案(6)|浏览(243)

问:

  • 如何在Docker容器中设置jenkins的时区,以便在重启服务器时保持设置?

所以我在AWS上使用Docker和Jenkins(https://hub.docker.com/r/jenkinsci/blueocean/)运行RHEL 7。
这篇文章讨论了如何更改时区https://wiki.jenkins.io/display/JENKINS/Change+time+zone
基本上有两种方式
1.在脚本控制台中运行System.setProperty('org.apache.commons.jelly.tags.fmt. timeZone','America/New_York'),它可以工作,但如果重新启动服务器或jenkins,它会恢复
1.将其添加到/etc/sysconfig/jenkins,不幸的是,该目录不存在,因为它运行在容器中
然而,我已经创建了一个卷的设置文件夹这样

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

这使主目录“jenkins-blue”
目录列表是

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
xxhby3vn

xxhby3vn1#

您可以像这样将TZ环境应用到docker-compose中:

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
mefy6pfw

mefy6pfw2#

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
顺便说一句,我在我的定时任务中添加了这个:

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

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

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

b09cbbtk

b09cbbtk3#

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
lg40wkob

lg40wkob4#

运行jenkins容器时简单地Map卷
-v /etc/timezone:/etc/timezone:ro

f0brbegy

f0brbegy5#

工作环境:

  • TZ=亚洲/上海
disho6za

disho6za6#

我可以从Jenkins和Docker容器中设置时区,方法是在Dockerfile中添加以下内容:
ENV TZ=美国/洛杉矶
...
运行ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

相关问题