Skip to content

Commit e4cf9c0

Browse files
committed
Add Docker support with multi-stage build and startup script
1 parent c7f5e74 commit e4cf9c0

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

.dockerignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Exclude unnecessary files from Docker build context
2+
3+
# VCS and IDE
4+
.git/
5+
.gitignore
6+
.idea/
7+
*.iml
8+
.vscode/
9+
10+
# OS junk
11+
.DS_Store
12+
Thumbs.db
13+
14+
# Build outputs
15+
/target/
16+
/build/
17+
/out/
18+
/dist/
19+
/dist-newstyle/
20+
/.stack-work/
21+
22+
# Java/Maven/Gradle
23+
.mvn/
24+
.gradle/
25+
.settings/
26+
.classpath
27+
.project
28+
29+
# Logs and temp
30+
*.log
31+
*.tmp
32+
/tmp/
33+
34+
# Coverage and test caches
35+
/coverage/
36+
/.pytest_cache/
37+
38+
# Avoid copying CI configs if not needed in image
39+
.github/
40+
qodana.yaml

Dockerfile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Multi-stage Dockerfile for the polyglot scheduling stack
2+
# Stage 1: builder and test runner
3+
FROM ubuntu:22.04 AS builder
4+
5+
ENV DEBIAN_FRONTEND=noninteractive
6+
RUN apt-get update && apt-get install -y --no-install-recommends \
7+
ca-certificates curl git \
8+
ghc cabal-install \
9+
swi-prolog \
10+
build-essential \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
WORKDIR /app
14+
COPY . /app
15+
16+
# Install Haskell dependencies required by GeneticSchedule.hs
17+
RUN cabal update && cabal install aeson --lib
18+
19+
# Build the Haskell scheduler as a native binary for faster startup
20+
RUN ghc -O2 -threaded haskell/GeneticSchedule.hs -o /app/genetic-schedule
21+
22+
# Run smoke tests during the build to ensure the image is healthy
23+
# If tests are not desired, pass --target runtime when building.
24+
RUN bash tests/run_all.sh || (echo "Tests failed during Docker build" && exit 1)
25+
26+
# Stage 2: runtime image with just what we need
27+
FROM ubuntu:22.04 AS runtime
28+
29+
ENV DEBIAN_FRONTEND=noninteractive
30+
# libgmp is required by GHC-built binaries; include swipl runtime as optional tool and a minimal JRE
31+
RUN apt-get update && apt-get install -y --no-install-recommends \
32+
libgmp10 \
33+
swi-prolog-nox \
34+
ca-certificates \
35+
openjdk-17-jre-headless \
36+
&& rm -rf /var/lib/apt/lists/*
37+
38+
WORKDIR /app
39+
# Copy compiled binary and necessary assets
40+
COPY --from=builder /app/genetic-schedule /app/genetic-schedule
41+
COPY --from=builder /app/prolog /app/prolog
42+
COPY --from=builder /app/sample-request.json /app/sample-request.json
43+
44+
# Copy startup script that decides whether to run Java or the Haskell scheduler
45+
COPY start.sh /app/start.sh
46+
RUN chmod +x /app/start.sh
47+
48+
# Document ENV that Java service would use if run in this container later
49+
ENV SCHEDULER_HS_CMD=/app/genetic-schedule \
50+
SCHEDULER_HS_SCRIPT=""
51+
52+
# Expose Spring Boot default port (used when running the Java app)
53+
EXPOSE 8080
54+
55+
# Default entrypoint delegates to the start script
56+
ENTRYPOINT ["/app/start.sh"]

start.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# If a Spring Boot fat JAR is present at /app/app.jar, start the Java API.
5+
# Otherwise, run the Haskell scheduler (default behavior of this image).
6+
7+
if [[ -f "/app/app.jar" ]]; then
8+
echo "[start.sh] Detected /app/app.jar. Launching Java Spring Boot API..."
9+
export SCHEDULER_HS_CMD="/app/genetic-schedule"
10+
export SCHEDULER_HS_SCRIPT=""
11+
exec java -jar /app/app.jar
12+
else
13+
echo "[start.sh] No /app/app.jar found. Launching Haskell scheduler binary..."
14+
echo "[start.sh] Tip: To run the Java API, copy or mount your Spring Boot fat JAR to /app/app.jar"
15+
exec /app/genetic-schedule
16+
fi

0 commit comments

Comments
 (0)