dongqian5639 2015-05-05 17:07
浏览 175
已采纳

使用Docker容器部署Golang Web应用程序静态文件

I'm working on a small web application that has some static files (configs and html templates):

├── Dockerfile
├── manifest.json
├── session
│   ├── config.go
│   ├── handlers.go
│   └── restapi_client.go
├── templates
│   ├── header.tmpl
│   └── index.tmpl
└── webserver.go

For instance, templates in the code are discovered with a local path (is it a good practice?):

func init() {
    templates = template.Must(template.ParseGlob("templates/*.tmpl"))
}

Docker container is used for app deployment. As you can see in the Dockerfile, I have to copy all the static files in /go/bin directory:

FROM golang:latest

ENV PORT=8000

ADD . /go/src/webserver/
RUN go install webserver
RUN go get webserver

# Copy static files
RUN cp -r /go/src/webserver/templates /go/bin/templates
RUN cp -r /go/src/webserver/manifest.json /go/bin/manifest.json

EXPOSE $PORT
ENTRYPOINT cd /go/bin && PORT=$PORT REDIRECT=mailtest-1.dev.search.km /go/bin/webserver -manifest=manifest.json

I think this workaround should be considered as incorrect since it violates standard Linux conventions (separate storing of the executable files and various data files). If anyone uses Docker for Golang web application deployment too, please share your experience:

  • How do you store static content and how do you discover it in your code?
  • What is the most proper method of deploying web application with a Docker containers?
  • 写回答

1条回答 默认 最新

  • duanbinren8906 2015-05-05 22:20
    关注

    Since you are passing a relative pathname to template.ParseGlob, it will look for templates relative to the current working directory, which you are setting to /go/bin in your ENTRYPOINT.

    I would suggest modifying your Dockerfile to use the WORKDIR instruction to set the working directory to /go/src/webserver, which would avoid the need to copy files into /go/bin, for example:

    FROM golang:latest
    ADD . /go/src/webserver
    WORKDIR /go/src/webserver
    RUN go get
    RUN go install
    ENV PORT=8000
    ENV REDIRECT=mailtest-1.dev.search.km
    EXPOSE 8000
    ENTRYPOINT /go/bin/webserver -manifest=manifest.json
    

    You could also consider using Flynn to deploy and manage your application (see here for a walkthrough of deploying a Go web app).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题