Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you change the Docker container TZ in spring?

I am using frolvlad/alpine-oraclejdk8 base image as recommended by spring: https://spring.io/guides/gs/spring-boot-docker/

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD gs-spring-boot-docker-0.1.0.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

I am running the image with -e TZ=/usr/share/zoneinfo/Europe/Paris

I am setting the env TZ but this doesn't change the TimeZone in my container.

How do you change the timezone for this image?

like image 867
Dimitri Kopriwa Avatar asked Dec 14 '22 00:12

Dimitri Kopriwa


1 Answers

Alpine Linux does not install timezone files by default to minimize the size of the Docker image.

You need to explicitly install the tzdata package and copy the zoneinfo file according to the timezone you want to set.

An example for Dockerfile is as follows:

FROM frolvlad/alpine-oraclejdk8:slim

RUN apk --update add tzdata && \
    cp /usr/share/zoneinfo/Europe/Paris /etc/localtime && \
    apk del tzdata && \
    rm -rf /var/cache/apk/*

Build the image:

$ docker build -t tztest .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM frolvlad/alpine-oraclejdk8:slim
 ---> 00d8610f052e
Step 2 : RUN apk --update add tzdata &&     cp /usr/share/zoneinfo/Europe/Paris /etc/localtime &&     apk del tzdata &&     rm -rf /var/cache/apk/*
 ---> Running in 5b6a014fdaf3
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.5/community/x86_64/APKINDEX.tar.gz
(1/1) Installing tzdata (2016i-r0)
Executing busybox-1.25.1-r0.trigger
OK: 14 MiB in 15 packages
(1/1) Purging tzdata (2016i-r0)
Executing busybox-1.25.1-r0.trigger
OK: 10 MiB in 14 packages
 ---> 6c379ddd4186
Removing intermediate container 5b6a014fdaf3
Successfully built 6c379ddd4186

Check date of the container:

$ docker run -it --rm tztest date
Thu Mar  9 16:34:54 CET 2017

EDIT:

If you want to set timzone at runtime not on build, install the tzdata package and do not delete on build:

FROM frolvlad/alpine-oraclejdk8:slim

RUN apk --no-cache add tzdata

Build the image:

$ docker build -t tztest .

And then, you can set environment variable TZ at runtime:

$ docker run -it --rm -e TZ="Europe/Paris" tztest date
Fri Mar 10 01:59:27 CET 2017
like image 198
minamijoyo Avatar answered Feb 04 '23 07:02

minamijoyo