A quickstart to dockerizing your existing Rails-Postgresql application with Docker compose Dockerfiles — Define the project Create in the app’s root directory, the project is defined by adding the dependencies to the Dockerfile Dockerfile. FROM ruby:2.5.3 RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs RUN mkdir /myapp WORKDIR /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 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 docker-compose.yml version: '3'services:db:image: postgresvolumes: ./tmp/db:/var/lib/postgresql/dataweb:build: .command: bundle exec rails s -p 3000 -b '0.0.0.0'volumes: .:/myappports: "3000:3000"depends_on: db Database.yml — Configure DB Now, Change the host to ‘db’ in your file to point to the docker service database.yml default: &default adapter: postgresql encoding: unicode host: db username: postgres password: pool: 5 development: <<: *defaultdatabase: myapp_development The following tasks can be completed be completed by running the respective commands Build Docker Image docker-compose build DB — Create and Migrate docker-compose run web rake db:create db:migrate Accessing Rails Console docker-compose exec web rails console Start application docker-compose up You should now be able to browse to and see your application running. http://localhost:3000
Share Your Thoughts