Golang web application static files with Docker container

I am 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 example, patterns in code are discovered locally (is this a good practice?):

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

      

Docker container is used to deploy the application. As you can see in Dockerfile

, I need to copy all static files to a directory /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

      

I think this workaround should be considered incorrect as it violates standard Linux conventions (keeping executables and various data files separate). If anyone is using Docker to deploy Golang web applications, please share your experience:

  • How to store static content and how to detect it in your code?
  • What is the most correct method to deploy a web application with Docker containers?
+3


source to share


1 answer


As you pass the relative path to template.ParseGlob

, it will look for patterns relative to the current working directory, which you install on /go/bin

to ENTRYPOINT

.

I would suggest changing yours Dockerfile

to use the instruction WORKDIR

to set the working directory to /go/src/webserver

, thus avoiding the need to copy files to /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 can also use Flynn to deploy and manage your application (see here for a walkthrough of deploying a Go web application).

+5


source







All Articles