get docker uberjar working

This commit is contained in:
Travis Shears 2025-06-07 16:05:34 +02:00
parent 4acc4e2201
commit c28c398a14
9 changed files with 91 additions and 27 deletions

View file

@ -13,21 +13,17 @@
# it uses alpine for a small image
FROM clojure:temurin-21-tools-deps-alpine AS jre-build
ENV TAILWIND_VERSION=v3.2.4
# Install the missing packages and applications in a single layer
RUN apk add curl rlwrap && curl -L -o /usr/local/bin/tailwindcss \
https://github.com/tailwindlabs/tailwindcss/releases/download/$TAILWIND_VERSION/tailwindcss-linux-x64 \
&& chmod +x /usr/local/bin/tailwindcss
RUN apk add curl rlwrap
WORKDIR /app
COPY deps.edn ./
RUN clj -P
COPY build.clj config.edn ./
COPY src ./src
COPY dev ./dev
COPY resources ./resources
COPY deps.edn .
# construct the application jar
RUN clj -M:dev uberjar && cp target/jar/app.jar . && rm -r target
RUN clj -T:build uber && cp target/snippets-standalone.jar ./app.jar && rm -r target
# This stage (see multi-stage builds) is a bare Java container
# copy over the uberjar from the builder image and run the application
@ -36,11 +32,12 @@ WORKDIR /app
# Take the uberjar from the base image and put it in the final image
COPY --from=jre-build /app/app.jar /app/app.jar
COPY --from=jre-build /app/config.edn /app/
EXPOSE 8080
# By default, run in PROD profile
ENV BIFF_PROFILE=prod
# ENV BIFF_PROFILE=prod
ENV HOST=0.0.0.0
ENV PORT=8080
CMD ["/opt/java/openjdk/bin/java", "-XX:-OmitStackTraceInFastThrow", "-XX:+CrashOnOutOfMemoryError", "-jar", "app.jar"]

View file

@ -5,5 +5,19 @@
"ignore": [
"node_modules",
".git"
]
],
"size": 0.0006971359252929688,
"filesCount": 5,
"proxy": {
"bypassProxy": "",
"enabled": false,
"auth": {
"enabled": false,
"username": "",
"password": ""
},
"port": null,
"hostname": "",
"protocol": "http"
}
}

View file

@ -0,0 +1,3 @@
vars:secret [
host
]

View file

@ -5,14 +5,14 @@ meta {
}
get {
url: {{host}}/api/snippet?limit=10&skip=10
url: 192.168.1.157:29406/api/snippet?limit=4&skip=0
body: none
auth: none
}
params:query {
limit: 10
skip: 10
limit: 4
skip: 0
}
body:json {

25
build.clj Normal file
View file

@ -0,0 +1,25 @@
(ns build
(:require [clojure.tools.build.api :as b]))
(def lib 'snippets)
(def class-dir "target/classes")
(def uber-file (format "target/%s-standalone.jar" (name lib)))
;; delay to defer side effects (artifact downloads)
(def basis (delay (b/create-basis {:project "deps.edn"})))
(defn clean [_]
(b/delete {:path "target"}))
(defn uber [_]
(clean nil)
(b/copy-dir {:src-dirs ["src" "resources"]
:target-dir class-dir})
(b/compile-clj {:basis @basis
:ns-compile '[snippets.main]
:class-dir class-dir})
(b/uber {:class-dir class-dir
:uber-file uber-file
:basis @basis
:main 'snippets.main}))

View file

@ -10,6 +10,9 @@
org.postgresql/postgresql {:mvn/version "42.7.6"}
frontmatter/frontmatter {:mvn/version "0.0.1"}
;; environment variables
environ/environ {:mvn/version "1.2.0"}
;; schema validation
metosin/malli {:mvn/version "0.18.0"}
@ -22,7 +25,11 @@
;; convenient package of "default" middleware:
;; ring/ring-defaults {:mvn/version "0.5.0"}
org.clojure/clojure {:mvn/version "1.12.1"}}}
org.clojure/clojure {:mvn/version "1.12.1"}}
:aliases
{;; Run with clj -T:build function-in-build
:build {:deps {io.github.clojure/tools.build {:git/tag "v0.10.9" :git/sha "e405aac"}}
:ns-default build}}}
;; :aliases
;; {:dev {:extra-deps {com.biffweb/tasks {:git/url "https://github.com/jacobobryant/biff", :git/sha "1570ccc", :git/tag "v1.8.29", :deps/root "libs/tasks"}}

View file

@ -3,6 +3,7 @@
[ring.adapter.jetty :as jetty]
[clojure.pprint :as pprint]
[snippets.db :as db]
[snippets.config :as config]
[muuntaja.middleware :as mm]
[ring.middleware.params]
[reitit.ring :as rr]))
@ -43,9 +44,15 @@
;; (def app
;; ;; use #' prefix for REPL-friendly code -- see note below
;; (wrap-defaults #'app-routes site-defaults))
;;
(defn run-server []
(let [jetty-config (:jetty (config/get-config))
port (:port jetty-config)
host (:host jetty-config)]
(jetty/run-jetty #'app {:port port :host host})))
(defn -main []
(jetty/run-jetty #'app {:port 3000}))
(run-server))
(comment
;; evaluate this def form to start the webapp via the REPL:

View file

@ -1,13 +1,18 @@
(ns snippets.config)
(ns snippets.config
(:require [environ.core :refer [env]]))
;; (require '[environ.core :refer [env]])
(defn get-config [] (read-string (slurp "config.edn")))
(defn- static-config [] (read-string (slurp "config.edn")))
;; (def config
;; (merge
;; (read-string (slurp "config.edn"))
;; {:db {:host (env :db-host)
;; :port (Integer. (env :db-port))
;; :user (env :db-user)
;; :password (env :db-password)
;; :dbname (env :db-name)}}))
(defn assoc-in-not-nil [config keys value]
(if (nil? value)
config
(assoc-in config keys value)))
(defn safe-parse-int [value]
(if (nil? value) nil (Integer/parseInt value)))
(defn get-config []
(->
(static-config)
(assoc-in-not-nil [:jetty :host] (env :host))
(assoc-in-not-nil [:jetty :port] (safe-parse-int (env :port)))))

6
src/snippets/main.clj Normal file
View file

@ -0,0 +1,6 @@
(ns snippets.main
(:require [snippets.api :as api])
(:gen-class))
(defn -main []
(api/run-server))