Kang-Kyu Lee

April 15, 2025

docker on Heroku (with your Go project)

Simple as this:

heroku create app-name-here
heroku stack:set container
git push heroku master

Now you will get an error that you need `heroku.yml` file. Make a heroku.yml file in the same directory of `Dockerfile` (which is normally just base directory)

build:
  docker:
    web: Dockerfile

In this case, there is no "run" part in this heroku.yml file (compare with the one in https://devcenter.heroku.com/articles/build-docker-images-heroku-yml#get-started), so do not forget to add CMD at the end of Dockerfile like this:

FROM golang:1.22 AS builder

WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . ./
RUN CGO_ENABLED=0 go build -o main .

FROM alpine:latest
WORKDIR /bin
COPY --from=builder /app/main .
RUN apk --no-cache add ca-certificates

RUN chmod +x /bin/main
CMD ["/bin/main"]

This file has two-stage deployment for Go project. I used this file and it worked. Thank you