Hello, long time no see!
In this guide I’m going to show you how to use Docker Compose to setup and run a Ruby On Rails application using the brand new version 8. In this guide I’m not going to list all new features that are shipped with the newest version. If you want to know more about, you can check the official release note.
There’s only one requirement to make this guide work, which is having Docker or OrbStack installed on your computer. That’s it. You don’t need to install Ruby, Postgres, SQLite, Redis… nothing.
This is an updated version of a quickstart guide I wrote a while back.
Setup folder
First, let’s create a directory for your project and then create 4 empty files inside of it.
mkdir my-project
cd my-project
touch Dockerfile.dev
touch docker-compose.yml
touch Gemfile
touch Gemfile.lock
Define the project
Start by setting up the files needed to build the app. The app will run inside a Docker container containing its dependencies. Defining dependencies is done using a file called Dockerfile.dev
. Let’s add the following inside of it:
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.5
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim-bookworm
# Rails app lives here
WORKDIR /rails
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git curl node-gyp sqlite3
# Install JavaScript dependencies
ARG NODE_VERSION=18.15.0
ARG YARN_VERSION=latest
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Run bundle install
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy application code
COPY . .
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Tip
It is important to use the .dev as extension for this file otherwise rails new command will replace it.
That’ll put your application code inside an image that builds a container with Ruby, Bundler and all your dependencies inside it. For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile reference.
Next, open an editor and create a bootstrap Gemfile
which just loads Rails. This will be overwritten in a moment by rails new
. At this point you can use whatever rails version you want.
source 'https://rubygems.org'
gem 'rails', '~> 8.0'
Finally, docker-compose.yml
is where the magic happens. This file describes the services that comprise your app and the configuration needed to make the app run.
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
command: bash -c "./bin/dev"
tty: true
volumes:
- .:/rails
ports:
- "3000:3000"
Tip
You have to use tty:true so we can watch for changes on css/js files.
Build the project
With those files in place, you can now generate the Rails skeleton app using docker compose run:
$ docker compose run --no-deps web rails new . --name=my-project --force --css=tailwind --js=esbuild
When you run this command, Compose builds the image for the web
service using the Dockerfile.dev
file.
The --no-deps
tells Compose not to start linked services. Then it runs rails new
inside a new container, using that image.
Once it’s done, you should have generated a fresh app inside of your folder.
Tip
If you are running Docker on Linux, the files
rails new
created are owned by root, you might have to run chown to change that.$ sudo chown -R $USER:$USER .
Now that you’ve got a new Gemfile, you need to build the image again. (This, and changes to the Gemfile
or the Dockerfile, should be the only times you’ll need to rebuild.)
$ docker compose build
Running the project
Before you boot your app, let’s make sure we bind 0.0.0.0 to our server. Go to Procfile.dev
and add ‘-b 0.0.0.0’ at the end of the first line, it should look like this.
web: env RUBY_DEBUG_OPEN=true bin/rails server -b 0.0.0.0
js: yarn build --watch
css: yarn build:css --watch
And the last step before you spin your project app, let’s do some minor modifications on the Dockerfile.dev
file. Add these lines right after COPY ..
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
The final Dockerfile.dev should looke like this:
# syntax = docker/dockerfile:1
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.3.5
FROM registry.docker.com/library/ruby:$RUBY_VERSION-slim-bookworm
# Rails app lives here
WORKDIR /rails
# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git curl node-gyp sqlite3
# Install JavaScript dependencies
ARG NODE_VERSION=18.15.0
ARG YARN_VERSION=latest
ENV PATH=/usr/local/node/bin:$PATH
RUN curl -sL https://github.com/nodenv/node-build/archive/master.tar.gz | tar xz -C /tmp/ && \
/tmp/node-build-master/bin/node-build "${NODE_VERSION}" /usr/local/node && \
npm install -g yarn@$YARN_VERSION && \
rm -rf /tmp/node-build-master
# Run bundle install
COPY Gemfile Gemfile.lock ./
RUN bundle install
# Copy application code
COPY . .
# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Now run docker compose build
again and then finaly, docker compose up
.
View the Rails welcome page!
That’s it. Your app should now be running on port 3000 on your Docker daemon, go to http://localhost:3000
on a web browser to see the Rails Welcome.
Stop the application
To stop the application, run docker compose down in your project directory. You can use the same terminal window in which you started the database, or another one where you have access to a command prompt. This is a clean way to stop the application.
docker compose down
✔ Container my-project-web-1 Removed0.0s
✔ Network my-project_default Removed
Restart the application
To restart the application run docker compose up
in the project directory.
Rebuild the application
If you make changes to the Gemfile or the Compose file to try out some different
configurations, you need to rebuild. Some changes require only
docker compose up --build
, but a full rebuild requires a re-run of
docker compose run web bundle install
to sync changes in the Gemfile.lock
to
the host, followed by docker compose up --build
.
Here is an example of the first case, where a full rebuild is not necessary.
Suppose you simply want to change the exposed port on the local host from 3000
in our first example to 3001
. Make the change to the Compose file to expose
port 3000
on the container through a new port, 3001
, on the host, and save
the changes:
ports:
- "3001:3000"
Now, rebuild and restart the app with docker compose up --build
.
Inside the container, your app is running on the same port as before 3000
, but
the Rails Welcome is now available on http://localhost:3001
on your local
host.