Setting up Rails 6 with PostgreSQL & Webpack on Docker

Rails 6 comes with a lot of new features including Webpack.

Guillaume Occuly
4 min readFeb 13, 2020

Chapter 1: Dockerfile

I assume you already have a Rails project created with PostgreSQL.

Now we will create a Dockerfile file in the root project.

# Dockerfile# Use ruby image to build our own image
FROM ruby:2.7
# We specify everything will happen within the /app folder inside the container
WORKDIR /app
# We copy these files from our current application to the /app container
COPY Gemfile Gemfile.lock ./
# We install all the dependencies
RUN bundle install
# We copy all the files from our current application to the /app container
COPY . .
# We expose the port
EXPOSE 3000
# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

For more information, you can check: https://hub.docker.com/_/ruby

On your terminal, run :

$ docker build -t docker_on_rails .

After many minutes, your image is finally built!

Now run your container:

$ docker run -p 3000:3000 docker_on_rails
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
sh: 1: yarn: not found
========================================
Your Yarn packages are out of date!
Please run `yarn install --check-files` to update.
========================================
To disable this check, please change `check_yarn_integrity`
to `false` in your webpacker config file (config/webpacker.yml).
Exiting

As we see, there is an error including Webpack.

We can disable the check by editing the config/webpacker.yml file but this is not recommended.

We will instead edit the config/environment/development.rb file by adding config.webpacker.check_yarn_integrity = false .

Add this line on config/environment/development.rb

Source: https://github.com/rails/webpacker/issues/1568#issuecomment-459995382

Because we have changed a file in the project, we need to rebuild the image before running again the container:

$ docker build -t docker_on_rails .
$ docker run -p 3000:3000 docker_on_rails
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
/usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/bundle/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.1 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

We do not have the yarn error anymore! Check on your browser: http://localhost:3000/

Our Rails application can’t connect to PostgreSQL

Chapter 2: docker-compose.yml

On Docker, your Rails project is not linked to PostgreSQL yet. We need to create a docker-compose.yml file at the root of the project.

# docker-compose.yml
version: '3.0'
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
volumes:
- .:/app
ports:
- "3000:3000"
depends_on:
- db
environment:
PG_PASSWORD: password

POSTGRES_PASSWORD and PG_PASSWORD must have the same value.

For more information, you can check: https://docs.docker.com/compose/rails/

Edit your config/database.yml file and add host and username lines :

# config/database.ymldefault: &default
adapter: postgresql
encoding: unicode
host: db # From docker-compose service's name
username: postgres
password: <%= ENV['PG_PASSWORD'] %> # From web's service environment
...

After that, you can run again docker-compose up and check your browser http://localhost:3000

This time, the database is not created inside your container. Create it by running docker-compose run web rails db:setup .

$ docker-compose run web rails db:setupStarting rails_on_docker_db_1 ... done
Created database 'rails_on_docker_development'
Created database 'rails_on_docker_test'

Finally, we run docker-compose upto run the server one last time.

When you see the server is up and running, check on your browser http://localhost:3000.

We did it! Your Rails application is fully dockerized!

You can now share your dockerized project to everyone and they just need to have Docker installed on their machine and run docker-compose up .

You can check this repository: https://github.com/GuillaumeOcculy/rails_on_docker

--

--