You go to the website, you put ocaml
inside the search bar and you find ocaml/opam. If you go to the tag list page, you will see all the available versions and the OS list on which they are showcased. Let's choose this one:
Now, the goal is to create a folder on your host system (let's call it $ROOT
), containing the code you will write to try things and use that folder inside docker to compile or run this code. Let's make the folder, and once it's done, open a terminal and go to this folder using cd
. Now, run this command:
# On windows [powershell]
docker run -v"$PWD:/repo" -it ocaml/opam:debian-10-ocaml-4.06
# On linux [bash]
docker run -v"$PWD:/repo" -u"$(id -u):$(id -g)" -it ocaml/opam:debian-10-ocaml-4.06
# -u"$(id -u):$(id -g)" is to tell docker to launch the image as a user with
# the same user id and group id than your host user. This ensure all the files
# you create will be owned by your host user (and not root if you do not
# put the argument).
This command will start the image and mount our host directory $PWD
(which is supposed to be $ROOT
if you did well) to the container internal directory /repo
(you could have chosen any name, but I recommend to not choose one among the linux standard directories like /usr
, /lib
, ..., otherwise you may encounter some troubles).
Then, let's install dune
package, create a sample project and build it:
While still running the container, you can edit the files on your host system and build the project inside . Let's put this in $ROOT/helloworld/bin/main.ml
:
let square x = x * x
let () = Printf.printf "square 4: %d\n" (square 4)
And now let's build and run again from inside of docker: