Skip to content

Instantly share code, notes, and snippets.

@lukemorton
Last active February 8, 2018 10:08
Show Gist options
  • Save lukemorton/9d68dacdc1bf3d17ed453ca91c0ff028 to your computer and use it in GitHub Desktop.
Save lukemorton/9d68dacdc1bf3d17ed453ca91c0ff028 to your computer and use it in GitHub Desktop.
Using Docker like Vagrant

Using Docker like Vagrant

You may consider it heresy to use Docker like Vagrant however hear me out. This setup provides us a number of benefits:

  • We treat Docker as a virtual machine which make it easier to reason about, you know there is only one Docker container in use for the application at all times
  • By default we need to SSH into the Docker container with make ssh which then means subsequent command runs don't require Docker to initialise
  • We can stay logged into Docker so re-running tests is faster, and we can run specific tests, unlike make test
  • We no longer have to prefix commands in documentation with bin/docker_run bundle exec rspec instead we say make sure you are logged into the Docker container and then run commands as normal
  • It also means help on Stackoverflow can be followed more easily without needing to prefix things with bin/docker_run
  • PID issue no longer happens as you're always in the shell session that booted rails
  • Less files in repo (no Dockerfile, no dev.sh, no bin/docker_run)

The biggest known issue is that if you restart Docker, you need to install dependencies and setup DB from scratch. This is the same whether you treat Docker as Vagrant or not, though.

README for a Rails app

Quickstart

In order to get your development environment up and running, simply follow these steps.

  1. Install Docker
  2. Boot up docker with make up
  3. SSH into docker with make ssh
  4. Install rails dependencies with bundle install
  5. Setup database with bundle exec rails db:setup
  6. Test it works by running the server bundle exec rails s

If you need help, ask the original engineering team.

Developing

Below is a guide to common development tasks you'll probably need whilst working on this application.

Before running any of these commands, you need to make sure Docker is running with the following:

make up

If Docker is running but it doesn't seem to be working, you can follow the advice below.

Running development server

make ssh
bundle exec rails s

Running tests

To run all tests, you can do the following:

make ssh
bundle exec rspec

If you want to run a particular test:

make ssh
bundle exec rspec spec/path/to_my_spec.rb

Working with the database

Creating a migration and running it:

make ssh
bundle exec rails g migration CreateProducts name:string
bundle exec rails db:migrate

Recreating the database:

make ssh
bundle exec rails db:reset

Working with Docker

To setup Docker for the first time:

make up

When things go wrong or you stop working on this application, you may want to tear down the Docker setup. You can use this command:

make down

Or if things have gone wrong and you want to start from scratch, you can restart the whole setup:

make restart
make ssh
bundle install
bundle exec rails db:setup
version: '2'
services:
db:
image: postgres:alpine
ports:
- '15432:5432'
environment:
POSTGRES_PASSWORD: postgres
web:
image: madetech/docker-rails-deps:ruby2.5.0
working_dir: /app
command: bash
volumes:
- .:/app
volumes_from:
- bundle
ports:
- "3000:3000"
environment:
DATABASE_PASSWORD: postgres
BUNDLE_PATH: /bundle
BUNDLE_SILENCE_ROOT_WARNING: "true"
depends_on:
- db
bundle:
image: busybox
volumes:
- /bundle
up:
docker-compose build
docker-compose up -d
down:
docker-compose down -v
restart: down up
ssh:
docker-compose run --service-ports web
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment