본문 바로가기

프로젝트 설정/Docker

[Docker] 🐳 docker container 시간대 변경하기 (feat. timezone)

반응형

Docker의 기본 시간은 UTC 로 되어있어 KST 사용하려면 바꾸는 작업을 해야한다.

여러방법이 있다고 하는데 잘 모르니 블로그에서 봤던것들을 그냥 적어본다.

도커 시작시 설정 (run/compose)

tz 속성 설정 (-e TZ=Asia/Seoul)

docker run -it --name dockertimezone -e TZ=Asia/Seoul ubuntu

시스템 환경변수 tz속성을 줘서 timezone을 설정하는 것은 경량화된 이미지에선 아래와 같은 요인으로 안될 수 있다.

1. tz속성은 timezone 정보(/usr/share/zoneinfo)가 필요한데 tzdata가 없을 경우 timezone 정보도 없어 안될 수 있다.

(tzdata가 없는 경우  없어 안되는건지 아니면 tzdata 자체가 필요한지는 정확하지 않다.)

이럴 경우 tzdata 를 install 하거나 ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime 심볼릭 링크를 형성하여 

2. ubuntu/linux 등 기본적으로 깔려져 있는 요소들이 없는 등 다른 다양한 요인으로 안될 수 있다.

 

안될 경우 다양한 옵션을 추가로 해주어야해 run 보다 docker-compose.yml 나 docker file로 작성하는걸 추천한다.

 

ex) host의 localtime 가져가기

docker run -it --name dockertimezone -v /etc/localtime:/etc/localtime:ro -e TZ=Asia/Seoul ubuntu

ex) tzdata install

docker run -it --name dockertimezone -e TZ=Asia/Seoul \
apt-get update && apt-get install -y tzdata && \
rm /etc/localtime && \
ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
ubuntu

도커 실행 중 설정

여러가지 방법이 있다.

방법은 3 에서 볼 수 있다.

1. timezone 있는지 확인

timezone 정보가 있는지 /usr/share/zoneinfo 경로에서 확인

2. 없을 경우 tzdata install

apt udpate
apt install tzdata

# or

apt-get udpate
apt-get install tzdata

3. 설정방법

timedatectl, localtime 폴더에 설정(cp,  symbolic link), tzselect 등 다양한 방법이 있다.

3-1. timedatectl 이용

timedatectl set-timezone Asia/Seoul

error

더보기

3-1. error 1

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

해결

apt-get udpate

 

3-2. localtime cp

 cp -p /usr/share/zoneinfo/Asia/Seoul /etc/localtime

error

더보기

3-2. error 1 (해결 못함)

# error
cp: cannot create regular file '/etc/localtime': Read-only file system

 

3-3. symbolic link 생성

cp 와 비슷하지만 조금 다른 !

한국

ln -sf /usr/share/zoneinfo/Asia/Seoul /etc/localtime

UTC/GMT

ln -sf /usr/share/zoneinfo/GMT /etc/localtime

 

3-4. tzselect (사용자별)

# timezone 선택
# 1회성
tzselect

# 영구 적용
# vi ~/.profile or ~/.bashrc
export TZ='Asia/Seoul'
tzselect

Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
1) Africa                                                            7) Europe
2) Americas                                                          8) Indian Ocean
3) Antarctica                                                        9) Pacific Ocean
4) Asia                                                             10) coord - I want to use geographical coordinates.
5) Atlantic Ocean                                                   11) TZ - I want to specify the timezone using the Posix TZ format.
6) Australia
#? 4
Please select a country whose clocks agree with yours.
 1) Afghanistan                  13) Cyprus                       25) Kazakhstan                   37) Oman                         49) Taiwan
 2) Antarctica                   14) East Timor                   26) Korea (North)                38) Pakistan                     50) Tajikistan
 3) Armenia                      15) French Southern Territories  27) Korea (South)                39) Palestine                    51) Thailand
 4) Azerbaijan                   16) Georgia                      28) Kuwait                       40) Philippines                  52) Turkmenistan
 5) Bahrain                      17) Hong Kong                    29) Kyrgyzstan                   41) Qatar                        53) United Arab Emirates
 6) Bangladesh                   18) India                        30) Laos                         42) R?union                      54) Uzbekistan
 7) Bhutan                       19) Indonesia                    31) Lebanon                      43) Russia                       55) Vietnam
 8) Brunei                       20) Iran                         32) Macau                        44) Saudi Arabia                 56) Yemen
 9) Cambodia                     21) Iraq                         33) Malaysia                     45) Seychelles
10) China                        22) Israel                       34) Mongolia                     46) Singapore
11) Christmas Island             23) Japan                        35) Myanmar (Burma)              47) Sri Lanka
12) Cocos (Keeling) Islands      24) Jordan                       36) Nepal                        48) Syria
#? 27

The following information has been given:

        Korea (South)

Therefore TZ='Asia/Seoul' will be used.
Selected time is now:   Tue Jan 31 19:24:04 KST 2023.
Universal Time is now:  Tue Jan 31 10:24:04 UTC 2023.
Is the above information OK?
1) Yes
2) No
#? 1

You can make this change permanent for yourself by appending the line
        TZ='Asia/Seoul'; export TZ
to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Seoul
root@ubuntu:/# date
Tue Jan 31 19:24:27 KST 2023

error

더보기

tzselect error 1

tzselect
/usr/bin/tzselect: line 180: /usr/share/zoneinfo/iso3166.tab: No such file or directory
/usr/bin/tzselect: time zone files are not set up correctly

tzselect error 1 해결방안

apt-get install tzdata

tzdata install error 1

tzdata install
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 333 kB of archives.
After this operation, 3840 kB of additional disk space will be used.
Ign:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 tzdata all 2022f-0ubuntu0.22.04.1
Err:1 http://security.ubuntu.com/ubuntu jammy-updates/main amd64 tzdata all 2022f-0ubuntu0.22.04.1
  404  Not Found [IP: 185.125.190.39 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/t/tzdata/tzdata_2022f-0ubuntu0.22.04.1_all.deb  404  Not Found [IP: 185.125.190.39 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

tzdata install error 1

apt-get update

 

타임존 확인

date

상세 타임존 확인

# install timedatectl
# apt-get install systemd
timedatectl

error 1 (해결 못함..ㅠ)

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down

 

참조

회사분 Notion

https://sarc.io/index.php/cloud/1959-configure-container-image-timezone

https://seongmin.dev/setting-up-docker-container-timezone

https://mycup.tistory.com/241

https://choelhee.tistory.com/30

 

 

https://young-cow.tistory.com/85

https://jstar0525.tistory.com/360

https://www.lesstif.com/lpt/ubuntu-linux-timezone-setting-61899162.html

https://stynxh.github.io/2020-07-26-set-timezone-when-ubuntu-docker-image-build/ (Docker File)

https://forgiveall.tistory.com/591

 

https://gist.github.com/alyleite/ca8b10581dbecd722d9dcc35b50d9b2b

https://domdom.tistory.com/394

 

https://forums.debian.net/viewtopic.php?t=15348 

 

 

 

 

 

 

 

반응형