I am assuming that you already know about rails and also have setup docker on your system
Create sample app and change directory
rails new sample-app
cd sample-app
Create Dockerfile
FROM ruby:2.5
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /sampe-app
COPY Gemfile /sampe-app/Gemfile
COPY Gemfile.lock /sample-app/Gemfile.lock
RUN bundle install
COPY . /sampe-app
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
CMD ["rails", "server", "-b", "0.0.0.0"]
Create entrypoint.sh
#!/bin/bash
set -e
# remove potentiallly pre existing server.pid for rails
rm -f /sample-app/tmp/pids/server.pid
# execute the docker containers main process
exec "$@"
Create docker-compose.yml
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
Build project(start containers)
docker-compose up
Adjust permissions
sudo chown -R $USER:$USER .
Update config/database.yml
default: &default
adapter: postgresql
encoding: unicode
host: db
username: postgres
password: password
pool: 5
development:
<<: *default
database: sample_app_development
test:
<<: *default
database: sample_app_test
Create database
docker-compose run web rails db:create
References
https://guides.rubyonrails.org/getting_started.html
https://docs.docker.com/compose/rails/
Comments
Post a Comment