Skip to content

Commit

Permalink
Dockerize Pet Rescue (#824)
Browse files Browse the repository at this point in the history
* Initial attempt at running in Docker. It works, but I think assets might be handled differently.

* Add chromium to image and precompile assets - I know this can slow things down, but it should solve the asset delivery issue I was having

* Use PG14

* Add ENV vars and healthcheck for db. Add SASS service.

* Update app name; add db port;

* Use service name postgres instead of db

* add docker instructions to readme
  • Loading branch information
kasugaijin authored Jun 26, 2024
1 parent 0e60a29 commit fb85ab0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 11 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@ The Pet Rescue app is derived from the [Baja Pet Rescue Dog Adoption Application

# 🚀 Getting Started

Let's get your machine setup to startup the application!
Let's get your machine setup to start up the application!

## Prerequisites
## Docker

Install a containerization app, such as Docker Desktop.

Clone the repo to your local machine and navigate to the directory and run:

`docker-compose build` to build the base image.

`docker-compose up` to start the containers. You can add the `-d` flag to run silently without logging.

`docker-compose run --rm app bin/setup` to set up and seed the database.

If you need to run migrations at all: `docker-compose run --rm app bin/rails db:migrate`

Visit `localhost:3000` in your browser.

## Local Installation

⚠️ We assume you already have ruby installed with your preferred version manager. This codebase supports [rbenv](
https://github.com/rbenv/rbenv) and [asdf](https://github.com/asdf-vm/asdf-ruby).
Expand Down
39 changes: 30 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
---
version: '3.8'
services:
postgres:
image: postgres:14
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: ${DATABASE_USERNAME}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
POSTGRES_DB: pet_rescue_development

POSTGRES_DB: petrescue_development
POSTGRES_USER: ${DATABASE_USERNAME:-postgres}
POSTGRES_PASSWORD: ${DATABASE_PASSWORD:-password}
ports:
- 5432

healthcheck:
test: ["CMD-SHELL", "pg_isready", "-U", "bwoa", "-d", "pet_rescue_development"]
test: ["CMD-SHELL", "pg_isready -U ${DATABASE_USERNAME:-postgres} -d petrescue_development"]
interval: 10s

app:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -b '0.0.0.0'"
volumes:
- .:/petrescue
ports:
- "3000:3000"
depends_on:
postgres:
condition: service_healthy
environment:
DATABASE_URL: postgres://${DATABASE_USERNAME:-postgres}:${DATABASE_PASSWORD:-password}@postgres:5432/petrescue_development
RAILS_ENV: development

sass:
build: .
command: bundle exec rails dartsass:watch
volumes:
- db:/var/lib/postgresql/data
- .:/petrescue
depends_on:
app:
condition: service_started

volumes:
db:
postgres-data:
37 changes: 37 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Use the official Ruby image as a base
FROM ruby:3.3.0-slim

# Install dependencies
RUN apt-get update -qq && apt-get install -y \
nodejs \
postgresql-client \
build-essential \
libpq-dev \
chromium-driver

# Set up working directory
WORKDIR /petrescue

# Copy Gemfile and Gemfile.lock before other files to leverage Docker's caching mechanism
COPY Gemfile /petrescue/Gemfile
COPY Gemfile.lock /petrescue/Gemfile.lock

# Install gems
RUN gem install bundler && bundle install

# Copy the rest of the application code
COPY . /petrescue

# Precompile assets (optional for development, but useful for production)
RUN bundle exec rails assets:precompile

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh

# Expose port 3000 to the Docker host
EXPOSE 3000

# Start the Rails server
ENTRYPOINT ["entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]
8 changes: 8 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

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

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

0 comments on commit fb85ab0

Please sign in to comment.