Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go app Dockerfile always downloads the modules on restart

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?

like image 919
Mike Avatar asked Oct 22 '25 14:10

Mike


1 Answers

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).

like image 56
mehrdadep Avatar answered Oct 25 '25 09:10

mehrdadep



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!