Docker의 timezone 과 java application 의 timezone의 sync를 맞추자! 

혹은.. 서버의 timezone과 java application의 sync...

 

docker의 timezone을 설정하고, 

spring boot인 application 을 띄웠는데..

 

 

다음과 같이 localtime을 지정해주고 docker를 띄워주면

 

docker run -d -v /etc/localtime:/etc/localtime:ro

 

/ # date
Thu Mar 12 06:55:37 UTC 2020

date 명령어로 timezone이 잘 바뀐걸 확인 할 수 있다.

 

 

그런데..

spring의 log를 보면 timezone의 시간과 안맞는 것이다..

 

그래서 검색해보니 

log의 시간은 jvm의 시간을 물고가는데 

혹은, java application 단에서도 jvm의 시간을 가져오기 때문이다.

 

그래서 하단의 stack overflow를 보고 해결하였다. 한가지 옵션을 더주니 해결 되었다.

 

docker run -d -v /etc/localtime:/etc/localtime:ro -v /usr/share/zoneinfo/Asia/Seoul:/etc/timezone:ro

 

그리고 다시 재기동~ 후 확인해보니

date도, log도 모두 같은 시간인걸 확인하였다.

 

 

그런데 저 명령어가 무슨 의미가 있어서 잘되는지 알고 싶은데.. 

찾다가 정확하게 이해는 못하였다.

 

하단 github 참조

 

As I experience it, containers use their internal timezone, not the timezone of their host OS.

 

So, let's unravel this a little bit (hopefully).

  • containers share the kernel
  • the kernel tracks the "current time" (note this is "time", not "timezone"), and it is tracked simply as the "number of seconds since the epoch (1970-01-01 00:00:00 UTC)" (hence the name, "Unix Timestamp")
  • userspace (which includes tools like date) has a notion of "timezone", and it usually comes from "/etc/localtime" (but this does sometimes depends on the utility, some read "/etc/timezone" instead, and some update "/etc/localtime" from the contents of "/etc/timezone")

The Gentoo Wiki's "System time" entry also has some great information that helps untangle

 

 

저 말을 해석해보면.

컨테이너는 host OS의 timezone이 아니라 각각 내부의 timezone을 사용하고 

 

그아래 문구를 해석해보자면.. 결국 timezone은 여러군데서 읽어온다는 뜻인거 같다... 허허

 


 

 

그리고 찾다보니 docker가 아닌 서버에서도 이런 경우가 있는데 다음 블로그에 그 방법이 자세히 나와있다.

 

https://irontooth.tistory.com/47

 

java Calendar 시간과 서버시간 불일치 처리

1) Calendar runTimeCal = Calendar.getInstance(); System.out.println ("현재 타임존 : " + runTimeCal.getTimeZone().getDisplayName() ); TimeZone tz = TimeZone.getTimeZone("GMT+09:00"); runTimeCal.setTi..

irontooth.tistory.com

 

java -jar -Duser.timezone=Asia/Seoul 

해당방법처럼 매번 기동할때마다 이렇게 설정해주던가.. 아니면 start shell에 입력해주면 될거같다

아니면 

WAS에서 하는 방법도 있다.

 


 

참고 : 

 

https://stackoverflow.com/questions/34534079/how-to-sync-the-time-of-a-java-application-running-on-docker-container

 

How to sync the time of a java application running on docker container?

I am docker file like this: FROM anapsix/alpine-java:jre8 ADD service-god-sac-1.0.0-SNAPSHOT.jar app.jar ENTRYPOINT ["java", "-Xmx64m", "-XX:MaxMetaspaceSize=64m", "-jar", "/app.jar"] When I co...

stackoverflow.com

 

https://github.com/moby/moby/issues/3359

 

container timezone · Issue #3359 · moby/moby

As I experience it, containers use their internal timezone, not the timezone of their host OS. I currently work around this issue by providing a start script that takes a typezone parameter which i...

github.com

 

'DEVELOP > DevOps' 카테고리의 다른 글

내가 헷갈려서 쓰는 Git..기능  (0) 2019.07.12

+ Recent posts