How can I set the time zone in Dockerfile using gliderlabs/alpine:3.3

Question:

My Dockerfile is:

FROM gliderlabs/alpine:3.3
RUN set -x 
    && buildDeps='
        python-dev 
        py-pip 
        build-base 
    ' 
    && apk --update add python py-lxml py-mysqldb $buildDeps 
    && rm -rf /var/cache/apk/* 
    && mkdir -p /app
ENV INSTALL_PATH /app
ENV TZ=Asia/Shanghai
WORKDIR $INSTALL_PATH
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY requirements-docker.txt ./
RUN pip install -r requirements-docker.txt
COPY . .
RUN apk del --purge $buildDeps
ENTRYPOINT ["celery", "-A", "tasks", "worker", "-l", "info", "-B"]

I setted the timezone as Asia/Shanghai, but it did not work and gave me the UTC which had 8 hours deviation, the result is :

2016-01-24 11:25:07:[2016-01-24 03:25:07,893: WARNING/Worker-2] 2016-01-24 03:25:07.892718
2016-01-24 11:25:08:[2016-01-24 03:25:08,339: INFO/MainProcess] Task tasks.crawl[98c9a9fc-0817-45cb-a2fc-40320d63c41a] succeeded in 0.447403368002s: None
2016-01-24 11:27:07:[2016-01-24 03:27:07,884: INFO/Beat] Scheduler: Sending due task spider (tasks.crawl)

Then I tried other methods like:

RUN echo "Asia/Shanghai" > /etc/timezone && dpkg-reconfigure -f noninteractive tzdata

and

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

none of them did work, how can set the timezone? Thanks very much.

Asked By: thiiiiiking

||

Answers:

The usual workaround is to mount /etc/localtime, as in issue 3359

$ docker run --rm busybox date
Thu Mar 20 04:42:02 UTC 2014
$ docker run --rm -v /etc/localtime:/etc/localtime:ro  busybox date
Thu Mar 20 14:42:20 EST 2014
$ FILE=$(mktemp) ; echo $FILE ; echo -e "Europe/Brussels" > $FILE ; docker run --rm -v $FILE:/etc/timezone -v /usr/share/zoneinfo/Europe/Brussels:/etc/localtime:ro busybox date
/tmp/tmp.JwL2A9c50i 
Thu Mar 20 05:42:26 CET 2014

The same thread mentions (for ubuntu-based image though), but you already tried it.

RUN echo Europe/Berlin > /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata

(And I referred before to a similar solution)


Another option would be to build your own gliderlabs/docker-alpine image with builder/scripts/mkimage-alpine.bash.

That script allows you to set a timezone.

    [[ "$TIMEZONE" ]] && 
        cp "/usr/share/zoneinfo/$TIMEZONE" "$rootfs/etc/localtime"

You can see that image builder script used in Digital Ocean: Alpine Linux:

Generate Alpine root file system

  • Ensure Docker is running locally.
  • Download and unzip gliderlabs/docker-alpine.

    wget -O docker-alpine-master.zip https://github.com/gliderlabs/docker-alpine/archive/master.zip
    unzip docker-alpine-master.zip
    
  • Build the builder (export the right timezone first).

    export TIMEZONE=xxx
    docker build -t docker-alpine-builder docker-alpine-master/builder/
    
  • Build the root file system (change v3.3 to the Alpine version you want to build).

    docker run --name alpine-builder docker-alpine-builder -r v3.4
    
  • Copy the root file system from the container.

    docker cp alpine-builder:/rootfs.tar.gz .
    

Once you have the rootfs.tar.gz on your own filesystem, you can use it (as mentioned here) to build your own Alpine image, with the following Dockerfile:

FROM SCRATCH
ADD rootfs.tar.gz /

Once built, you can use that Alpine image with the right timezone.

Answered By: VonC

//dockerfile

RUN apk update && apk add tzdata 
     && cp -r -f /usr/share/zoneinfo/YOUR_TIMEZONE /etc/localtime
Answered By: 亚里士朱德

I‘m running my Docker on Mac Mojave,
In Dockerfile, I added the following after many failed tries:

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone &&
apt-get install tzdata &&
dpkg-reconfigure –frontend noninteractive tzdata

And it’s working fine.

Answered By: Yuan Wen

To Set timezone in Dockerfile you can use those lines

ENV TZ=Africa/Douala
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.