Dockerfiles don't make your application listen on a specific port number.
The EXPOSE
directive in Dockerfile is purely a documentation and doesn't do anything functional.
You have 2 options for a Go app:
-
Just refactor your code to read the
PORT
env variable:os.Getenv("PORT")
and use it on the HTTP server address you’re starting:port := os.Getenv("PORT") http.ListenAndServe(":"+port)
-
Create a
-port
flag and read it during the entrypoint of your app in the Dockerfile:e.g. if you can make
go run main.go -port=8080
work, change your dockerfile to:exec go run main.go -port=$PORT
These will get you what you want.
Ideally you should not use go run
inside a container. Just do:
RUN go build -o /bin/my-app ./my/pkg
ENTRYPOINT /bin/my-app
to compile a Go program and use it directly. Otherwise, every time Cloud Run starts your container, you would be re-compiling it from scratch, which is not fast, this will increase your cold start times.
Aside from these you seem to have a lot of inconsistencies in your dockerfile. You set a lot of Go env vars like GOOS GOARCH but you don't actually go build
your app (go run
is an on-the-fly compilation and doesn't take the linker flags in GOFLAGS into account I believe). Look at sample Go dockerfiles to have a better idea on how to write idiomatic Go dockerfiles.