본문 바로가기

개발/Docker

How to set custom timezone on Docker alpine version, 알파인 리눅스를 사용하는 도커 컨테이너의 timezone 설정

Korean

도커 컨테이너 중 -alpine으로 끝나는 tag의 이미지는 알파인 리눅스를 기반으로 제작된 것이다.

대부분 wheezy나 slim 버전에 비해 용량이 최소 5분의1 밖에 하지 않기때문에, AWS ECS 와 같은곳에 올려두고 사용할 사용자들은 구미가 당기기 마련이다.


어쨌거나, 저장공간 리소스를 적게 잡아먹는 장점은 최대의 단점이 될 수 있다. 왜냐하면, 대부분의 운영체제(와 이를 기반으로한 docker이미지)에서 정상작동하는 기능들이 없는 명령어라며 오류를 뱉어낼 수 있기 떄문이다.

시스템의 타임존을 설정하는 tzdata와 같은 유틸리티도 알파인 리눅스에서는 기본적으로 빠져있는데, 이를 사용하여 Docker Container만의 타임존을 사용하기 위해서는 Dockerfile에 다음과 같은 설정이 꼭 들어가 있어야한다.

RUN apk --no-cache add tzdata && \
        cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \
        echo "Asia/Seoul" > /etc/timezone


CMD나 ENTRYPOINT를 선언하기 전, 적절한 위치에다 RUN 명령어를 넣어두면 KST로 타임존이 세팅되게 된다.



English

If you use docker image that has "-alpine" postfix tag, that means the image is based on Alpine Linux.

Images based on Alpine Linux take up a small amount of space (~50MB) thanks to the lightweight operating system.

However, these advantages can be disadvantageous because they do not include programs that are considered essential in other operating systems.

Programs such as tzdata, which sets the system timezone, are not installed by default in alpine linux. So you must define a separate installation in Dockerfile.

Add the following code to the appropriate location in your Dockerfile:


RUN apk --no-cache add tzdata && \
        cp /usr/share/zoneinfo/Asia/Seoul /etc/localtime && \
        echo "Asia/Seoul" > /etc/timezone

This sets your docker container's timezone into KST ('Asia/Seoul')

If you want to use your own timezone, you can modify the Asia/Seoul section as desired.



Reference

http://devmas.tistory.com/entry/Docker-Container%EC%9D%98-Timezone%EC%9D%84-Host-OS%EC%99%80-%EB%A7%9E%EC%B6%94%EA%B8%B0

https://wiki.alpinelinux.org/wiki/Setting_the_timezone