dockerコンテナのAlpine Linuxのタイムゾーンを設定する

dockerコンテナのAlpine Linuxのタイムゾーンを設定する

dockerコンテナのAlpine Linuxのタイムゾーンを設定する手順を記述してます。

環境

  • OS Rocky Linux release 8.4 (Green Obsidian)
  • docker 20.10.7

タイムゾーン設定

タイムゾーンがデフォルトだとUTC時間になっているので、これをJST時間に変更します。

docker-compose.yml用意

まずは以下の内容で、「docker-compose.yml」ファイルを用意します。
「build」により、次に用意する「Dockerfile」でDockerイメージをビルドします。

version: '3'
services:
  site:
    build: .
    ports:
      - "88:80"

次にbuild用の「Dockerfile」を同じ階層に、以下の内容で用意しておきます。これでタイムゾーンを設定します。

FROM httpd:alpine

RUN apk add tzdata && \
    cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime && \
    echo "Asia/Tokyo" > /etc/timezone && \
    apk del tzdata

buildします。

$ docker-compose build

Building site
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM httpd:alpine
alpine: Pulling from library/httpd
a0d0a0d46f8b: Already exists 
3152ee199245: Pull complete 
b593f3373482: Pull complete 
354fd86e4009: Pull complete 
d78cac68daf9: Pull complete 
Digest: sha256:fa9e25cc424c7d55448acfc7e4c1b5f93715d4143a8c0cdd04c271a1ad735f91
Status: Downloaded newer image for httpd:alpine
 ---> dc1f741bcc09
Step 2/2 : RUN apk add tzdata &&     cp /usr/share/zoneinfo/Asia/Tokyo /etc/localtime &&     echo "Asia/Tokyo" > /etc/timezone &&     apk del tzdata
 ---> Running in b71562f63b4f
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2021c-r0)
Executing busybox-1.33.1-r3.trigger
OK: 52 MiB in 35 packages
(1/1) Purging tzdata (2021c-r0)
Executing busybox-1.33.1-r3.trigger
OK: 49 MiB in 34 packages
Removing intermediate container b71562f63b4f
 ---> 0e694d7b91f6
Successfully built 0e694d7b91f6
Successfully tagged al_site:latest

起動します。

$ docker-compose up -d

Creating network "al_default" with the default driver
Creating al_site_1 ... done

確認

タイムゾーンがJSTになっているか確認してみます。

$ docker exec -i al_site_1 date

Thu Oct 21 14:39:22 JST 2021

JSTに設定されていることが確認できます。