DEV Community

Cover image for Create new Rails 6 project with Docker
Rogier van den Berg
Rogier van den Berg

Posted on

Create new Rails 6 project with Docker

If you want to set-up a new Rails project, but you don't want to go through the hassle of setting op Ruby, node, Yarn, Rails itself, etc. you could do it with Docker. That way you can really fast start a new project, without possible version conflicts, installs, etc. on your computer...

Let's go! πŸš€

Prerequisite: Have Docker installed

If not, install Docker first

Create the directory for your project

Open a terminal, create a project folder somewhere and go into that directory:

mkdir my_rails_project ; cd my_rails_project
Enter fullscreen mode Exit fullscreen mode

Setup your project

First, you need to start a container to open your folder with, to install Rails.
Run your container:

docker run --rm -v ${PWD}:/usr/src -w /usr/src -ti starefossen/ruby-node /bin/bash
Enter fullscreen mode Exit fullscreen mode

Within the container, install Rails first:

gem install rails
Enter fullscreen mode Exit fullscreen mode

After installing Rails, create you project. Because your container working directory is bound to you project folder, the Rails project will be created there:

rails new .
Enter fullscreen mode Exit fullscreen mode

(mind the . at the end! This will install the project in the current directory)

After creating the project, close your container with exit. Your container will be closed and removed. And you'll end up with a shiny new Rails install, without having Rails, Ruby, etc. on your computer.

Create a Dockerfile

Next, to Run your project, create a Dockerfile file in the root of your project, and paste the following (or whatever setup you'd prefer) in that file:

FROM starefossen/ruby-node

# Set the workdir inside the container
WORKDIR /usr/src/app

# Set the gemfile and install
COPY Gemfile* ./
RUN bundle install

# Copy the main application.
COPY . ./

# Expose port 3000 to the Docker host, so we can access it
# from the outside.
EXPOSE 3000

# The main command to run when the container starts. Also
# tell the Rails dev server to bind to all interfaces by
# default.
CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Enter fullscreen mode Exit fullscreen mode

Build you container

Build a shiny new Docker container, based on a Ruby + Node Docker Image, having your project in there, executed on launch:

docker build -t my_project .
Enter fullscreen mode Exit fullscreen mode

Run your container

Execute:

docker run \
-p 3000:3000 \
-v $(pwd):/usr/src/app/ \
my_project
Enter fullscreen mode Exit fullscreen mode

This runs your built container, forwarding port 3000 and connects the current folder (your project folder) to the one inside the container (so you can edit files while it is running).

Check it out

Open your browser en go to http://localhost:3000 Yay! You’re on Rails! πŸš‚

Log in to your container

If you want to go inside your container (to create controllers, views, models, etc.) run:

docker exec -it $(docker ps -q) /bin/bash
Enter fullscreen mode Exit fullscreen mode

This will execute an interactive (so you can actually use it) /bin/bash inside the running container.

That's all for now! πŸ‘‹

Image credit: Jason White

Top comments (3)

Collapse
 
almokhtar profile image
almokhtar bekkour

What if i don't want to install rails in my machine and just pull it from source ? how to do that ?

Collapse
 
leonardogrtt profile image
Leonardo Girotto

I think when he runs gem install rails command, he is inside a temporary container, that is just used to scaffold the project.
As he said:
"After creating the project, close your container with exit. Your container will be closed and removed. And you'll end up with a shiny new Rails install, without having Rails, Ruby, etc. on your computer."

Collapse
 
leonardogrtt profile image
Leonardo Girotto

Here is my Dockerfile for Rails 6

FROM ruby:3.0.0-preview2-buster

# Move to workspace
WORKDIR /usr/src/app

# Update packages
RUN apt update -qq

# Install packages
RUN apt install -y curl \
                vim \
                wget \
                zip \
                unzip \
                build-essential


# Install NodeJS LTS
RUN sh -c "curl -sL https://deb.nodesource.com/setup_lts.x | bash -"
RUN apt install -y nodejs

# Install Yarn package
RUN sh -c "npm install yarn --global"

# Install gems 'bundler' and 'rails'
RUN sh -c "gem install bundler && gem install rails -v 6.1"
Enter fullscreen mode Exit fullscreen mode


`