2017-10-02 18:48
浏览 148


I have a Go application that I build into a binary and distribute as a Docker image.

Currently, I'm using ubuntu as my base image, but this causes an issue where if a user tries to use a Timezone other than UTC or their local timezone, they get an error stating:

pod error: panic: open /usr/local/go/lib/time/ no such file or directory

This error is caused because the LoadLocation package in Go requires that file.

I can think of two ways to fix this issue:

  1. Continue using the ubuntu base image, but in my Dockerfile add the commands: RUN apt-get install -y tzdata

  2. Use one of Golang's base images, eg. golang:1.7.5-alpine.

What would be the recommended way? I'm not sure if I need to or should be using a Golang image since this is the container where the pre-built binary runs. My understanding is that Golang images are good for building the binary in the first place.

图片转代码服务由CSDN问答提供 功能建议


当前,我正在使用 ubuntu 作为我的基本映像,但这会导致一个问题:如果用户尝试使用UTC或其本地时区以外的时区,则会收到错误消息 说明:


此错误是由于Go中的 LoadLocation 包引起的 需要该文件。


  1. 继续使用 ubuntu 基本映像,但在我的Dockerfile中添加命令: RUN apt-get install -y tzdata

  2. 使用一个 Golang的基本图像,例如 golang:1.7.5-alpine

    推荐的方法是什么? 我不确定是否需要或应该使用Golang映像,因为这是运行预构建二进制文件的容器。 我的理解是Golang图像最适合首先构建二进制文件。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dpnfjx755573 2017-10-03 18:39

    This is more of a vote, but apt-get is what we (my company's tech group) do in situations like this. It gives us complete control over the hierarchy of images, but this is assuming you may have future images based on this one.

    点赞 打赏 评论
  • douyue8364 2017-10-02 19:24

    I prefer to use multi-stage build. On 1st step you use special golang building container for installing all dependencies and build an application. On 2nd stage I copy binary file to empty alpine container. This allows having all required tooling and minimal docker image simultaneously (in my case 6MB instead of 280MB).

    Example of Dockerfile:

    # build stage
    FROM golang:1.8
    ADD . /src
    RUN set -x && \
        cd /src && \
        go get -t -v && \
        CGO_ENABLED=0 GOOS=linux go build -a -o goapp
    # final stage
    FROM alpine
    WORKDIR /app
    COPY --from=0 /src/goapp /app/
    ENTRYPOINT /goapp
    EXPOSE 8080
    点赞 打赏 评论
  • duanmajing9332 2017-10-02 23:28

    You can use the system's tzdata. Or you can copy $GOROOT/lib/time/ into your image, which is a trimmed version of the system one.

    点赞 打赏 评论
  • dtpf76658 2019-08-22 17:03

    Since not all OS have localized timezone installed, this is what I did to make it work:

    ADD /usr/local/go/lib/time/

    The full example of multi-step Docker image for adding timezone is here

    点赞 打赏 评论

相关推荐 更多相似问题