Skip to content

Instantly share code, notes, and snippets.

@aimerib
Last active September 10, 2020 01:43
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 aimerib/64e4673ee26147c334401d474956647c to your computer and use it in GitHub Desktop.
Save aimerib/64e4673ee26147c334401d474956647c to your computer and use it in GitHub Desktop.
Quickly generating dockerized rails projects
#append this to the end of your ~/.zshrc or ~/.bashrc
generate_rails_project() {
docker run -it --rm -v "$PWD":/"$1" -w /"$1" ruby:latest /bin/bash -c "curl -sL https://deb.nodesource.com/setup_12.x | bash && apt-get install -y nodejs && npm i -g yarn && gem install rails && rails new $1 $2"
echo "FROM ruby:latest
\n
\n
RUN mkdir /"$1"
WORKDIR /"$1"
\n
RUN apt-get update
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs
RUN npm i -g yarn
RUN gem install bundler
\n
\n
COPY Gemfile /"$1"/Gemfile
COPY Gemfile.lock /"$1"/Gemfile.lock
RUN bundle install
COPY . /"$1"
" > "$1"/Dockerfile
echo "version: '3.6'
services:
$1:
build:
context: .
volumes:
- .:/"$1"
ports:
- '3000:3000'
command: bash -c \"rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'\"" > "$1"/docker-compose.yaml
}
alias nrp=generate_rails_project

to make changes available immediately, run:

source ~/.zshrc

or if you're not running the ZSH shell:

source ~/.bashrc

To use this new alias:

nrp my_project
cd my_project
docker-compose up --build

in a new tab you can start your IDE and issue commands to the docker container like so:

code .
docker-compose run my_project rails generate controler Welcome index

You can also start a new Rails API application as such:

nrp my_project --api

Things to note:

  • this script will use the latest version of Ruby and Rails available
  • You need docker and docker compose installed for this setup to work
@crazyoptimist
Copy link

Great! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment