Skip to content

Commit a5adc59

Browse files
authored
Merge pull request #1432 from indentlabs/railway-test-deployment
Railway test deployment
2 parents 03d0c6e + be3089c commit a5adc59

File tree

7 files changed

+62
-14
lines changed

7 files changed

+62
-14
lines changed

.dockerignore

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Don't include any of these files in the Dockerfile build context.
22
# This will improve build performance and reduce the final image size.
3-
# See: https://docs.docker.com/engine/reference/builder/#dockerignore-file
43
.git/
54
.github/
65
.dockerignore
@@ -9,5 +8,12 @@
98
.rubocop.yml
109
CHANGELOG.md
1110
docker-compose.yml
12-
log/
11+
node_modules/
12+
log/*
13+
tmp/*
14+
!log/.keep
15+
!tmp/.keep
16+
storage/*
17+
public/assets
18+
public/packs
1319
test/

Dockerfile

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# The image to build from.
2-
FROM ruby:3.2.1
2+
FROM ruby:3.2.1-slim
33

44
# Properties/labels for the image.
55
LABEL maintainer="Notebook.ai Contributors"
@@ -12,14 +12,24 @@ ENV RAILS_ENV=${RAILS_ENV}
1212
RUN groupadd --system --gid 1000 notebookai && \
1313
useradd --system --home-dir /home/notebookai --gid notebookai --uid 1000 --shell /bin/bash notebookai
1414

15-
# Install system dependencies
16-
RUN curl -fsSL https://deb.nodesource.com/setup_16.x | bash - && \
17-
apt-get update -qq && \
18-
apt-get install -y build-essential libpq-dev nodejs imagemagick libmagickwand-dev curl && \
15+
# Install system dependencies (including curl which is needed for Node download, and git for Bundler)
16+
RUN apt-get update -qq && \
17+
apt-get install -y build-essential libpq-dev imagemagick libmagickwand-dev curl git libjemalloc2 && \
1918
rm --recursive --force /var/lib/apt/lists/*
2019

21-
# Install yarn via npm (avoids the deprecated apt-key approach)
22-
RUN npm install -g yarn
20+
# Install Node.js 16.x explicitly and support both ARM and x64 architectures
21+
RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" && \
22+
case "${dpkgArch##*-}" in \
23+
amd64) ARCH='x64';; \
24+
arm64) ARCH='arm64';; \
25+
*) echo "unsupported architecture"; exit 1 ;; \
26+
esac && \
27+
curl -fsSLO "https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-$ARCH.tar.gz" && \
28+
tar -xzf "node-v16.20.2-linux-$ARCH.tar.gz" -C /usr/local --strip-components=1 && \
29+
rm "node-v16.20.2-linux-$ARCH.tar.gz"
30+
31+
# Install yarn via npm (matches local tools specification)
32+
RUN npm install -g yarn@1.22.22
2333

2434
# Set the notebookai user's home directory as the working directory.
2535
WORKDIR /home/notebookai
@@ -37,11 +47,21 @@ COPY . .
3747
# Adjust permissions on all copied files to match the system user
3848
RUN chown -R notebookai:notebookai /home/notebookai
3949

50+
# Drop down to the unprivileged user before running Rake, so any files it generates (like logs) are owned correctly
51+
USER notebookai
52+
53+
# Precompile assets during docker build to prevent OOM memory spikes at runtime
54+
# We strictly limit Node.js memory to 1GB to prevent Railway's Builder container from OOMing
55+
RUN NODE_OPTIONS="--max_old_space_size=1024" SECRET_KEY_BASE=dummy bundle exec rake assets:precompile
56+
4057
# This image should expose port 3000.
4158
EXPOSE 3000/tcp
4259

43-
# Run unprivileged
44-
USER notebookai
60+
# Enable jemalloc to drastically reduce memory fragmentation and usage
61+
ENV LD_PRELOAD="libjemalloc.so.2"
62+
63+
# Configure the main process to run when running the image
64+
ENTRYPOINT ["./docker-entrypoint.sh"]
4565

4666
# Start the server using Puma!
4767
CMD ["bundle", "exec", "puma", "-C", "config/puma.rb", "-e", "development", "-b", "tcp://0.0.0.0:3000"]

config/environments/development.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
Rails.application.configure do
22
# Settings specified here will take precedence over those in config/application.rb.
33

4+
# Allow PR environments on Railway (auto-deployed dynamic domains)
5+
config.hosts << /.*\.up\.railway\.app/
6+
config.hosts << ENV["RAILWAY_PUBLIC_DOMAIN"] if ENV["RAILWAY_PUBLIC_DOMAIN"].present?
7+
48
# In the development environment your application's code is reloaded on
59
# every request. This slows down response time but is perfect for development
610
# since you don't have to restart the web server when you make code changes.
711
config.cache_classes = false
812

913
# Do not eager load code on boot.
10-
config.eager_load = true
14+
# We set this to false to save hundreds of megabytes of memory on boot.
15+
config.eager_load = false
1116

1217
# Show full error reports.
1318
config.consider_all_requests_local = true

config/environments/production.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Rails.application.configure do
22
# Settings specified here will take precedence over those in config/application.rb.
33

4+
# Allow PR environments on Railway (auto-deployed dynamic domains)
5+
config.hosts << /.*\.up\.railway\.app/
6+
config.hosts << ENV["RAILWAY_PUBLIC_DOMAIN"] if ENV["RAILWAY_PUBLIC_DOMAIN"].present?
7+
48
# Code is not reloaded between requests.
59
config.cache_classes = true
610

config/puma.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
# Workers do not work on JRuby or Windows (both of which do not support
2424
# processes).
2525
#
26-
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
26+
# In development, default to 0 workers (single mode) to save memory and allow for easier debugging.
27+
workers ENV.fetch("WEB_CONCURRENCY") { ENV.fetch("RAILS_ENV", "development") == "development" ? 0 : 2 }
2728

2829
# Use the `preload_app!` method when specifying a `workers` number.
2930
# This directive tells Puma to first boot the application and load code

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ services:
44
build: .
55
ports:
66
- "3000:3000/tcp"
7-
command: sh -c "rake db:migrate && rm -f tmp/pids/server.pid && exec rails server -b 0.0.0.0"
7+
command: sh -c "rake db:migrate && exec rails server -b 0.0.0.0"
88
volumes:
99
- "./:/home/notebookai"

docker-entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Remove a potentially pre-existing server.pid for Rails.
5+
rm -f /home/notebookai/tmp/pids/server.pid
6+
7+
# Ensure the database is prepared (migrated & seeded) before starting the server
8+
bundle exec rails db:prepare
9+
bundle exec rails db:seed
10+
11+
# Then exec the container's main process (what's set as CMD in the Dockerfile).
12+
exec "$@"

0 commit comments

Comments
 (0)