Reputation: 720
This is my Dockerfile
FROM golang:alpine AS build
RUN apk --no-cache add gcc g++ make git
WORKDIR /go/src/app
COPY . .
RUN go get ./...
RUN GOOS=linux go build -ldflags="-s -w" -o ./bin/web-app ./main.go
FROM alpine:3.9
RUN apk --no-cache add ca-certificates
WORKDIR /usr/bin
COPY --from=build /go/src/app/bin /go/bin
EXPOSE 80
ENTRYPOINT /go/bin/web-app --port 80
And this is a simple main.go:
package main
import (
"fmt"
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./public")))
log.Fatal(http.ListenAndServe(":80", nil))
}
The public directory is in the same folder with the main.go file. There is an index.html in the public directory.
When I run go run main.go
my website is served properly. But with Docker, I get the 404 page. I suspect it has to do with the placement of the files inside the docker container. Any help is appreciated.
Upvotes: 1
Views: 998
Reputation: 61
As LinPy has mentioned you should copy the public folder from build to alpine
And you have to set the WORKDIR to /go in order for the relative path to work
eg.
FROM golang:alpine AS build
RUN apk --no-cache add gcc g++ make git
WORKDIR /go/src/app
COPY . .
RUN go get ./...
RUN GOOS=linux go build -ldflags="-s -w" -o ./bin/web-app ./main.go
FROM alpine:3.9
RUN apk --no-cache add ca-certificates
WORKDIR /go
COPY --from=build /go/src/app/bin /go/bin
COPY --from=build /go/src/app/public /go/public
EXPOSE 80
ENTRYPOINT /go/bin/web-app --port 80
Upvotes: 1
Reputation: 18588
I think you need to create the folder in your alpine
container:
RUN mkdir -p /go/bin/public
and make sure to have something in that folder.
or you need to copy the folder and files from the first go container.
and edit your dokcerfile :
WORKDIR /go/bin
ENTRYPOINT web-app --port 80
Upvotes: 1