From b6258eee5ad3523667625d63b0a803985a2f7ce2 Mon Sep 17 00:00:00 2001 From: mccraig mccraig of the clan mccraig Date: Fri, 22 Aug 2014 00:43:34 +0100 Subject: [PATCH] Specify command and extra files in project.clj Allows config to be given in project.clj with the form : :uberimage {:cmd ["/bin/dash" "/myrunscript" "param1" "param2"] :files {"myrunscript" "docker/myrunscript"}} - The :cmd value maps directly to a Dockerfile CMD statement - The :files value is a map of additional files to be copied into the docker image. Keys are docker image target paths and values are lein project source paths This permits more flexibility in starting the containerized program, allowing things such as configuring extra environment with a script. --- src/leiningen/uberimage.clj | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/leiningen/uberimage.clj b/src/leiningen/uberimage.clj index fff3de8..a7b8c07 100644 --- a/src/leiningen/uberimage.clj +++ b/src/leiningen/uberimage.clj @@ -15,22 +15,32 @@ (defn dockerfile "Return a dockerfile string" - [{:keys [base-image]}] - (format "FROM %s -ADD uberjar.jar uberjar.jar -CMD [\"/usr/bin/java\", \"-jar\", \"/uberjar.jar\"]" - base-image)) + [{:keys [cmd files base-image]}] + (let [cmd (or cmd ["/usr/bin/java" "-jar" "/uberjar.jar"]) + cmd (if (sequential? cmd) cmd [cmd]) + cmd-str (->> (for [s cmd] (str "\"" s "\"")) + (string/join ","))] + (->> (concat [(str "FROM " base-image) + "ADD uberjar.jar uberjar.jar" + (str "CMD [" cmd-str "]")] + (for [[tar-path local-path] files] + (str "ADD " tar-path " " tar-path))) + (string/join "\n")))) (defn buildtar "Return an InputStream that will deliver a tar archive with a Dockerfile and the uberjar." - [tar-output-stream piped-output-stream standalone-filename options] + [tar-output-stream piped-output-stream standalone-filename + {:keys [files] :as options}] (with-open [piped-output-stream piped-output-stream tar-output-stream tar-output-stream] (tar-entry-from-string tar-output-stream "Dockerfile" (dockerfile options)) (tar-entry-from-file - tar-output-stream "uberjar.jar" (file standalone-filename)))) + tar-output-stream "uberjar.jar" (file standalone-filename)) + (doseq [[tar-path local-path] files] + (tar-entry-from-file + tar-output-stream tar-path (file local-path))))) (def info-timbre-config "A basic timbre configuration for use with info level logging." @@ -69,7 +79,8 @@ CMD [\"/usr/bin/java\", \"-jar\", \"/uberjar.jar\"]" (defn ^{:doc (help)} uberimage [project & args] (let [{:keys [options arguments summary errors]} (parse-opts args cli-options) - {:keys [base-image endpoint]} options] + {:keys [base-image endpoint]} options + options (merge (:uberimage project) options)] (when errors (throw (ex-info (str "Invalid arguments: " (string/join " " errors))