<p>I have the following directory structure:</p>
<pre><code>templates/
  a.tmpl
services/
  service.go
main.go
</code></pre>
<p>Now inside the <code>service.go</code> file i am calling the below function:</p>
<pre><code>dir, err := filepath.Abs(filepath.Dir(&quot;./templates/&quot;))
    if err != nil {
        return nil, err
    }


    baseFile := filepath.Join(dir, &quot;a.tmpl&quot;)

tmpl, err := template.New(&quot;base&quot;).ParseFiles(baseFile)
</code></pre>
<p>now the above function is parsing my <code>a.tmpl</code> file as expected.</p>
<p>but once this service is up on docker and kubernetes, i am no longer able to open the file since the file does not exists</p>
<p>why is that?</p>
<p>UPDATE:</p>
<pre><code>FROM golang:1.16-buster AS builder
# Copy the code from the host and compile it
WORKDIR $GOPATH/src/github.com/me/report
COPY . ./
# pack templates to binary
RUN CGO_ENABLED=0 GOOS=linux go build -mod vendor -ldflags &quot;-X github.com/me/report/cmd.version=$(cat .VERSION)&quot; -o /app .

FROM xyz.amazonaws.com/common/platform/base:latest as prod
COPY --from=builder /app ./
ADD ./migrations /migrations
ENTRYPOINT [&quot;/app&quot;]
</code></pre>

<p>When you <a href="https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies" rel="nofollow noreferrer">build</a> your binary, go only includes the necessary go files to have your program work. It does not know that your <code>templates</code> directory is necessary to the running of the program.</p>
<p>There is several solutions to your problem :</p>
<ul>
<li>Create an environment variable pointing to were the templates are and use it on runtime.</li>
<li>Embed the <code>templates</code> directory into your binary using the <a href="https://golang.google.cn/pkg/embed/" rel="nofollow noreferrer">embed package</a> so that you can access the files at runtime</li>
</ul>
