I've written an application using Go language and created a Dockerfile for it as follows. The problem is every time I restart the docker container, all the modules are being downloaded again.
FROM golang:1.15-alpine
WORKDIR /my-app
COPY . .
RUN go run main.go
How can I fix this?
Docker will cache each layer and use them if they are identical with the previous run. The problem with your Dockerfile is that you're not adding a layer for downloading modules and you're not building your application (You are running it every time). I assume that you're using go modules for your dependencies, so here is the fix for your Dockerfile
FROM golang:1.15-alpine
WORKDIR /my-app
COPY go.mod .
COPY go.sum .
RUN go mod download
COPY . .
RUN go build -i -o ./build/my-app
ENTRYPOINT ["./build/my-app"]
With this Dockerfile the download process is going to cache and reused on each restart (unless you change the dependencies).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With