Skip to content

Commit b232568

Browse files
authored
prod dockerfile (#118)
* Dockerfile builds the base successfully now * we won't actually be behind nginx here * pare down the libraries that are not actually used * convert to a multistage build * use the `-slim` image this is about half the size of the base `bookworm` image tag * remove yarn shim * add css manifest * add a "production" service doesn't run unless explicitly triggered * raise if asked for an asset we don't know about
1 parent c5108ec commit b232568

File tree

9 files changed

+79
-43
lines changed

9 files changed

+79
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/public/system/attachments/*
2525
/public/system/thumbnails/*
2626
/public/ckeditor_assets
27+
/public/assets/
2728

2829
# Ignore Byebug command history file.
2930
.byebug_history

Dockerfile

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1-
FROM ruby:2.7.8-buster
1+
FROM ruby:3.3.8-slim AS base
2+
3+
# Set working directory
4+
WORKDIR /app
5+
6+
# Set environment variables
7+
ENV RAILS_ENV=production \
8+
RACK_ENV=production \
9+
BUNDLE_PATH=/gems \
10+
BUNDLE_WITHOUT="development test" \
11+
BUNDLE_BIN=/gems/bin \
12+
PATH="/gems/bin:$PATH"
13+
14+
# Install bundler
15+
RUN gem install bundler -v 2.5.22
16+
17+
18+
FROM base AS assets
219

320
# Install basic Linux packages
421
RUN apt-get update -qq && apt-get install -y \
522
build-essential \
6-
libpq-dev \
723
nodejs \
824
yarn \
9-
git \
10-
curl \
1125
imagemagick \
1226
libvips \
1327
tzdata \
@@ -16,22 +30,7 @@ RUN apt-get update -qq && apt-get install -y \
1630
libffi-dev \
1731
libreadline-dev \
1832
libssl-dev \
19-
zlib1g-dev \
20-
libsqlite3-dev \
21-
sqlite3
22-
23-
# Set working directory
24-
WORKDIR /app
25-
26-
# Set environment variables
27-
ENV RAILS_ENV=production \
28-
RACK_ENV=production \
29-
BUNDLE_PATH=/gems \
30-
BUNDLE_BIN=/gems/bin \
31-
PATH="/gems/bin:$PATH"
32-
33-
# Install bundler
34-
RUN gem install bundler -v 2.4.12
33+
zlib1g-dev
3534

3635
# Copy app code and install dependencies
3736
COPY . .
@@ -41,21 +40,40 @@ RUN bundle install --without development test
4140
# These envs are used in the rails application. While they are entirely
4241
# unrelated to the docker build process, they are required for the app to run.
4342
# Without these build args the asset precompilation will fail.
44-
ARG SECRET_KEY_BASE
45-
ARG AWS_ACCESS_KEY_ID
46-
ARG AWS_SECRET_ACCESS_KEY
47-
ARG AWS_REGION
48-
ARG AWS_S3_BUCKET
49-
ARG SMTP_USERNAME
50-
ARG SMTP_PASSWORD
51-
ARG SMTP_SERVER
52-
ARG SMTP_PORT
5343

5444
# Precompile assets (if applicable)
55-
RUN bundle exec rake assets:precompile
45+
RUN SECRET_KEY_BASE=1 bundle exec rake assets:precompile
46+
47+
48+
FROM base AS server
49+
50+
RUN apt-get update -qq && apt-get install --no-install-recommends -y \
51+
imagemagick \
52+
libvips \
53+
tzdata \
54+
libxml2 \
55+
libxslt1.1 \
56+
libffi8 \
57+
libreadline8 \
58+
libssl3 \
59+
zlib1g \
60+
&& apt-get clean \
61+
&& rm -rf /var/lib/apt/lists/*
62+
63+
64+
65+
66+
# Set working directory
67+
WORKDIR /app
68+
69+
COPY --from=assets /gems /gems
70+
COPY --from=assets /app/public/assets /app/public/assets
71+
72+
# Copy app code and install dependencies
73+
COPY . .
5674

5775
# Expose port (default Rails)
5876
EXPOSE 3000
5977

6078
# Start the server (customize to your app server if needed)
61-
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
79+
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
*= require_self
3+
*= require awbw
4+
*/

bin/yarn

Lines changed: 0 additions & 11 deletions
This file was deleted.

config/database.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ test:
1414
<<: *base
1515
pool: 5
1616
database: awbw_test
17+
18+
production:
19+
<<: *base

config/environments/development.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
# number of complex assets.
5151
config.assets.debug = true
5252

53+
config.assets.unknown_asset_fallback = false
54+
5355
# Suppress logger output for asset requests.
5456
config.assets.quiet = true
5557

config/environments/production.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
# Disable serving static files from the `/public` folder by default since
2222
# Apache or NGINX already handles this.
23-
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
23+
config.public_file_server.enabled = true
2424

2525
# Compress CSS using a preprocessor.
2626
# config.assets.css_compressor = :sass

docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ services:
4545
stdin_open: true
4646
restart: unless-stopped
4747

48+
49+
prod:
50+
profiles:
51+
- production
52+
build:
53+
context: .
54+
dockerfile: Dockerfile
55+
target: server
56+
depends_on:
57+
- database
58+
environment:
59+
DATABASE_URL: trilogy://root@database:3306/awbw_development
60+
RAILS_LOG_TO_STDOUT: "true"
61+
SECRET_KEY_BASE: "foobar"
62+
ports:
63+
- 3030:3000
64+
65+
66+
4867
volumes:
4968
mysql-data:
5069

0 commit comments

Comments
 (0)