douchao9899
2017-10-02 18:48
浏览 148

用于构建Golang二进制文件的Docker映像

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/zoneinfo.zip: 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问答提供 功能建议

我有一个Go应用程序,该应用程序已构建为二进制文件并作为Docker映像分发。

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

  pod错误:紧急:打开/usr/local/go/lib/time/zoneinfo.zip:没有这样的文件或目录
   
 
 

此错误是由于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 github.com/lisitsky/go-site-search-string && \
        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/zoneinfo.zip 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 https://github.com/golang/go/raw/master/lib/time/zoneinfo.zip /usr/local/go/lib/time/zoneinfo.zip
    

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

    点赞 打赏 评论

相关推荐 更多相似问题