Warm tip: This article is reproduced from serverfault.com, please click

Golang Server 404 on Static Pages

发布于 2020-11-29 19:56:25

I am working on a compiled Golang server that displays a home page, and upload page, and a file repository. It can also upload a file to its backend. The goal is to put this into a container and use it.

Code: https://github.com/thatMacAdmin/go-webserver

If I do the following on my local machine: go run webserver.go

Then it works as expected. The page in /static/index.html loads. I can upload files etc (as long as the repo exists in the right location). However when I build this and put it in a docker container, the repo file list works, and the upload endpoint exists, however the two static html pages get 404 errors.

What am I doing wrong? Why doesn't http://localhost:8080 work to display my home page in the container but it does work with the go run method?

Thanks, Ed

Questioner
Edward S.
Viewed
0
Edward S. 2020-12-18 12:33:55

The issue here was that Docker uses / as its relative path unless you tell it otherwise. In this instance my code expected the relative path to be where my server binary was located. In my case:

/server

Setting the WORKDIR option correctly in my Dockerfile fixed this.

Example:

FROM alpine:latest
RUN mkdir -p /server/static
COPY webserver /server
COPY static /server/static
WORKDIR /server
ENTRYPOINT ["/server/webserver"] 
EXPOSE 8080