Setup Ruby on Rails with Docker

Azzam Jihad Ulhaq
4 min readSep 15, 2023

--

Alright, let’s set up a basic Rails development environment using Docker. This setup won’t include a database for now.

Here’s a step-by-step guide:

Dockerfile

First, let’s set up a Dockerfile to describe how to build your Rails application image:

# Use the official Ruby image from the DockerHub
FROM ruby:2.7

# Set the 'app' as the working directory inside the image
WORKDIR /app

# Install Rails gem
RUN gem install rails

# Start the main process (bash for now)
CMD ["bash"]

Build the Docker Image:

docker build -t myrailsimage .

Run the Docker Container:

Now, you’ll run a Docker container based on the image we just created. This will drop you into a bash shell within the container.

docker run -it --rm -v $(pwd):/app -p 3000:3000 myrailsimage

Create a New Rails App:

Now, inside the Docker container’s bash shell, run:

rails new .

This will initiate a new Rails app in the current directory (/app). The files will also appear in your local directory due to the volume mount we specified (-v $(pwd):/app).

You will get something like this in your terminal

And in your VS Code will looks like this

As you can see, the rails files also appear in our local machine and can be accessed via VS Code.

Exit the Container:

Once the Rails app is generated, you can type exit to leave the Docker container's bash shell.

Modify the Dockerfile (again):

Now that you have a Gemfile (because Rails just generated one for you), you can adjust the Dockerfile to make it aware of the Rails app:

# Use the official Ruby image from the DockerHub
FROM ruby:2.7

# Set the 'app' as the working directory inside the image
WORKDIR /app

# Copy the Gemfile and Gemfile.lock from the host to the working directory inside the image
COPY Gemfile Gemfile.lock ./

# Install bundler and gems
RUN gem install bundler && bundle install

# Copy the rest of the Rails app
COPY . .

# Start the main process (puma server by default for Rails)
CMD ["rails", "server", "-b", "0.0.0.0"]

Rebuild and Run:

Now that you’ve changed the Dockerfile, you need to rebuild the image and then use docker-compose to manage the container:

docker build -t myrailsimage .
docker run -it --rm -v $(pwd):/app -p 3000:3000 myrailsimage

This will start the Rails server. You should now be able to access the Rails welcome page by visiting http://localhost:3000.

Use docker-compose for better experience

Using docker-compose will make managing your Rails container (and any others you might add in the future) much simpler.

Here’s a step-by-step guide:

Create a docker-compose.yml file:

In the root directory of your Rails project, create a docker-compose.yml file with the following content:

version: '3'

services:
web:
build: .
command: rails server -b '0.0.0.0'
volumes:
- .:/app
ports:
- "3000:3000"

This configuration:

  • Tells docker-compose to build an image using the Dockerfile in the current directory.
  • Mounts the current directory to /app inside the container so changes you make on your local machine will reflect inside the container.
  • Maps port 3000 inside the container to port 3000 on your local machine so you can access the Rails server.

Start the Rails Server with docker-compose:

In the terminal, navigate to the root directory of your Rails project (where your docker-compose.yml file is located) and run:

docker-compose up -d

The -d flag will start the services in detached mode, meaning they'll run in the background.

Check if everything is running:

You can see the list of running services with:

docker-compose ps

Access the Rails Server:

You should be able to open a browser and access http://localhost:3000 to see your Rails application.

Stopping the Services:

When you want to stop the services, simply run:

docker-compose down

This setup makes it easy to manage your Rails development environment. If in the future you wish to add additional services like a database, Redis, etc., you can simply extend the docker-compose.yml file to include those services.

--

--

No responses yet