Skip to content

Instantly share code, notes, and snippets.

@benjamin-thomas
Created September 2, 2014 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamin-thomas/ff8e7c2c9e4cc30d880a to your computer and use it in GitHub Desktop.
Save benjamin-thomas/ff8e7c2c9e4cc30d880a to your computer and use it in GitHub Desktop.
Small script to test a rails container with docker
#!/bin/bash
set -e
DEBUG=0
function notify() {
echo -e "\033[1;33m"
echo '================================================================================================='
echo $1
echo '================================================================================================='
echo -e "\033[0m"
echo 'Press a key to continue'
if [ $DEBUG -eq 0 ];then read; fi
}
notify '0. Cleaning up if previous runs'
set -x
docker stop my_app_pg &>/dev/null && docker rm my_app_pg
set +x
notify '1. Creating a rails project with the official docker rails container on the host in /tmp/rails'
set -x
docker run --rm -it -v /tmp/rails:/tmp/rails -w /tmp/rails rails rails new my_app --database=postgresql --skip-bundle
set +x
notify '2. Creating the db config to /tmp/my_app-database.yml on the host'
cat <<EOF >/tmp/my_app-database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
host: pg
username: my_app
password: <%= ENV["MY_APP_DATABASE_PASSWORD"] %>
development:
<<: *default
database: my_app_development
test:
<<: *default
database: my_app_test
production:
<<: *default
database: my_app_production
EOF
notify '3. Copying it over with docker to bypass permission issues'
set -x
docker run --rm -it -v /tmp/rails:/tmp/rails -v /tmp/my_app-database.yml:/tmp/my_app-database.yml rails cp /tmp/my_app-database.yml /tmp/rails/my_app/config/database.yml
set +x
notify '4 Launching an official postgresql container as a daemon, just for the rails app container'
set -x
docker run -d --name my_app_pg postgres && sleep 3 #give postgresql time to startup fully
set +x
notify '5. Setup a db superuser on that new db container, by launching a temporary container that will act as a db client'
set -x
DB_PASSWORD=$(tr -dc 'a-zA-Z0-9_' </dev/urandom | head -c 14) && docker run --rm -it --env PG_PASSWORD=$(echo \'$DB_PASSWORD\') --link my_app_pg:pg postgres sh -c 'exec psql -h pg -U postgres --echo-all --command "CREATE USER my_app PASSWORD $PG_PASSWORD SUPERUSER"'
set +x
notify '6. Install the required gems and cache them in ./vendor/cache for faster rebuilds'
set -x
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app rails bundle install --path ./vendor/cache
set +x
notify '7. Create a User model + migration'
set -x
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app rails bundle exec rails g model User first_name last_name age:integer
set +x
notify '8. Create the db and apply the migration'
set -x
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app --link my_app_pg:pg rails bundle exec rake db:create db:migrate
set +x
notify '9. Create a user via the rails console'
set -x
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app --link my_app_pg:pg rails bundle exec rails runner "User.create(first_name: 'John', last_name: 'Doe', age: 999)"
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app --link my_app_pg:pg rails bundle exec rails runner 'puts "user_count=#{User.count}"'
set +x
notify '10. Launch the rails container in development mode, bind mounting the app dir for editing on the host. `bundle exec rails console` works fine and `bundle exec rails dbconsole -p` has to work too. I should be able to issue `SELECT \* FROM users;` inside psql'
set -x
docker run --rm -it --env MY_APP_DATABASE_PASSWORD=$DB_PASSWORD -v /tmp/rails/my_app:/tmp/rails/my_app -w /tmp/rails/my_app --link my_app_pg:pg --publish 127.0.0.1:3000:3000 rails /bin/bash
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment