Skip to content

Instantly share code, notes, and snippets.

@DerTev
Last active June 15, 2023 19:31
Show Gist options
  • Save DerTev/0c74e145e9badcebcee385638bc9fe4e to your computer and use it in GitHub Desktop.
Save DerTev/0c74e145e9badcebcee385638bc9fe4e to your computer and use it in GitHub Desktop.
lein-docker

lein-docker

Build docker-image containing leiningen with amazon coretto.

Usage

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!")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment