How to Deploy Ruby to Docker Without Writing a Dockerfile
You can containerize any Ruby application without writing a Dockerfile using migetpacks. It auto-detects your Ruby version from .ruby-version or Gemfile, installs gems with bundler, automatically precompiles Rails assets, and produces an optimized production image. One command, zero configuration.
Why Is Writing a Dockerfile for Ruby So Complicated?
A production-ready Ruby Dockerfile requires:
- Multi-stage builds to separate build dependencies from runtime
- Installing native extension build tools (
libpq-dev,libxml2-dev, etc.) - Proper bundler configuration for deployment
- Rails asset precompilation with
SECRET_KEY_BASE_DUMMY - Pruning gem cache after installation
- Production environment variables (
RAILS_ENV,RACK_ENV) - Non-root user for security
Most tutorials get the native gem dependencies wrong. You end up with images that fail to start because pg or nokogiri can't find their libraries.
How Do I Containerize a Ruby App Without a Dockerfile?
Open your terminal, navigate to your Ruby project, and run:
cd ~/projects/my-rails-app
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-rails-app:latest \
miget/migetpacks:latest
That's it. migetpacks will:
- Detect Ruby from your
Gemfile - Read version from
.ruby-versionorGemfile - Detect bundler version from
Gemfile.lock - Install system libraries for native gems
- Run
bundle installwith caching - Precompile Rails assets automatically
- Output a slim production image
How Does migetpacks Detect My Ruby Version?
Version is resolved in this order:
| Priority | Source | Example |
|---|---|---|
| 1 | RUBY_VERSION env var | RUBY_VERSION=3.3 |
| 2 | .ruby-version file | 3.4.4 |
| 3 | ruby declaration in Gemfile | ruby "3.3.0" |
| 4 | Default | Latest stable |
The ruby- prefix is automatically stripped (e.g., ruby-3.2.0 becomes 3.2.0).
What Native Gems Does migetpacks Support?
migetpacks inspects your Gemfile and Gemfile.lock to install the correct system libraries:
| Gem | Build Package | Runtime Package |
|---|---|---|
pg | libpq-dev | libpq5 |
mysql2, trilogy | libmariadb-dev | libmariadb3 |
sqlite3 | libsqlite3-dev | libsqlite3-0 |
nokogiri, rails | libxml2-dev, libxslt1-dev | libxml2, libxslt1.1 |
No more "library not found" errors at runtime.
What Does the Generated Dockerfile Look Like?
# Builder stage
FROM ruby:3.4 AS builder
WORKDIR /build
COPY Gemfile Gemfile.lock ./
ENV GEM_HOME=/build/vendor/bundle
ENV BUNDLE_PATH=/build/vendor/bundle
RUN gem install bundler -v '2.5.0' --no-document && \
bundle config set --local path '/build/vendor/bundle' && \
bundle config set --local deployment 'true' && \
bundle install --jobs 4 --retry 3
COPY . .
# Rails asset precompilation
ENV RAILS_ENV=production
ENV SECRET_KEY_BASE_DUMMY=1
RUN bundle exec rake assets:precompile && \
bundle exec rake assets:clean && \
rm -rf .git test spec vendor/bundle/ruby/*/cache
# Runtime stage
FROM ruby:3.4-slim
RUN apt-get update && apt-get install -y libpq5 tzdata ca-certificates
WORKDIR /app
COPY --from=builder /build /app
You never write or maintain this file - migetpacks handles it automatically.
How Does Rails Asset Precompilation Work?
If your project has a Rakefile and either app/assets/ or app/javascript/ directories, migetpacks automatically:
- Sets
RAILS_ENV=productionandSECRET_KEY_BASE_DUMMY=1 - Runs
bundle exec rake assets:precompile - Runs
bundle exec rake assets:clean
No configuration needed.
What Runtime Environment Variables Are Set?
RAILS_ENV=production
RACK_ENV=production
RAILS_SERVE_STATIC_FILES=enabled
RAILS_LOG_TO_STDOUT=enabled
MALLOC_ARENA_MAX=2
DISABLE_SPRING=1
PUMA_PERSISTENT_TIMEOUT=95
Does migetpacks Support Node.js for Rails?
Yes. Ruby applications commonly use Node.js for asset compilation. When package.json is detected alongside a Ruby project, Node.js is automatically added:
cd ~/projects/my-rails-app
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-rails-app:latest \
-e BUILDPACKS=ruby,nodejs \
miget/migetpacks:latest
How Do I Build Secure Distroless Images?
Enable Docker Hardened Images for minimal containers:
docker run --rm \
-v "$PWD":/workspace/source \
-v /var/run/docker.sock:/var/run/docker.sock \
-e OUTPUT_IMAGE=my-app:latest \
-e USE_DHI=true \
miget/migetpacks:latest
DHI images are distroless - no shell, no package manager, minimal attack surface.
Next Steps
- Full Ruby documentation - version detection, caching, Rails support
- Deploy on Miget - unlimited apps, one price
- Star on GitHub - migetpacks is open source
- Related guides: Elixir, Python, PHP