From 4f5e052194755660d61f7a165d0d4e5a80ae2635 Mon Sep 17 00:00:00 2001 From: Adrien Poly Date: Sat, 5 Oct 2024 03:09:57 +0200 Subject: [PATCH] rails 8 docker file --- Dockerfile | 50 ++++++++++++++++++++++++++++------------------- config/deploy.yml | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2f5c43a..6f45fe5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,29 +1,41 @@ -# syntax = docker/dockerfile:1 +# syntax=docker/dockerfile:1 +# check=error=true -# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +# This Dockerfile is designed for production, not development. Use with Kamal or build'n'run by hand: +# docker build -t rails_8_es_tw . +# docker run -d -p 80:80 -e RAILS_MASTER_KEY= --name rails_8_es_tw rails_8_es_tw + +# For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html + +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version ARG RUBY_VERSION=3.3.4 -FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim as base +FROM docker.io/library/ruby:$RUBY_VERSION-slim AS base # Rails app lives here WORKDIR /rails +# Install base packages +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives + # Set production environment ENV RAILS_ENV="production" \ BUNDLE_DEPLOYMENT="1" \ BUNDLE_PATH="/usr/local/bundle" \ BUNDLE_WITHOUT="development" - # Throw-away build stage to reduce size of final image -FROM base as build +FROM base AS build # Install packages needed to build gems and node modules RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y build-essential curl git libvips node-gyp pkg-config python-is-python3 + apt-get install --no-install-recommends -y build-essential git node-gyp pkg-config python-is-python3 && \ + rm -rf /var/lib/apt/lists /var/cache/apt/archives # Install JavaScript dependencies -ARG NODE_VERSION=20.10.0 -ARG YARN_VERSION=1.22.19 +ARG NODE_VERSION=22.1.0 +ARG YARN_VERSION=1.22.22 ENV PATH=/usr/local/node/bin:$PATH RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \ /tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \ @@ -36,7 +48,6 @@ RUN bundle install && \ rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ bundle exec bootsnap precompile --gemfile - # Install node modules COPY package.json yarn.lock ./ RUN yarn install --frozen-lockfile @@ -51,26 +62,25 @@ RUN bundle exec bootsnap precompile app/ lib/ RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile +RUN rm -rf node_modules + + # Final stage for app image FROM base -# Install packages needed for deployment -RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \ - rm -rf /var/lib/apt/lists /var/cache/apt/archives - # Copy built artifacts: gems, application -COPY --from=build /usr/local/bundle /usr/local/bundle +COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}" COPY --from=build /rails /rails # Run and own only the runtime files as a non-root user for security -RUN useradd rails --create-home --shell /bin/bash && \ +RUN groupadd --system --gid 1000 rails && \ + useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \ chown -R rails:rails db log storage tmp -USER rails:rails +USER 1000:1000 # Entrypoint prepares the database. ENTRYPOINT ["/rails/bin/docker-entrypoint"] -# Start the server by default, this can be overwritten at runtime -EXPOSE 3000 -CMD ["./bin/rails", "server"] +# Start server via Thruster by default, this can be overwritten at runtime +EXPOSE 80 +CMD ["./bin/thrust", "./bin/rails", "server"] diff --git a/config/deploy.yml b/config/deploy.yml index 20f9605..8dfa4ab 100644 --- a/config/deploy.yml +++ b/config/deploy.yml @@ -19,7 +19,7 @@ proxy: ssl: true host: daisy-on-rails.adrienpoly.com # kamal-proxy connects to your container over port 80, use `app_port` to specify a different port. - app_port: 3000 + app_port: 80 # Credentials for your image host. registry: