Skip to content

Instantly share code, notes, and snippets.

Last active Sep 10, 2020
What would you like to do?
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 | bash && apt-get install -y nodejs && npm i -g yarn && gem install rails && rails new $1 $2"
echo "FROM ruby:latest
RUN mkdir /"$1"
RUN apt-get update
RUN curl -sL | bash -
RUN apt-get install -y nodejs
RUN npm i -g yarn
RUN gem install bundler
COPY Gemfile /"$1"/Gemfile
COPY Gemfile.lock /"$1"/Gemfile.lock
RUN bundle install
COPY . /"$1"
" > "$1"/Dockerfile
echo "version: '3.6'
context: .
- .:/"$1"
- '3000:3000'
command: bash -c \"rm -f tmp/pids/ && bundle exec rails s -p 3000 -b ''\"" > "$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

This comment has been minimized.

Copy link

@CrazyOptimist CrazyOptimist commented Sep 10, 2020

Great! 👍

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