【发布时间】:2015-07-15 13:12:10
【问题描述】:
我正在开发一个包含一些静态文件(配置和 html 模板)的小型 Web 应用程序:
├── Dockerfile
├── manifest.json
├── session
│ ├── config.go
│ ├── handlers.go
│ └── restapi_client.go
├── templates
│ ├── header.tmpl
│ └── index.tmpl
└── webserver.go
例如,代码中的模板是通过本地路径发现的(这是一个好习惯吗?):
func init() {
templates = template.Must(template.ParseGlob("templates/*.tmpl"))
}
Docker 容器用于应用部署。正如您在Dockerfile 中看到的,我必须复制/go/bin 目录中的所有静态文件:
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
我认为这种解决方法应该被认为是不正确的,因为它违反了标准的 Linux 约定(单独存储可执行文件和各种数据文件)。如果有人也使用 Docker 进行 Golang Web 应用程序部署,请分享您的经验:
- 如何存储静态内容以及如何在代码中发现它?
- 使用 Docker 容器部署 Web 应用程序最合适的方法是什么?
【问题讨论】:
-
我会用环境变量替换代码中的静态模板路径 - 例如
os.Getenv("TEMPLATE_PATH")然后在您的 Dockerfile 中使用ENV /path/to/template/file进行设置。如果TEMPLATE_PATH == "",您可以选择回退到同一目录中的硬编码路径。如果您有多个选项可以传入,像 github.com/kelseyhightower/envconfig 这样的包会很有用。 -
此外,就约定而言:二进制文件通常应存储在
/opt/或/usr/local/bin中,具体取决于您如何解释文档。$HOME/bin也是可以接受的。模板/配置文件可以位于/opt/或/etc/<yourapp>/...- 请记住权限可能是个问题。
标签: deployment go docker static-files