I use Docker for all my client work, and most of my personal projects too. I love it. It’s saved me a ton of money because it uses so much less RAM than Vagrant — what I had been using.
Benefits of this config
docker-compose upto set up and launch the dev environment (Yep, a one-liner like
vagrant up– that’s the goal.)
- One easy-to-install dependency to get coding on a new computer: Docker. (Versus two complex ones when using Vagrant.)
- A true development-oriented config: Source code is mounted so that changes in the container appear on the host, and vice-versa.
- Fast re-builds because the
DOCKERFILEis written to help Docker cache the images.
- Syncing with the Postgres startup delay.
- All the crappy little dependencies installed.
- No weird hacks.
Now the three files, followed by instructions and my comments. Or jump right to the GitHub Repo.
|- type: bind|
|# Ruby on Rails Development Environment|
|# Set up Linux|
|RUN apt-get update|
|RUN apt-get install -y build-essential inotify-tools libpq-dev nodejs postgresql-client|
|# Ensure the app's dependencies are installed|
|echo "bundle install --without=production..."|
|bundle install --without=production|
|# Wait for Postgres to become available.|
|echo "Checking for Postgres..."|
|until psql -h db -U "postgres" -c '\q' 2>/dev/null; do|
|>&2 echo "Postgres is unavailable - sleeping"|
|echo "Postgres is available: continuing with database setup..."|
|# Potentially Set up the database|
|echo "bundle exec rake db:setup..."|
|# Start the web server|
|echo "bin/rails s -p 3000 -b '0.0.0.0'..."|
|bin/rails s -p 3000 -b '0.0.0.0'|
How to Dockerize a Rails app
- Copy the three config files to the root folder of an existing Rails project. Make
chmod +x run.sh.
- Edit your development database settings to connect to Postgres at host
postgres, password is an empty string.
- Spin it up with
Your Rails app should be up and running at
http://localhost:3000. You should see someething like this:
Now a little about this configuration
These files are the result of several hours spent fine-tuning it and going through the Docker file references. I wanted a “online-liner” container-based dev environment; a Docker equivalent of
vagrant up. To get this, I read a ton of example configs and got a boost from this excellent post by Alex Kleissner. He had the nice idea of a run.sh script to do some things like synchronize Phoenix startup and Postgres.
Also available as a GitHub repo.