Build docker-image containing leiningen with amazon coretto.
Coming soon
(require '[babashka.cli :as cli] | |
'[babashka.fs :as fs] | |
'[babashka.process :as shell]) | |
(defn render-dockerfile | |
"Returns the rendered dockerfile based on given arguments. | |
Thanks, @amejonah1200, for the template! | |
`java-version`: Version for the amazon-coretto-Docker-image. | |
`lein-url`: URL to download the leiningen-script." | |
[^String java-version ^String lein-url] | |
(str "FROM amazoncorretto:" java-version "\n" | |
"RUN apk -U upgrade\n" | |
"RUN apk add --no-cache curl bash\n" | |
"RUN curl -LO " lein-url "\n" | |
"RUN chmod +x lein\n" | |
"RUN mv lein /bin\n" | |
"RUN lein self-install\n")) | |
(defn parse-args | |
"Parse args from map containing the command line arguments, possible to get from [[*command-line-args*]]." | |
[args] | |
(cli/parse-opts args {:alias {:jv :java-version | |
:lu :lein-url | |
:t :docker-tag | |
:d :temp-dir} | |
:coerce {:java-version :string | |
:lein-url :string | |
:docker-tag :string | |
:temp-dir :string} | |
:exec-args {:java-version "17-alpine" | |
:lein-url "https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein" | |
:docker-tag "leiningen-coretto" | |
:temp-dir "/tmp/lein-docker/"}})) | |
(defn write-dockerfile! | |
"Writes dockerfile from [[render-dockerfile]]. | |
`parsed-args`: Map with the return value of [[parse-args]]." | |
[parsed-args] | |
(fs/create-dirs (:temp-dir parsed-args)) | |
(->> (render-dockerfile (:java-version parsed-args) | |
(:lein-url parsed-args)) | |
(spit (str (:temp-dir parsed-args) | |
"Dockerfile")))) | |
(let [start-time (.currentTimeMillis System) | |
parsed-args (-> *command-line-args* | |
parse-args)] | |
(println "Write Dockerfile...") | |
(write-dockerfile! parsed-args) | |
(println "Build Docker-Image...") | |
(shell/shell {:dir (:temp-dir parsed-args)} | |
"docker" "build" (str "-t" (:docker-tag parsed-args)) ".") | |
(println (str "Finished in " (- (.currentTimeMillis System) start-time) "ms!"))) |