OCaml Dockerfile build from scratch

I’m trying to package Ocaml programs for future container deployments. Upon looking at docker hub, there is no official Ocaml base images other than ocaml/opam, which doesn’t really suit my use case.

I created two versions of ocaml base images, with …


This content originally appeared on DEV Community and was authored by James

I'm trying to package Ocaml programs for future container deployments. Upon looking at docker hub, there is no official Ocaml base images other than ocaml/opam, which doesn't really suit my use case.

I created two versions of ocaml base images, with the goal of optimized image sizes. One based on Ubuntu and one base on Alpine Linux. (may add more base os later...)

Ubuntu version:

FROM ubuntu:20.04 AS base

FROM base AS builder

# --disable-sandboxing is needed due to bwrap: No permissions to creating new namespace error
RUN apt-get update \
    && apt-get upgrade \
    && apt-get install -y opam \
    && opam init --bare -a -y --disable-sandboxing \
    && opam update

RUN opam switch create default ocaml-base-compiler.5.2.0

RUN opam install -y dune

WORKDIR /app

COPY dune-project dune hello.ml ./

# eval $(opam config env) applies dune to PATH but it only persists in a single RUN layer
RUN eval $(opam config env) \
    && dune build hello.exe

FROM base AS runner

WORKDIR /app

COPY --from=builder /app/_build/default/hello.exe /app

CMD [ "/app/hello.exe" ]

Alpine version:

FROM alpine:3.20 AS base

FROM base AS builder

RUN apk update && \
    apk upgrade && \
    apk add build-base opam

RUN opam init --bare -a -y --disable-sandboxing \
    && opam update

RUN opam switch create default ocaml-base-compiler.5.2.0

RUN opam install -y dune

WORKDIR /app

COPY dune-project dune hello.ml ./

# eval $(opam config env) applies dune to PATH but it only persists in a single RUN layer
RUN eval $(opam config env) \
    && dune build hello.exe

FROM base AS runner

WORKDIR /app

COPY --from=builder /app/_build/default/hello.exe /app

CMD [ "/app/hello.exe" ]

Host environment:
cpu: apple m2
macos: 14.6.1

Image size:
alpine: 10.5 mb
ubuntu: 67.7 mb

Hope these would help anyone who wants to package Ocaml programs for container deployment.


This content originally appeared on DEV Community and was authored by James


Print Share Comment Cite Upload Translate Updates
APA

James | Sciencx (2024-09-15T20:01:30+00:00) OCaml Dockerfile build from scratch. Retrieved from https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/

MLA
" » OCaml Dockerfile build from scratch." James | Sciencx - Sunday September 15, 2024, https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/
HARVARD
James | Sciencx Sunday September 15, 2024 » OCaml Dockerfile build from scratch., viewed ,<https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/>
VANCOUVER
James | Sciencx - » OCaml Dockerfile build from scratch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/
CHICAGO
" » OCaml Dockerfile build from scratch." James | Sciencx - Accessed . https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/
IEEE
" » OCaml Dockerfile build from scratch." James | Sciencx [Online]. Available: https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/. [Accessed: ]
rf:citation
» OCaml Dockerfile build from scratch | James | Sciencx | https://www.scien.cx/2024/09/15/ocaml-dockerfile-build-from-scratch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.