Dockerfile ARG not set from docker-compose.yml file

I ran into an issue where I need to build a Rails environment with the :development group, which doesn't happen in the default Rails Dockerfile.

To fix this, we can use ARGs in the Dockerfile to enable Docker Compose to pass in variables at build time. However, you CANNOT use them after the FROM declaration as I found out from this Stack Overflow post.

So, if you want to build your Rails Dockerfile with the :development gems, you need to set it up like this:

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.2
# Available to override via docker compose
ARG BUNDLE_WITHOUT=development
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim AS base

And then modify your compose file:

environment:
  RAILS_ENV: development
  BUNDLE_WITHOUT: none