Skip to content

Instantly share code, notes, and snippets.

@dbuenzli
Created July 12, 2016 13:24
Show Gist options
  • Select an option

  • Save dbuenzli/bc97fa81c3f744e453944d3d52f861b3 to your computer and use it in GitHub Desktop.

Select an option

Save dbuenzli/bc97fa81c3f744e453944d3d52f861b3 to your computer and use it in GitHub Desktop.
Docker cross compilation
FROM ocaml/opam:alpine-armhf_ocaml-4.02.3
ENV OPAMYES 1
ENV OPAM_PKGS ocamlfind ocamlbuild cmdliner bos topkg topkg-care
RUN opam update -u && \
opam config exec -- opam-depext $OPAM_PKGS && \
opam install $OPAM_PKGS
WORKDIR /build
CMD ["bash"]
#!/usr/bin/env ocaml
#use "topfind"
#require "bos.setup"
open Bos_setup
let docker cmd = Cmd.(v "docker" % cmd)
let image_exists i =
OS.Cmd.(run_out Cmd.(docker "images" % "-q" % i) |> to_string)
>>| fun id -> id <> ""
let delete_image i = image_exists i >>= function
| false -> Ok ()
| true -> OS.Cmd.(run_out Cmd.(docker "rmi" % i) |> to_null)
let create_image i build_dir =
delete_image i >>= fun () ->
OS.Cmd.run Cmd.(docker "build" % "--force-rm" % "-t" % i % p build_dir)
let container_exists name =
let name = strf "name=%s" name in
OS.Cmd.(run_out Cmd.(docker "ps" % "-a" % "-q" % "-f" % name) |> to_string)
>>| fun id -> id <> ""
let delete_container name = container_exists name >>= function
| false -> Ok ()
| true -> OS.Cmd.run Cmd.(docker "rm" % name)
let create_container name =
OS.Dir.current () >>= fun cwd ->
Ok (strf "%a:/build" Fpath.pp cwd) >>= fun volume ->
OS.Cmd.run Cmd.(docker "run" % "--detach" % "-it" % "--volume" % volume %
"--name" % name % name)
let start_container name = container_exists name >>= function
| false -> create_container name
| true -> OS.Cmd.run Cmd.(docker "start" % name)
let attach_container name =
start_container name
>>= fun () -> OS.Cmd.run Cmd.(docker "attach" % name)
let clean_docker name =
delete_container name
>>= fun () -> delete_image name
let cross_exec name =
Cmd.(docker "exec" % "-it" % name % "opam" % "config" % "exec" % "--")
let xbdir = "_xbuild"
let cross_build name =
OS.Cmd.run Cmd.(cross_exec name % "topkg" % "build" % "--build-dir" % xbdir)
let cross_clean name =
OS.Cmd.run Cmd.(cross_exec name % "topkg" % "clean" % "--build-dir" % xbdir)
let main () =
let doc = "Docker topkg cross-compilation" in
let name =
let doc = "Image and container name" in
OS.Arg.(opt ["c"] string ~absent:"villa" ~doc ~docv:"NAME")
in
let img_build_dir =
let doc = "Image build directory" in
let docv = "DIR" in
OS.Arg.(opt ["img-build-dir"] path ~absent:(Fpath.v "cross") ~doc ~docv)
in
begin match OS.Arg.(parse ~pos:string () ~doc) with
| ["image"] -> create_image name img_build_dir
| ["start"] -> start_container name
| ["attach"] -> attach_container name
| ["clean-docker"] -> clean_docker name
| ["build"] -> cross_build name
| ["clean"] -> cross_clean name
| args ->
R.error_msgf "%s: unknown positional args (%a)"
OS.Arg.exec Fmt.Dump.(list String.dump) args
end
|> R.map (fun () -> 0)
|> Logs.on_error_msg ~use:(fun () -> 1)
let () = exit (main ())
(*---------------------------------------------------------------------------
Copyright (c) 2016 Daniel C. Bünzli
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment