Running rail app in docker

Step 1:

First, using Dockfile to Define the project

we need to create Dockefile, you can cop sample dockerfile from offical websie (This is sample Dockerfile copy from docker offical website)

FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

# Add a script to be executed every time the container starts.
COPY /usr/bin/
RUN chmod +x /usr/bin/

# Start the main process.
CMD ["rails", "server", "-b", ""]
second, create a Gemfile to just load rails.
source ''
gem 'rails', '~>5' # specify rails version in here

Third, create an empty Gemfile.lock To build Dockefile

touch Gemfile.lock

Fourth, create shell script, named

The purpose of entrypoint scipt is to fix a Rails-specific issue that prevents the server from restarting when a certain file pre-exists.

set -e

# Remove a potentially pre-existing for Rails.
rm -f /myapp/tmp/pids/

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"
Finaly,create docker-compose.yml file
version: '3'
    image: mysql
      - ./tmp/db:/var/lib/mysql/data
    build: .
    command: bash -c "rm -f tmp/pids/ && bundle exec rails s -p 3000 -b ''"
      - .:/myapp
      - "3000:3000"
      - db

Step 2: Build the project

Now it’s time to generate the rails skelton app using docker compose run command:

docker-compose run web rails new . --force --no-deps --database=postgresql

If you ever modify gem file. which you’ll do. you need run:

docker-compose build

Step 3: Connect the database

This is tricky part. when I followed docker website. I was stuck in this step. the offical mysql docker image which is somehow can’t initial. I got

[ERROR] --initialize specified but the data directory has files in it. Aborting.

After google for a while. I found a solution which is change db image source in docker-compose.yml file

from image: mysql to image: mariadb the detail discuss about this issue is here

Now we can boot the app with docker-compose up:

docker-compose up Finally, you need to create the database. In another terminal, run: docker-compose run web rake db:create

Written on June 12, 2019