This is an example of how I use Docker and Docker Compose to develop my rails apps.
It is an ideal project setup for new and experienced developers alike, and allows to a nearly trouble-free environment setup in their development machines.
In this example we'll fire up a full app environment consisting of the following:
- A postgres container which hosts the app's database.
- A redis container which hosts the app's job queue.
- An example Rails app running 3 processes on separate containers:
- A
web
container running the Rails web server. - A
worker
container running the app's job processor (Sidekiq). - A
test
container running guard, which fires tests automatically whenever the app code changes. We'll also use this container to launch remote debugging sessions to the runningweb
andworker
containers.
- A
You'll need to follow some instructions to get this example running:
You must have both Docker (or a Docker Machine host) and Docker Compose running on your machine.
You can find instructions for the recommended setup on Linux, Mac OSX and Windows 8+.
You should clone the project into a conveniently-named directory, as this repo's name is big enough to make typing docker/compose commands tiresome, should the need arise:
git clone https://github.com/vovimayhem/docker-compose-rails-dev-example.git example
The setup-development-with-docker.sh
script will do the following:
- Generate an
.env
file, into which we'll store sensitive info (passwords, SECRET_KEY_BASE, etc). This file shouldn't be in version control - Generate the app's
config/secrets.yml
file, which will read theSECRET_KEY_BASE
environment variable for all rails app environments. - Run the
bundler
command to install the app dependencies in the app'svendor/bundle
folder. - Run the 'rake db:setup' command to initialize the database.
cd example && ./setup-development-with-docker.sh
Edit if you need the generated .env
file.
docker-compose up -d
That's it! Check the running app web interface: http://localhost:3000
I usually do some tricks to aid in my day to day activities:
- Modify the
/etc/hosts
file for rails apps that depend on subdomains. - Generate aliases to the docker-compose command, as the
docker-compose
command tires my delicate fingers... - Mess around with the compose commands to start & stop the project's containers.
- VERY IMPORTANT Launch a remote debug session easily to any of the project's app containers, as Docker makes debugging with byebug not so trivial.
- Running rails/rake commands on one-off containers, which is necessary quite often.
- Create a new rails app using docker, if you find yourself in the need of creating a new app, and you are already running this example project.
It uses MRI ruby 2.2.2. I'll make some branches using different engines (i.e. Rubinius, JRuby)
- Everything Docker
- GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS (Seriously, thoughtbot?)
- Introducing 'heroku docker:release': Build & Deploy Heroku Apps with Docker geez! These guys are on EVERYTHING!
I'd love to receive feedback & suggestions from everyone. If you see something off, or think there's a better way to do something this thing does, please do:
- Fork the original repository.
- Make your changes in a topic branch.
- Send a pull request.
This is free software, and may be redistributed under the terms specified in the LICENSE file.