Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 632
thiiiiiking Avatar asked Jan 24 '16 05:01

thiiiiiking


People also ask

How do I change the date and time in a Docker container?

The easiest way to change the time in a Docker container is to change the time using 'date' command after connecting to the container. Though the time zone change usually reflects immediately, in some cases, the container needs a restart for the time to change.

How do I find the container time zone?

The directory /usr/share/zoneinfo in Docker contains the container time zones available. The desired time zone from this folder can be copied to /etc/localtime file, to set as default time. The containers created out of this Dockerfile will have the same timezone as the host OS (as set in /etc/localtime file).


2 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.

like image 163
VonC Avatar answered Oct 11 '22 11:10

VonC


//dockerfile

RUN apk update && apk add tzdata \
     && cp -r -f /usr/share/zoneinfo/YOUR_TIMEZONE /etc/localtime
like image 6
亚里士朱德 Avatar answered Oct 11 '22 09:10

亚里士朱德