For an even faster method, see the end of the article.
Approach
First, start a temporary Ruby container to create the project, and then build the necessary development image.
Create a New Project
To create a Rails project, start a temporary Ruby container:
$ docker run -it -v $(pwd):/app -w /app ruby:3.2 bash
Install the Rails gem inside the container:
/app# gem install rails
Then create the project:
/app# rails new myapp --database=postgresql --css=sass --skip-bundle
Here, we use the --skip-bundle
parameter because this is just a temporary container. We will run bundle in the development container later. Now, the temporary container has served its purpose. Exit the container by pressing ctrl-d
or typing exit
.
Add a Dockerfile
Add a Dockerfile in the project directory with the following content:
FROM ruby:3.2
# Reset this environment variable set by the Ruby image to its default value
ENV BUNDLE_APP_CONFIG=.bundle
# Uncomment this section if you need to install other dependencies
# RUN apt-get update && apt-get install -y --no-install-recommends \
# nodejs \
# npm \
# postgresql-client
WORKDIR /app
This is a minimal Rails development environment image. If needed, you can install other system dependencies using apt-get
. We don't need to build the image yet; we will build it later using the docker compose command.
Add docker-compose.yml
Add a docker-compose.yml
file in the project directory with the following content:
version: "3.9"
services:
web:
build: .
command: bin/rails server -b 0.0.0.0
volumes:
- .:/app
ports:
- 3000:3000
depends_on:
- postgres
postgres:
image: postgres:13
environment:
POSTGRES_PASSWORD: postgres
This defines the web
and postgres
services. The web
service will build an image based on the Dockerfile in the current directory, mount the current directory to the /app
directory inside the container, expose port 3000, and add a dependency on the postgres
service. The postgres
service will use the postgres
image and set the initial password via an environment variable.
Build the Image
Run the following command:
$ docker compose build
Docker Compose will read the configuration in docker-compose.yml
and build the respective images. Note: you need to re-run this command every time you modify the Dockerfile.
Enter the Command Line
Run the following command:
$ docker compose run web bash
This will start the web
service container and open a bash shell. In this shell, you can run the commands needed for local development, such as bundle install
, bin/rails g
, etc. All subsequent operations that need to be executed inside the container will be done through this shell.
Run Bundle
First, run the following command inside the container:
/app# bundle config set --local path vendor/bundle
This command sets the bundle installation directory to the vendor/bundle
directory under the project. This way, every time you update the Gemfile during development, you won't need to rebuild the image.
Then run bundle:
/app# bundle install
Note: remember to add vendor/bundle
to .gitignore
.
Prepare the Database
Before creating the database, modify the database settings of the Rails project. In the Docker Compose environment, PostgreSQL and the Rails process run in different containers, similar to different hosts, where the service name is their respective network name.
Modify database.yml
to add the following content in the development
and test
sections:
host: postgres
username: postgres
password: postgres
Then run the following command:
/app# bin/setup
This will create the respective databases.
Start the Web Service
After the preparation, it's time to start the web service. Open another terminal and run the following command:
$ docker compose up
Once the startup is complete, open http://localhost:3000
to see the Rails welcome page.
An Even Simpler Way
Download ServBay
ServBay is an all-in-one development environment management tool that supports multiple development languages and database components, including PHP, Node.js, MariaDB (MySQL), PostgreSQL, as well as NoSQL databases like Redis and Memcached.
To set up a Redis environment, simply download the Redis package in ServBay, and all configurations will be completed in just 3 minutes.
Top comments (0)