3

In a Go program I call time.LoadLocation("Europe/Berlin") and it returns an error saying open /usr/local/go/lib/time/zoneinfo.zip: no such file or directory, even though in the container (running alpine:3.9 with tzdata installed) /usr/share/zoneinfo/Europe/Berlin exists and, according to the docs, should take precedence over the zip file. The same program finds the file on my machine (Arch Linux). The executable got statically linked on my machine and then copied into the container. I tried Go 1.11.5 and 1.10.3.

I built the executable with:

CGO_ENABLED=0 go build -a -ldflags "-s" -o gocake_static

I'm looking for any ideas that help me identify the problem.

rausch
  • 3,148
  • 2
  • 18
  • 27
  • Did you specify the container's os and arch when you build that executable? Can you share the command with which you build the executable? – mkopriva Feb 14 '19 at 16:46
  • ... is the `ZONEINFO` env variable set on the container, and if so what is its value? – mkopriva Feb 14 '19 at 16:49
  • @mkopriva No, I didn't. It should be the same architecture, though, no? I added the build command to my question. – rausch Feb 14 '19 at 16:51
  • @mkopriva Setting the `ZONEINFO` variable doesn't seem to make a difference. I tried setting it to `/usr/share/zoneinfo`. – rausch Feb 14 '19 at 16:51
  • 1
    Is the `tzdata` package installed? (In the official Ubuntu images it is not). – kostix Feb 14 '19 at 17:12
  • @kostix Yes it is. – rausch Feb 14 '19 at 17:23
  • Please see how the Go runtime [looks for the timezone info](https://github.com/golang/go/blob/35bb62e60a7779ff82c3067903b3306ff8666471/src/time/zoneinfo.go#L269) (for 1.11.5); [here](https://github.com/golang/go/blob/35bb62e60a7779ff82c3067903b3306ff8666471/src/time/zoneinfo_unix.go#L19) is where the locations are on UNIX-compatibles. Basically, it appears that the idea of Alpine Linux about where the tzdata is located is different from that of the Go stdlib. – kostix Feb 14 '19 at 18:11
  • What you're seeing is the failure of the stdlib code to find the tzdata in any of the known built-in locations, so the last-resort one, which attempts to find a zip archive with the tzdata in the root directory of the Go installation, fails, and is shown. – kostix Feb 14 '19 at 18:14
  • I couldn't reproduce the problem under `go:1.11.5` and `alpine:3.9` with `tzdata` installed. I built the binary ( `GOOS=linux` `GOARCH=amd64` `CGO_ENABLED=0` ) in Mac and put it in an alpine container. I didn't set `ZONEINFO` variable. Are there other env variables may cause the problem? – Hunsin Feb 15 '19 at 08:18

1 Answers1

1

if you use only one static zoneinfo. maybe FixedZone can solve your problem.

It does not require timezone.zip, thus no need to download zoneinfo.zip and set env ZONEINFO in Dockerfile.

for example

loc := time.FixedZone("Europe/Berlin", 1*60*60)
fmt.Println(time.Now().In(loc).Format("2006-01-02 15:04:05"))
Lifei Chen
  • 575
  • 4
  • 17