App is using wrong timesone

Hello, I have a Go application which needs a time. I host my application in cdg (Paris) so UTC+1. I set the timezone when building the docker container but the application always has UTC. Does anyone know how I can use the Europe/Paris timezone?

Hi!

Can you share how you set the timezone in your Docker image?

I wonder if an ENTRYPOINT script can be used to set the timezone when the VM boots up / starts running your application.

Sure, here if my Dockerfile im using to deploy the application:

FROM alpine:latest

RUN apk update && apk upgrade \
  && apk add ca-certificates tzdata \
  && rm -rf /var/cache/apk/*

WORKDIR /app

EXPOSE 8080
ARG TZ=Europe/Paris

COPY app /app/
COPY templates /app/templates

ENTRYPOINT ["./app"]

When I install Docker on my own linux machine hosted in the US the container got the correct time for Paris.

The mistake is probably to do with the use of ARG when you need ENV. ARG is for arguments used at build-time (passed from the docker build... command into the Dockerfile) whereas ENV is to set a default value for Environment Variables to be used at runtime – this article is a fantastic breakdown of ARG vs ENV.

A tiny change should fix this for you:

-ARG TZ=Europe/Paris
+ENV TZ=Europe/Paris

That said, it’s uncommon to do this: usually, you would set the timezone via the Environment at runtime. You can do this on Fly through your fly.toml, e.g:

[env]
TZ="Europe/Paris"

So you can completely remove TZ=Europe/Paris from your Dockerfile!

Also, a small note for future reference when debugging an issue where something works in one environment but not another: because your host machine is using Europe/Paris it appeared as if ARG was working for you locally (but not on Fly) because it was inheriting the configuration from your environment. You can often identify when this is happening by debugging with “strange” input that you know will definitely produce different results, for example, if you had run a test using America/New_York then you would have seen that it didn’t work outside of Fly either. That’s a tip that has helped me a lot with debugging infrastructure issues.

(Also, the reason it defaults to UTC is that in almost all cases, UTC is preferred for servers, and then when presenting a date/time to a user an application would normally convert it to the user’s local time. Due to daylight savings time, and various other quirks of time, using Europe/Paris for your server may cause some strange behaviour – so you may be best off with UTC if possible!)

1 Like

You could also get the region from the FLY_REGION env var and then select the timezone in your app.

If you hardcode it into an env var then all your app instances will have the same time zone.

I just learned what the difference between ARG and ENV is :smiley:

When using it with ENV everything works as i expected. Thank you. Ofcause i would have used the UTC timezone but at the progamm should to some things on a specific time i have to use the correct timezone.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.