I’ve followed along with the Hotiwired ATS once and now am working through it again using Bootstrap, which seems like it should be a minor change. I keep running into an issue that I can’t figure out with yarn.
Basically, yarn isn’t available in the container and for the life of me I can’t figure out why. I’ve made some small changes to the basic Dockerfile created by Rails in an effort to get it working and based on some other successful container/compose starts and hoping the community can see something I can’t.
Dockerfile
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
# FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim AS base
FROM ruby:${RUBY_VERSION} as base
# Rails app lives here
WORKDIR /rails
# Set production environment
ENV RAILS_ENV="development"
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
# 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 libpq-dev libvips node-gyp pkg-config python-is-python3 gnupg2
# Install JavaScript dependencies
ARG NODE_VERSION=18.19.1
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 &&
# npm install -g yarn@$YARN_VERSION &&
rm -rf /tmp/node-build-master
RUN echo "Installing yarn"
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg -o /root/yarn-pubkey.gpg && apt-key add /root/yarn-pubkey.gpg
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y --no-install-recommends nodejs yarn
# Install application gems
COPY Gemfile Gemfile.lock ./
RUN bundle install &&
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
RUN bundle exec bootsnap precompile --gemfile
# Install node modules
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
# Copy application code
RUN gem update --system
COPY . .
# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/
# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
# RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
# 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 libvips postgresql-client &&
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 /rails /rails
# Run and own only the runtime files as a non-root user for security
# RUN useradd rails --create-home --shell /bin/bash &&
# chown -R rails:rails db log storage tmp
# USER rails:rails
# 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"]
docker-compose
services:
web:
build: .
command: sh -c "rm -f tmp/pids/server.pid && bin/dev"
volumes:
- .:/rails
- /rails/node_modules
ports:
- "3000:3000"
- "8082:8082"
env_file:
- rails.env
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
stdin_open: true
tty: true
db:
image: postgres:15
expose:
- "5432"
ports:
- "5432:5432"
env_file:
- rails.env
volumes:
- pg_data:/var/lib/postgresql/data
healthcheck:
test: pg_isready -U postgres
interval: 2s
timeout: 5s
retries: 30
redis:
image: redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
volumes:
pg_data:
redis_data:
I’ve adjusted the original to get yarn from dl.yarnpkg.com rather than using npm (which didn’t work either). When the container starts up I constantly get unknown command: yarn build:css --watch
not found and running yarn --version
get a yarn: not found
.