Dockerizing Your Legacy Rails App: No More 'Works on My Machine'
In 2013, we're all tired of the same problem. You have a Ruby on Rails application that requires a specific version of Ruby (1.9.3), a specific version of MySQL, and a handful of system libraries for gems like nokogiri or rmagick. A new developer joins the team and spends two days just trying to get the app running on their Mac.
The answer is Docker. It's still new (0.x versions!), but it's already changing how we think about deployment and development environments.
The Problem: Ruby Dependency Hell
Before Docker, we used rvm or rbenv for Ruby versions and bundler for gems. But what about the underlying system dependencies? If the system has the wrong version of libxml2, your gem installation will fail, and your app won't start.
The Solution: The Dockerfile
In 2013, a basic Dockerfile for a Rails app looks like this:
# We start with a base image that has Ruby 1.9.3
FROM ruby:1.9.3
# Install system dependencies
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
# Create the app directory
RUN mkdir /myapp
WORKDIR /myapp
# Copy the Gemfile and Gemfile.lock
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
# Install gems
RUN bundle install
# Copy the rest of the application
COPY . /myapp
# Expose the Rails port
EXPOSE 3000
# Start the server
CMD ["rails", "server", "-b", "0.0.0.0"]
Docker Compose (formerly 'fig')
In late 2013, we're also starting to use fig (which will soon become docker-compose). It allows us to define our entire stack—Rails app, MySQL database, and Redis—in a single file.
# fig.yml (2013 era)
db:
image: mysql:5.5
environment:
MYSQL_ROOT_PASSWORD: password
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
links:
- db
Why It's a Game Changer
- Isolation: The database and app are completely isolated from the host machine.
- Consistency: The environment is exactly the same in development, staging, and production.
- Fast Onboarding: A new developer just runs
fig upand they are ready to code.
In 2013, if you're not looking at containerization, you're missing the future of DevOps. Docker is the bridge between developers and operations that we've been waiting for.