我的工作,有一些靜態文件(CONFIGS和HTML模板)一個小型Web應用程序的靜態文件:部署Golang 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"))
}
泊塢容器用於應用程序部署。正如你可以在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 ==」「',您可以選擇回退到同一目錄中的硬編碼路徑。像https://github.com/kelseyhightower/envconfig這樣的軟件包可以很有用,如果你有多個選項可以通過。 – elithrar
另外,根據約定:二進制文件通常應該*存儲在'/ opt /'或'/ opt/usr/local/bin',具體取決於你如何解釋文檔。 '$ HOME/bin'也是可以接受的。 templates/config文件可以共同位於'/ opt /'或潛在地'/ etc//...' - 記住權限可能是一個問題。 –
elithrar