Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docker-compose to run seeder #55

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ RUN yarn install --check-files
# Precompile assets with esbuild and TailwindCSS
RUN bundle exec rake assets:precompile

# Run database migrations
# RUN rails db:create db:migrate

# Run database migrations
# RUN bundle exec rake db:migrate

# Precompile assets with esbuild and TailwindCSS
RUN yarn build

# Start the Rails server
CMD ["rails", "server", "-b", "0.0.0.0"]
# Change permissions of start.sh script to make it executable
RUN chmod +x ./bin/docker-entrypoint
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@
This is the code used for [https://rubyandrails.info](https://rubyandrails.info) website.


# Local Deployment with Docker (Tested on Linux Ubuntu 22.04)
`docker-compose up --build`
### Local Deployment with Docker (Tested on Linux Ubuntu 22.04)
#### Start dev environment
`docker-compose up -d --build`

#### Allow to drop && seed database when initializing the dev environment
export $RECREATE_DB=true

#### Access the shell in the app container
`docker-compose run --rm --entrypoint sh app`
20 changes: 20 additions & 0 deletions bin/docker-entrypoint
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Remove a potentially pre-existing server.pid for Rails.
rm -f /app/tmp/pids/server.pid

# Check if RECREATE_DB is set to true and RAILS_ENV is development or test
echo "ENV RECREATE_DB: ${RECREATE_DB}"
echo "RAILS_ENV ${RAILS_ENV}"

if test "${RECREATE_DB}" = "true" && ( test "${RAILS_ENV}" = "development" || test "${RAILS_ENV}" = "test" ); then
echo "Dropping && preparing DB"
bundle exec rake db:drop
bundle exec rake db:prepare
else
echo "Preparing DB"
bundle exec rake db:prepare
fi

# Start the Rails server
bundle exec rails server -b 0.0.0.0
22 changes: 13 additions & 9 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@
require 'faker'


20.times do |tag|
Tag.create(title: Faker::Lorem.word)
if Tag.none?
20.times do |tag|
Tag.create(title: Faker::Lorem.word)
end
end

100.times do |book|
Book.create!(title: Faker::Book.title[0..40],
subtitle: Faker::Book.title[0..20],
content: Faker::Lorem::paragraph,
cover: "rails-novice-to-ninja.jpg",
year: Faker::Number.between(from: 2000, to: 2022),
tag_ids: [] )
if Book.none?
100.times do |book|
Book.create!(title: Faker::Book.title[0..40],
subtitle: Faker::Book.title[0..20],
content: Faker::Lorem::paragraph,
cover: "rails-novice-to-ninja",
year: Faker::Number.between(from: 2000, to: 2022),
tag_ids: [] )
end
end
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ services:
- "3000:3000"
environment:
DATABASE_URL: postgres://postgres:password@db:5432/rubyandrails_info_development
RAILS_ENV: development
RECREATE_DB: ${RECREATE_DB}
volumes:
- .:/app
- node_modules:/app/node_modules
- bundle:/usr/local/bundle
command: ["rails", "server", "-b", "0.0.0.0"]
command: ["sh", "./bin/docker-entrypoint"]

volumes:
db_data:
Expand Down