使用Docker容器部署Golang Web应用的静态文件。

huangapple go评论79阅读模式
英文:

Deploying Golang web app static files with Docker container

问题

我正在开发一个小型的Web应用程序,其中包含一些静态文件(配置文件和HTML模板):

├── 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

# 复制静态文件
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应用程序的最合适方法是什么?
英文:

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

得分: 8

由于您将相对路径名传递给template.ParseGlob,它将在相对于当前工作目录的位置查找模板,而您在ENTRYPOINT中将当前工作目录设置为/go/bin

我建议修改您的Dockerfile,使用WORKDIR指令将工作目录设置为/go/src/webserver,这样就不需要将文件复制到/go/bin中,例如:

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

您还可以考虑使用Flynn来部署和管理您的应用程序(参见这里有关部署Go Web应用程序的演示)。

英文:

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).

huangapple
  • 本文由 发表于 2015年5月6日 01:07:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/30059122.html
  • deployment
  • docker
  • go
  • static-files
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定