Skip to content

Add sample rails + postgresql application#288

Open
kochalex wants to merge 1 commit intodocker:masterfrom
kochalex:sample-rails
Open

Add sample rails + postgresql application#288
kochalex wants to merge 1 commit intodocker:masterfrom
kochalex:sample-rails

Conversation

@kochalex
Copy link

Signed-off-by: Alex Koch alex@esparklearning.com

Signed-off-by: Alex Koch <alex@esparklearning.com>
@kochalex
Copy link
Author

Closes #287 and #57

Copy link
Member

@nicksieger nicksieger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks again for submitting. Will get one more set of 👀 and probably get this merged early next week.

Copy link
Collaborator

@glours glours left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you propose your sample as a Dev Environment, you need to convert your Dockerfile to use multistage and add Docker tooling during a dedicated target that will be used in your .docker/docker-compose.yaml

It should be interesting to use the seeds.rb file to populate the database and display them in the main page to demonstrate that the ruby application is linked to the database.

Also do we need the log and tmp directories? Same question for all the directories with only a .keep file inside?

Otherwise do we need the

Comment on lines +1 to +18
FROM ruby:3.1

WORKDIR /app

RUN useradd -m app && chown app:app /app

RUN apt-get update -qq && apt-get install -y --no-install-recommends postgresql-client

USER app

COPY --chown=app:app Gemfile* .
RUN bundle install

COPY --chown=app:app . .

EXPOSE 3000

CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you described that your sample should work as a Dev Environments, you need to configure multistage build to include all the docker bits and check that git is present in the ruby base image
Check other existing samples of this repo if you need examples

Comment on lines +4 to +5
context: app
command: bash -c "bundle exec rails db:migrate && bundle exec rails server -b 0.0.0.0"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should reference the build stage of the dev envs configuration to be sure to embed docker cli, compose ... inside your container

Suggested change
context: app
command: bash -c "bundle exec rails db:migrate && bundle exec rails server -b 0.0.0.0"
context: app
target: dev-envs
command: bash -c "bundle exec rails db:migrate && bundle exec rails server -b 0.0.0.0"

Comment on lines +1 to +7
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should populate the database to make the sample more realistic.

@kochalex
Copy link
Author

As you propose your sample as a Dev Environment, you need to convert your Dockerfile to use multistage and add Docker tooling during a dedicated target that will be used in your .docker/docker-compose.yaml

Thank you, I didn't understand that and will update.

It should be interesting to use the seeds.rb file to populate the database and display them in the main page to demonstrate that the ruby application is linked to the database.

I can do that if you'd like - but I was going for something as simple and as close to rails new as possible with this. There are no database models in the default generated app. As this PR is currently, the Rails application within does need the postgres database to boot and store some basic metadata. The only changes I made to the ./app directory after running rails new were adding a Dockerfile and making Gemfile.lock track the "aarch64-linux" and "x86_64-linux" platforms for binary ruby gems.

Also do we need the log and tmp directories? Same question for all the directories with only a .keep file inside?

Rails writes to log and tmp by default - it will exit if those do not exist, however they could be added via the Dockerfile instead if that would be cleaner. The other .keep files are default locations to put certain types of code, I'm not sure offhand if they are all required. This was all boilerplate from rails new - if you'd like to see them deleted, I can look into and do that.

@nicksieger
Copy link
Member

I'd say it's fine to keep the log/tmp artifacts of Rails generation. Those directories still get used during development.

@glours
Copy link
Collaborator

glours commented Sep 20, 2022

Ok for the log/tmp and all the directories created by default by Rails initialiser 👍
@kochalex The purpose of the samples in this repository is it help people to bootstrap/see how they can use Compose to configure multi services applications.
So having 2 services running side by side without any interactions together doesn't provide the value we expect for the community, most of the users will expected to learn how they can connect their Rails application to a database running inside an another container.

@mculp
Copy link

mculp commented Jan 4, 2023

Another note: Redis is configured in the Rails app, but not in the docker-compose file. This should probably be Rails+Postgres+Redis since I think it's the most common stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants