Good way of installing specific Cabal and GHC inside a Docker container?

This Dockerfile builds without issue for me. The GHC installation step takes some time; perhaps it works for you as well if you give it more time.

$ docker buildx build -f Dockerfile .
[+] Building 124.9s (8/8) FINISHED                                              docker:default
 => [internal] load build definition from Dockerfile                                      0.0s
 => => transferring dockerfile: 644B                                                      0.0s
 => WARN: UndefinedVar: Usage of undefined variable '$HOME' (line 15)                     0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                          0.0s
 => [internal] load .dockerignore                                                         0.0s
 => => transferring context: 2B                                                           0.0s
 => [1/4] FROM docker.io/library/alpine:latest                                            0.0s
 => [2/4] RUN apk update && apk add --no-cache     bash curl git make m4 gcc g++ ocaml   24.5s
 => [3/4] RUN curl -L https://downloads.haskell.org/~ghcup/x86_64-linux-ghcup -o /usr/bi  2.5s
 => [4/4] RUN ghcup install cabal 3.10.2.1 &&     ghcup install ghc 9.4.8 &&     ghcup   93.7s
 => exporting to image                                                                    4.2s
 => => exporting layers                                                                   4.2s
 => => writing image sha256:5938b1bfe833439201af140accc3d902b4cd3d828c4dea4cfebe7722da1c  0.0s

 1 warning found (use --debug to expand):
 - UndefinedVar: Usage of undefined variable '$HOME' (line 15)

If your goal is to distribute the specific versions of GHC and Cabal, then this should be fine. Given that you are using Alpine, you may want to check out benz0li/ghc-musl for inspiration. You may also want to check out the Dockerfile in one of my projects, which does some things to reduce the size of the image.

If your goal is to distribute your program, then you may want to configure a multi-stage build and copy only your program and its dependencies into the final image.