A quickstart to dockerizing your existing Rails-Postgresql application with Docker compose
Dockerfiles — Define the project
Create Dockerfile in the app’s root directory, the project is defined by adding the dependencies to the Dockerfile.
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Make sure add the specific version of ruby that your application uses
Docker-compose — Build the project
Now, create docker-compose.yml file in the root directory. This file does the magic of building your app from the current directory and running the pre-built postgresql image with the web app
command: bundle exec rails s -p 3000 -b '0.0.0.0'
Database.yml — Configure DB
Now, Change the host to ‘db’ in your database.yml file to point to the docker service
The following tasks can be completed be completed by running the respective commands
Build Docker Image
DB — Create and Migrate
docker-compose run web rake db:create db:migrate
Accessing Rails Console
docker-compose exec web rails console
You should now be able to browse to http://localhost:3000 and see your application running.