Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
231: Dockerizing a Rails Application
You've probably heard the phrase "it works on my machine" enough times to last a lifetime. Docker is the industry's answer to that frustration, but there is a massive gap between "making it run in a container" and "making it run correctly." When I first started dockerizing Rails apps, I treated the Dockerfile like a bash script—just a list of commands to get the server up. It worked, but it was slow, bloated, and frankly, a security nightmare.
The "Just Make It Work" Approach
The naive way to Dockerize a Rails app is to throw everything into one giant layer. You might write a Dockerfile that starts with FROM ruby:latest, runs a massive apt-get install for every possible dependency, copies your entire project directory, and then runs bundle install. It looks something like this:
FROM ruby:latest
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
COPY . /app
WORKDIR /app
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]
On the surface, this is fine. Your app boots. But in a professional workflow, this approach falls apart quickly. Because you COPY . /app before running bundle install, Docker sees any change to a single CSS file or a README as a reason to invalidate the cache for the entire project. This means every time you change one line of code, you're stuck waiting several minutes for your gems to reinstall. I've seen developers waste hours of their week just waiting for "fast" containers to build because they didn't understand layer caching.
Building for Speed and Security
To do this right, we have to think about the order of operations. Docker caches each line (layer) of your Dockerfile. If a line hasn't changed, Docker skips it. The trick is to move the things that change infrequently to the top and the things that change constantly to the bottom.
First, we stop using latest. Using ruby:3.2-slim (or whatever version you're on) keeps the image size down and prevents your app from randomly breaking when a new Ruby version drops. Second, we copy the Gemfile and Gemfile.lock alone, run bundle install, and then copy the rest of the application code. This way, as long as you aren't adding new gems, the bundle install step is cached and takes milliseconds instead of minutes.
Then there's the security aspect. By default, Docker runs everything as root. If a vulnerability in your Rails app allows an attacker to execute code, they have root access to the container, which is a huge liability. I always tell my juniors: never run your app as root in production. Creating a dedicated rails user is a non-negotiable step for any app hitting a real server.
The Professional Blueprint
When you put these trade-offs together, you get a Dockerfile that is leaner and significantly faster to iterate with. We also add a .dockerignore file to prevent us from copying log/, tmp/, and node_modules/ into the image, which would otherwise bloat the size and potentially overwrite container settings with local environment junk.
# Use a specific, slim version of Ruby
FROM ruby:3.2.2-slim
# Install only the essential runtime dependencies
RUN apt-get update -qq && \
apt-get install -y build-essential libpq-dev nodejs && \
rm -rf /var/lib/apt/lists/*
# Create a non-root user for security
RUN useradd -m rails
USER rails
WORKDIR /home/rails/app
# Cache gems by copying Gemfiles first
COPY --chown=rails:rails Gemfile Gemfile.lock ./
RUN bundle install
# Now copy the rest of the app
COPY --chown=rails:rails . .
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Notice the --chown=rails:rails flag. Without it, the files are copied as root, and your rails user won't have permission to write to tmp/ or log/, leading to those annoying "Permission Denied" errors that usually lead people to just chmod 777 everything (please, don't do that).
📋 Practical Task
Exercise: Optimizing the Bloated Rails Image
You have inherited a legacy project with a Dockerfile that is taking 10 minutes to build every time a developer changes a single line of Ruby code. The current Dockerfile is as follows:
FROM ruby:latest
RUN apt-get update && apt-get install -y build-essential libpq-dev
COPY . /app
WORKDIR /app
RUN bundle install
CMD ["rails", "server", "-b", "0.0.0.0"]
Your task: Rewrite this Dockerfile to implement the following professional standards:
- Switch to a
slimimage version (assume Ruby 3.2.2). - Implement layer caching so that
bundle installonly runs when theGemfileorGemfile.lockchanges. - Create and use a non-root user named
deployto run the application. - Ensure the files are copied with the correct ownership for the
deployuser. - Clean up the
aptcache in the same layer as the installation to keep the image small.
There are no comments for now.