简体   繁体   中英

docker-compose: container timezone

I'm using Docker Compose to run my stack in my dev environment, but I'm having issues with the timezone in the container being one hour behind my own timezone, breaking mission-critical things and making development very difficult.

I figured this would be a common issue, so I've searched far and wide, but the only two solutions I've found don't work (don't work: no effect).

I've tried two things:

Attempt 1 - symlinking volumes from the host
By attempting to mount /etc/timezone and /etc/localtime as ro -volumes, I was hoping the container would have the same timezone as the host. No effect.

Attempt 2 - setting the variables in the command
Instead of letting Docker Compose use the ENTRYPOINT specified in my Dockerfile , I set up the command in the docker-compose.yml -file thus:

environment:
    - APPLICATION_ENV=dev-docker
    - TZ=Europe/Stockholm
build: ../../core/document
command: >
  sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &&
  echo $TZ > /etc/timezone &&
  exec /go/bin/documents"

Again, no effect at all.

Is there no official way of setting the timezone in Docker containers? I feel this should be a critical issue to more users than me.

Thanks.

Edit : the dockerfile from the core/documents -project, as requested.

# This file is intended for use with the GitLab CI tool where the binary has already been built.
FROM golang:1.9.2

# The binary is built and downloaded to the current directory by GitLab CI.
COPY ./documents /go/bin

# Run the program.
ENTRYPOINT /go/bin/documents

I have had problems with timezones as well, for me this made the trick:

environment:
  - TZ=Europe/Stockholm

I see that I never answered this properly. I did find the solution.

TL;DR:
Alpine only checks the /usr/share/zoneinfo/Europe/Stockholm -file, not the /etc/localtime -file.

Longer answer
Basically, the step that was missing was that Alpine doesn't check the /etc/localtime -path - it only checks the /usr/share/zoneinfo/Europe/Stockholm -path (change to your timezone).

In my case, I decided on an arguably unorthodox solution. I wanted to:

  • Only copy the timezone I actually wanted, as the tzdata package was twice the size than the entire rest of the container; base image and by binary both
  • Run the proper apk tel tzdata -command and not clean up myself in some weird rm -way.
  • Not clutter the tzdata -packages with other files, or write stuff to it manually (aside from a symlink)

Therefore, I decided to install tzdata , copy the time zone I wanted to /etc/localtime and then create a symlink for Alpine to still read the file.

I have the following in my Dockerfile :

RUN \
    apk --update add curl bash nano tzdata && \
    cp /usr/share/zoneinfo/Europe/Stockholm /etc/localtime && \
    echo "Europe/Stockholm" > /etc/timezone && \
    apk del tzdata && \
    rm -r /var/cache/apk/* && \
    mkdir -p /usr/share/zoneinfo/Europe && \
    ln -s /etc/localtime /usr/share/zoneinfo/Europe/Stockholm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM