hatamo commited on
Commit
fcf0023
·
1 Parent(s): 38bc8bf

AI docker changes

Browse files
Files changed (4) hide show
  1. Dockerfile +9 -13
  2. README-spaces.md +20 -0
  3. bin/start.sh +15 -0
  4. config/puma.rb +5 -2
Dockerfile CHANGED
@@ -54,16 +54,11 @@ RUN chmod +x bin/*
54
  # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
55
  RUN bundle exec bootsnap precompile -j 1 app/ lib/
56
 
57
- # Precompiling assets for production without requiring secret RAILS_MASTER_KEY
58
- # Użycie BuildKit secret dla dummy secret_key_base
59
-
60
- ENV ASSETS_PRECOMPILE=true
61
-
62
- RUN --mount=type=secret,id=SECRET_KEY_BASE,mode=0444,required=true \
63
- --mount=type=secret,id=RAILS_MASTER_KEY,mode=0444,required=true \
64
- SECRET_KEY_BASE=$(cat /run/secrets/SECRET_KEY_BASE) \
65
- RAILS_MASTER_KEY=$(cat /run/secrets/RAILS_MASTER_KEY) \
66
- ./bin/rails assets:precompile
67
 
68
  # Final stage for app image
69
  FROM base
@@ -80,6 +75,7 @@ COPY --chown=rails:rails --from=build /rails /rails
80
  # Entrypoint prepares the database.
81
  ENTRYPOINT ["/rails/bin/docker-entrypoint"]
82
 
83
- # Start server via Thruster by default, this can be overwritten at runtime
84
- EXPOSE 3000
85
- CMD ["./bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
 
 
54
  # -j 1 disable parallel compilation to avoid a QEMU bug: https://github.com/rails/bootsnap/issues/495
55
  RUN bundle exec bootsnap precompile -j 1 app/ lib/
56
 
57
+ # NOTE: BuildKit secret mounts are not available in all build environments
58
+ # (for example, Hugging Face Spaces). To keep builds portable we skip
59
+ # precompiling assets at build-time here and instead perform a best-effort
60
+ # assets precompile at container start. This makes the image buildable
61
+ # on Spaces without requiring BuildKit secret support.
 
 
 
 
 
62
 
63
  # Final stage for app image
64
  FROM base
 
75
  # Entrypoint prepares the database.
76
  ENTRYPOINT ["/rails/bin/docker-entrypoint"]
77
 
78
+ # Start server via Thruster by default; listen on port from $PORT (default 8080)
79
+ # Use shell CMD so the runtime can run database prep and a best-effort assets precompile
80
+ EXPOSE 8080
81
+ CMD ["./bin/start.sh"]
README-spaces.md ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Hugging Face Spaces Docker notes
2
+
3
+ - Required port: Spaces will set `PORT`; the container should listen on `$PORT`. This project defaults to 8080.
4
+ - Provide `RAILS_MASTER_KEY` via the Spaces secrets or environment variables if you use encrypted credentials.
5
+ - For production DB use, set `DATABASE_URL` to a hosted Postgres/MySQL instance; otherwise the image uses SQLite (not persistent across container restarts).
6
+
7
+ Build & run (local example):
8
+
9
+ ```bash
10
+ docker build -t antique_analyzer .
11
+ # run on port 8080 and pass a master key if needed
12
+ docker run -e RAILS_MASTER_KEY="$RAILS_MASTER_KEY" -e PORT=8080 -p 8080:8080 antique_analyzer
13
+ ```
14
+
15
+ Notes for Spaces:
16
+ - Push this repository to a Space that uses a `Dockerfile`.
17
+ - Add any required env vars (`RAILS_MASTER_KEY`, `DATABASE_URL`) in the Space settings.
18
+ - The container will attempt to run `rails db:prepare` and `rails assets:precompile` on startup; ensure migrations and assets are compatible with that flow.
19
+
20
+ If you want a faster startup, consider precompiling assets during build in CI and providing them in the image (requires secure handling of `RAILS_MASTER_KEY`).
bin/start.sh ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash -e
2
+
3
+ # Start script for container environments (Hugging Face Spaces compatible)
4
+ # - prepare or migrate DB
5
+ # - precompile assets (best-effort)
6
+ # - start Rails server on $PORT (default 8080)
7
+
8
+ echo "[container] Running db:prepare..."
9
+ bundle exec rails db:prepare
10
+
11
+ echo "[container] Precompiling assets (best-effort)..."
12
+ bundle exec rails assets:precompile || true
13
+
14
+ echo "[container] Starting Rails server on 0.0.0.0:${PORT:-8080}"
15
+ exec bundle exec rails server -b 0.0.0.0 -p "${PORT:-8080}"
config/puma.rb CHANGED
@@ -28,8 +28,11 @@
28
  threads_count = ENV.fetch("RAILS_MAX_THREADS", 3)
29
  threads threads_count, threads_count
30
 
31
- # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
32
- port ENV.fetch("PORT", 3000)
 
 
 
33
 
34
  # Allow puma to be restarted by `bin/rails restart` command.
35
  plugin :tmp_restart
 
28
  threads_count = ENV.fetch("RAILS_MAX_THREADS", 3)
29
  threads threads_count, threads_count
30
 
31
+ # Specifies the `port` that Puma will listen on to receive requests; default is 8080 to match container environments.
32
+ port ENV.fetch("PORT", 8080)
33
+
34
+ # Bind explicitly to all interfaces in container environments.
35
+ bind "tcp://0.0.0.0:#{ENV.fetch('PORT', 8080)}"
36
 
37
  # Allow puma to be restarted by `bin/rails restart` command.
38
  plugin :tmp_restart