Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 76 additions & 32 deletions src/docker_clojure/core.clj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
(ns docker-clojure.core
(:require
[clojure.java.shell :refer [sh with-sh-dir]]
[clojure.math.combinatorics :as combo]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[docker-clojure.dockerfile :as df]))
[clojure.java.shell :refer [sh with-sh-dir]]
[clojure.math.combinatorics :as combo]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[docker-clojure.dockerfile :as df]))

(s/def ::non-blank-string
(s/and string? #(not (str/blank? %))))
Expand All @@ -31,19 +31,30 @@
(s/def ::maintainers
(s/coll-of ::non-blank-string :distinct true :into #{}))

(def base-image "openjdk")
(def jdk-versions #{8 11 17 18})

(def jdk-versions #{8 11 17 18 19})
(def base-images
"Map of JDK version to base image name with :default as a fallback"
{8 "openjdk"
11 "openjdk"
:default "eclipse-temurin"})

;; The default JDK version to use for tags that don't specify one; usually the latest LTS release
(def default-jdk-version 17)

(def distros
#{:debian/buster :debian-slim/slim-buster :debian/bullseye :debian-slim/slim-bullseye :alpine/alpine})
"Map of base image name to set of distro tags to use, namespaced by Linux
distro type. :default key is a fallback for base images not o/w specified."
{"openjdk" #{:debian/buster :debian-slim/slim-buster :debian/bullseye
:debian-slim/slim-bullseye :alpine/alpine}
:default #{:alpine/alpine :ubuntu/focal}})

;; The default distro to use for tags that don't specify one, keyed by jdk-version.
(def default-distros
{:default :debian-slim/slim-bullseye})
"The default distro to use for tags that don't specify one, keyed by jdk-version.
:default is a fallback for jdk versions not o/w specified."
{8 :debian-slim/slim-bullseye
11 :debian-slim/slim-bullseye
:default :ubuntu/focal})

(def build-tools
{"lein" "2.9.8"
Expand Down Expand Up @@ -72,8 +83,10 @@
(def maintainers
"Paul Lam <[email protected]> & Wes Morgan <[email protected]>")

(defn default-distro [jdk-version]
(get default-distros jdk-version (:default default-distros)))
(defn get-or-default
"Returns the value in map m for key k or else the value for key :default."
[m k]
(get m k (get m :default)))

(defn contains-every-key-value?
"Returns true if the map `haystack` contains every key-value pair in the map
Expand All @@ -85,25 +98,37 @@
(= v (get haystack k)))
needles))

(defn base-image-name [jdk-version distro]
(str base-image ":" jdk-version "-" (name distro)))
(defn base-image-tag [base-image jdk-version distro]
(str base-image ":" jdk-version
(case base-image
"eclipse-temurin" "-jdk-"
"-")
(name distro)))

(defn exclude?
"Returns true if `variant` matches one of `exclusions` elements (meaning
`(contains-every-key-value? variant exclusion)` returns true)."
[exclusions variant]
(some (partial contains-every-key-value? variant) exclusions))

(defn jdk-label [jdk-version base-image]
(if (= default-jdk-version jdk-version)
nil
(str
(case base-image
"eclipse-temurin" "temurin"
base-image)
"-" jdk-version)))

(defn docker-tag
[{:keys [jdk-version distro build-tool build-tool-version]}]
[{:keys [base-image jdk-version distro build-tool
build-tool-version]}]
(if (= ::all build-tool)
"latest"
(let [jdk-label (if (= default-jdk-version jdk-version)
nil
(str base-image "-" jdk-version))
dd (default-distro jdk-version)
(let [jdk (jdk-label jdk-version base-image)
dd (get-or-default default-distros jdk-version)
distro-label (if (= dd distro) nil (when distro (name distro)))]
(str/join "-" (remove nil? [jdk-label build-tool build-tool-version
(str/join "-" (remove nil? [jdk build-tool build-tool-version
distro-label])))))

(s/def ::variant
Expand All @@ -116,9 +141,10 @@
(assoc m k v)
m))

(defn variant-map [[jdk-version distro [build-tool build-tool-version]]]
(defn variant-map [[base-image jdk-version distro [build-tool build-tool-version]]]
(let [base {:jdk-version jdk-version
:base-image (base-image-name jdk-version distro)
:base-image base-image
:base-image-tag (base-image-tag base-image jdk-version distro)
:distro distro
:build-tool build-tool
:build-tool-version build-tool-version
Expand All @@ -132,7 +158,7 @@

(defn generate-dockerfile! [installer-hashes variant]
(let [build-dir (df/build-dir variant)
filename "Dockerfile"]
filename "Dockerfile"]
(println "Generating" (str build-dir "/" filename))
(df/write-file build-dir filename installer-hashes variant)
(assoc variant
Expand Down Expand Up @@ -162,14 +188,32 @@
(def latest-variant
"The latest variant is special because we include all 3 build tools via the
[::all] value on the end."
(list default-jdk-version (default-distro default-jdk-version) [::all]))

(defn image-variants [jdk-versions distros build-tools]
(->> (combo/cartesian-product jdk-versions distros build-tools)
(cons latest-variant)
(map variant-map)
(remove #(= ::s/invalid (s/conform ::variant %)))
set))
(list (:default base-images)
default-jdk-version
(get-or-default default-distros default-jdk-version)
[::all]))

(defn image-variant-combinations [base-images jdk-versions distros build-tools]
(reduce
(fn [variants jdk-version]
(concat
variants
(let [base-image (get-or-default base-images jdk-version)]
(combo/cartesian-product #{(get-or-default base-images jdk-version)}
#{jdk-version}
(get-or-default distros base-image)
build-tools))))
#{} jdk-versions))

(defn image-variants [base-images jdk-versions distros build-tools]
(into #{}
(comp
(map variant-map)
(remove #(= ::s/invalid (s/conform ::variant %))))
(conj
(image-variant-combinations base-images jdk-versions distros
build-tools)
latest-variant)))

(defn build-images [installer-hashes variants]
(println "Building images")
Expand All @@ -182,7 +226,7 @@

(defn valid-variants []
(remove (partial exclude? exclusions)
(image-variants jdk-versions distros build-tools)))
(image-variants base-images jdk-versions distros build-tools)))

(defn -main [& args]
(case (first args)
Expand Down
6 changes: 3 additions & 3 deletions src/docker_clojure/dockerfile.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
[docker-clojure.dockerfile.tools-deps :as tools-deps]
[docker-clojure.dockerfile.shared :refer :all]))

(defn build-dir [{:keys [base-image build-tool]}]
(defn build-dir [{:keys [base-image-tag build-tool]}]
(str/join "/" ["target"
(str/replace base-image ":" "-")
(str/replace base-image-tag ":" "-")
(if (= :docker-clojure.core/all build-tool)
"latest"
build-tool)]))
Expand Down Expand Up @@ -40,7 +40,7 @@

(defn contents [installer-hashes {:keys [build-tool] :as variant}]
(str/join "\n"
(concat [(format "FROM %s" (:base-image variant))
(concat [(format "FROM %s" (:base-image-tag variant))
""]
(case build-tool
:docker-clojure.core/all (all-contents installer-hashes variant)
Expand Down
2 changes: 2 additions & 0 deletions src/docker_clojure/dockerfile/boot.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
{:debian-slim {:build #{"wget"}
:runtime #{}}
:debian {:runtime #{"make"}}
:ubuntu {:build #{"wget"}
:runtime #{}}
:alpine {:build #{"openssl"}
:runtime #{"bash"}}})

Expand Down
2 changes: 2 additions & 0 deletions src/docker_clojure/dockerfile/lein.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
:runtime #{}}
:debian {:build #{"gnupg"}
:runtime #{"make"}}
:ubuntu {:build #{"wget" "gnupg"}
:runtime #{"make"}}
:alpine {:build #{"tar" "gnupg" "openssl" "ca-certificates"}
:runtime #{"bash"}}})

Expand Down
4 changes: 2 additions & 2 deletions src/docker_clojure/dockerfile/shared.clj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
(let [deps (all-deps distro-deps distro)]
(when (seq deps)
(case (-> distro namespace keyword)
(:debian :debian-slim)
(:debian :debian-slim :ubuntu)
["apt-get update"
(str/join " " (concat ["apt-get install -y"] deps))
"rm -rf /var/lib/apt/lists/*"]
Expand All @@ -40,7 +40,7 @@
(let [deps (build-deps distro-deps distro)]
(when (seq deps)
(case (-> distro namespace keyword)
(:debian :debian-slim)
(:debian :debian-slim :ubuntu)
[(str/join " " (concat ["apt-get purge -y --auto-remove"] deps))]

:alpine
Expand Down
2 changes: 2 additions & 0 deletions src/docker_clojure/dockerfile/tools_deps.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
:runtime #{"rlwrap" "make" "git"}}
:debian {:build #{}
:runtime #{"rlwrap" "make"}}
:ubuntu {:build #{"wget"}
:runtime #{"rlwrap" "make" "git"}}
:alpine {:build #{"curl"}
:runtime #{"bash" "make" "git"}}})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:18-slim-bullseye
FROM eclipse-temurin:17-jdk-focal

ENV BOOT_VERSION=2.8.3
ENV BOOT_INSTALL=/usr/local/bin/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:17-slim-bullseye
FROM eclipse-temurin:17-jdk-focal

### INSTALL BOOT ###
ENV BOOT_VERSION=2.8.3
Expand Down Expand Up @@ -36,7 +36,7 @@ WORKDIR /tmp
# Download the whole repo as an archive
RUN set -eux; \
apt-get update && \
apt-get install -y gnupg wget && \
apt-get install -y make gnupg wget && \
rm -rf /var/lib/apt/lists/* && \
mkdir -p $LEIN_INSTALL && \
wget -q https://raw.githubusercontent.com/technomancy/leiningen/$LEIN_VERSION/bin/lein-pkg && \
Expand Down Expand Up @@ -77,7 +77,7 @@ WORKDIR /tmp

RUN \
apt-get update && \
apt-get install -y curl make git rlwrap wget && \
apt-get install -y make git rlwrap wget && \
rm -rf /var/lib/apt/lists/* && \
wget https://download.clojure.org/install/linux-install-$CLOJURE_VERSION.sh && \
sha256sum linux-install-$CLOJURE_VERSION.sh && \
Expand All @@ -86,7 +86,7 @@ chmod +x linux-install-$CLOJURE_VERSION.sh && \
./linux-install-$CLOJURE_VERSION.sh && \
rm linux-install-$CLOJURE_VERSION.sh && \
clojure -e "(clojure-version)" && \
apt-get purge -y --auto-remove curl wget
apt-get purge -y --auto-remove wget

# Docker bug makes rlwrap crash w/o short sleep first
# Bug: https://github.com/moby/moby/issues/28009
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:18-slim-bullseye
FROM eclipse-temurin:17-jdk-focal

ENV LEIN_VERSION=2.9.8
ENV LEIN_INSTALL=/usr/local/bin/
Expand All @@ -8,7 +8,7 @@ WORKDIR /tmp
# Download the whole repo as an archive
RUN set -eux; \
apt-get update && \
apt-get install -y gnupg wget && \
apt-get install -y make gnupg wget && \
rm -rf /var/lib/apt/lists/* && \
mkdir -p $LEIN_INSTALL && \
wget -q https://raw.githubusercontent.com/technomancy/leiningen/$LEIN_VERSION/bin/lein-pkg && \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM openjdk:17-slim-buster
FROM eclipse-temurin:17-jdk-focal

ENV CLOJURE_VERSION=1.11.1.1113

WORKDIR /tmp

RUN \
apt-get update && \
apt-get install -y curl make git rlwrap wget && \
apt-get install -y make git rlwrap wget && \
rm -rf /var/lib/apt/lists/* && \
wget https://download.clojure.org/install/linux-install-$CLOJURE_VERSION.sh && \
sha256sum linux-install-$CLOJURE_VERSION.sh && \
Expand All @@ -15,7 +15,7 @@ chmod +x linux-install-$CLOJURE_VERSION.sh && \
./linux-install-$CLOJURE_VERSION.sh && \
rm linux-install-$CLOJURE_VERSION.sh && \
clojure -e "(clojure-version)" && \
apt-get purge -y --auto-remove curl wget
apt-get purge -y --auto-remove wget

# Docker bug makes rlwrap crash w/o short sleep first
# Bug: https://github.com/moby/moby/issues/28009
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:17-slim-bullseye
FROM eclipse-temurin:18-jdk-focal

ENV BOOT_VERSION=2.8.3
ENV BOOT_INSTALL=/usr/local/bin/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM openjdk:18-slim-buster
FROM eclipse-temurin:18-jdk-focal

ENV LEIN_VERSION=2.9.8
ENV LEIN_INSTALL=/usr/local/bin/
Expand All @@ -8,7 +8,7 @@ WORKDIR /tmp
# Download the whole repo as an archive
RUN set -eux; \
apt-get update && \
apt-get install -y gnupg wget && \
apt-get install -y make gnupg wget && \
rm -rf /var/lib/apt/lists/* && \
mkdir -p $LEIN_INSTALL && \
wget -q https://raw.githubusercontent.com/technomancy/leiningen/$LEIN_VERSION/bin/lein-pkg && \
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
FROM openjdk:19-slim-buster
FROM eclipse-temurin:18-jdk-focal

ENV CLOJURE_VERSION=1.11.1.1113

WORKDIR /tmp

RUN \
apt-get update && \
apt-get install -y curl make git rlwrap wget && \
apt-get install -y make git rlwrap wget && \
rm -rf /var/lib/apt/lists/* && \
wget https://download.clojure.org/install/linux-install-$CLOJURE_VERSION.sh && \
sha256sum linux-install-$CLOJURE_VERSION.sh && \
Expand All @@ -15,7 +15,7 @@ chmod +x linux-install-$CLOJURE_VERSION.sh && \
./linux-install-$CLOJURE_VERSION.sh && \
rm linux-install-$CLOJURE_VERSION.sh && \
clojure -e "(clojure-version)" && \
apt-get purge -y --auto-remove curl wget
apt-get purge -y --auto-remove wget

# Docker bug makes rlwrap crash w/o short sleep first
# Bug: https://github.com/moby/moby/issues/28009
Expand Down
Loading