Skip to content

Instantly share code, notes, and snippets.

@peterkappus
Last active August 6, 2019 15:56
Show Gist options
  • Save peterkappus/339fb68df044401d2657b5d21c12c99a to your computer and use it in GitHub Desktop.
Save peterkappus/339fb68df044401d2657b5d21c12c99a to your computer and use it in GitHub Desktop.
New Docker, Rails, Postgres app in one bash script
#This script will create the required Dockerfile, docker-compose.yml, Gemfile, etc. and create a new rails app (using postgres)
#BEWARE! It will overwrite any existing files in the folder including Dockerfile, docker-compose.yml, and README.md
#the databses will be called "myapp_development", and "myapp_test"
cat << EOF > Dockerfile
FROM ruby:latest
#USER $USER
#make sure we don't get tripped up by any inherited proxies
#RUN unset HTTP_PROXY
#RUN unset HTTPS_PROXY
#NOTE!!!!!! If you're behind a proxy, you'll need to set it explicitly like so
#ENV http_proxy=http://myproxy.com:80
#ENV https_proxy=http://myproxy.com:80
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs vim man postgresql-client
#install latest nodejs to support autoprefixer
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
ENV APP_HOME /myapp
RUN mkdir \$APP_HOME
WORKDIR \$APP_HOME
RUN gem install rails
ADD Gemfile* \$APP_HOME/
RUN bundle install
EOF
#create our Gemfile
cat << EOF > Gemfile
source 'https://rubygems.org'
gem 'rails', '5.2.1'
EOF
#I guess we need one of these before we build...
touch Gemfile.lock
# four our database files
mkdir -p tmp/db
#create our docker-compose
cat << EOF > docker-compose.yml
version: '3'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
EOF
#create the rails site...and populate our Gemfile with the stuff needed by rails
docker-compose run web rails new . --force --database=postgresql
#On linux systems (where Docker must run as root), you'll need to open up permissions on the files created.
#!!!
#sudo chmod -R 777 .
#now build everything (since we now have the right gems in the gemfile)
docker-compose build
#tweak the database config to point to our db container
cat << EOF > config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password:
pool: 5
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
EOF
#start it up (in the background using &)
docker-compose up &
#wait for the db to appear...
sleep 5
#create the database(s)
docker-compose run web rake db:create
#throw in a Procfile for heroku
cat << EOF > Procfile
#Procfile for heroku
#see https://devcenter.heroku.com/articles/procfile
web: bundle exec rails server -p \$PORT
EOF
#visit!
open http://localhost:3000
#To gracefully shut down, use "docker-compose down"
#to allow web console use from a docker host, add this to env/development.rb
#config.web_console.whitelisted_ips = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment