Skip to content

Instantly share code, notes, and snippets.

@aimerib
Last active Sep 10, 2020
Embed
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 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

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