Docker container has not set GOPATH correctly

  docker, go

I have a problem when I try to run my app in a Docker container. It is running fine with a simple go run main.go, but whenever I build an image and I run the docker container, I got the error of panic: html/template: pattern matches no files: *.html, so I guess GOPATH is not properly set in the docker container (tho I use this same docker file from other projects and I don’t have any problems). I am a little lost here, since this method I been using already for a while without problems.

I am using gin as a framework for develop.

The docker file is:

FROM golang:alpine as builder

RUN apk update && apk add git && apk add ca-certificates 
# For email certificate
RUN apk add -U --no-cache ca-certificates

COPY . $GOPATH/src/github.com/kiketordera/advanced-performance/
WORKDIR $GOPATH/src/github.com/kiketordera/advanced-performance/

RUN go get -d -v $GOPATH/src/github.com/kiketordera/advanced-performance

# For Cloud Server
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o /go/bin/advanced-performance $GOPATH/src/github.com/kiketordera/advanced-performance

FROM scratch
COPY --from=builder /go/bin/advanced-performance /advanced-performance
COPY --from=builder /go/src/github.com/kiketordera/advanced-performance/media/ /go/src/github.com/kiketordera/advanced-performance/media/

# For email certificate
VOLUME /etc/ssl/certs/ca-certificates.crt:/etc/ssl/certs/ca-certificates.crt
COPY --from=alpine /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/

EXPOSE 8050/tcp

ENV GOPATH /go
ENTRYPOINT ["/advanced-performance"]

Main function is:

package main

import (
    "fmt"
    "net/http"

    "github.com/gin-gonic/gin"
    i18n "github.com/suisrc/gin-i18n"
    "golang.org/x/text/language"
)

func main() {
    // We create the instance for Gin
    r := gin.Default()

    /* Internationalization for showing the right language to match the browser's  default settings
    */
    bundle := i18n.NewBundle(
        language.English,
        "text/en.toml",
        "text/es.toml",
    )

    // Tell Gin to use our middleware. This means that in every single request (GET, POST...), the call to i18n will be executed
    r.Use(i18n.Serve(bundle))

    // Path to the static files. /static is rendered in the HTML and /media is the link to the path to the  images, svg, css.. the static files
    r.StaticFS("/static", http.Dir("media"))

    // Path to the HTML templates. * is a wildcard
    r.LoadHTMLGlob("*.html")

    // Redirects when users introduces a wrong URL
    r.NoRoute(redirect)

    // This get executed when the users gets into our website in the home domain ("/")
    r.GET("/", renderHome)
    r.POST("/", getForm)
    // Listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")

    r.Run()
}

The full project can be found in https://github.com/kiketordera/advanced-performance, is a simple website rendering with i18n and a POST form-handler

Source: Docker Questions

LEAVE A COMMENT