Skip to content

Commit

Permalink
Merge pull request #155 from rubyforgood/dev-docker-compose
Browse files Browse the repository at this point in the history
Basic developer tooling for running the app in Docker
  • Loading branch information
abachman authored Jun 1, 2024
2 parents 0598a8c + 5de397e commit 25363da
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 7 deletions.
25 changes: 25 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.2.3
FROM ruby:$RUBY_VERSION-slim as base

# Rails app lives here
WORKDIR /rails

# Install packages needed to build gems
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y \
build-essential libpq-dev curl \
nodejs npm
RUN npm install -g yarn

COPY Gemfile Gemfile.lock ./
RUN bundle install

COPY package.json yarn.lock ./
RUN yarn

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
CMD ["./bin/rails", "console"]
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,37 @@

* A ruby version manager such as [rvm](https://rvm.io/rvm/install) or [rbenv](https://formulae.brew.sh/formula/rbenv)
* Ruby 3.2.3 (Installed via ruby manager ^)
* [Rails 7](https://guides.rubyonrails.org/)
* [PostgreSQL](https://www.postgresql.org/)
* [bun](https://bun.sh/docs/installation)
* [PostgreSQL](https://www.postgresql.org/), if you're not using Docker.
* [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) and yarn (`npm install -g yarn`)

# Installation

## Mac Users
## With Docker

Build and start the application with `docker compose up`. Once the application has successfully started, you should be able to visit it at http://localhost:3000/

Run commands in docker with the `bin/dc` helper script on macos or Linux.

```console
$ bin/dc rails db:setup
$ bin/dc rails test
```

Or by prefacing `rails` commands with `docker compose run stocks`.

```console
$ docker compose run stocks rails db:setup
$ docker compose run stocks rails test
```

## Mac & Linux Users

* Run setup: `bin/setup`
* Run the Rails server locally: `foreman start -f Procfile.dev`
* Run the Rails server locally: `bin/dev`

## Windows & Linux users
## Windows

It is **strongly** recommend to use Docker.
It is **strongly** recommend to use Docker. See instructions above.

## URL

Expand Down
17 changes: 17 additions & 0 deletions bin/dc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# if first argument is help or -h or --help, show help
if [ "$1" = "help" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 [command]"
echo " command: command to run in the stocks container"
echo
echo "Examples:"
echo " bin/dc rails db:migrate # run migrations"
echo " bin/dc rails test # run tests"
echo " bin/dc rails console # start Rails console"
echo " bin/dc rails generate model Boop name:string # generate a Rails model"
exit 0
fi

echo "\033[2m> docker compose run stocks $@\033[0m"
docker compose run --rm --no-deps stocks $@
78 changes: 78 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
version: "3.9"
services:
redis:
image: redis:7.0
ports:
- 6379
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
volumes:
- redis:/data

db:
image: postgres:15
restart: always
environment:
PGUSER: sif
POSTGRES_USER: sif
POSTGRES_PASSWORD: password
POSTGRES_DB: sif
ports:
- 5432
healthcheck:
test: ["CMD-SHELL", "pg_isready", "--database", "stocks_in_the_future_development" ]
interval: 10s
volumes:
- postgres:/var/lib/postgresql/data

stocks:
build:
dockerfile: Dockerfile.dev
context: .
image: stocks:dev
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:${APP_PORT:-3000}"]
command: "bin/docker-entrypoint bin/rails server -b 0.0.0.0 -p ${APP_PORT:-3000}"
entrypoint: bin/docker-entrypoint
ports:
- "${APP_PORT:-3000}:${APP_PORT:-3000}"
volumes:
- .:/rails
- bundler:/usr/local/bundle
- node_modules:/rails/node_modules
environment:
RAILS_ENV: development
DATABASE_URL: postgresql://sif:password@db/
BROWSERSLIST_IGNORE_OLD_DATA: true

stocks-js:
image: node:lts-alpine
working_dir: /rails
tty: true
command: sh -c "yarn install && yarn build --watch"
volumes:
- .:/rails
- node_modules:/rails/node_modules

stocks-css:
image: node:lts-alpine
environment:
BROWSERSLIST_IGNORE_OLD_DATA: true
working_dir: /rails
tty: true
command: sh -c "yarn install && yarn build:css --watch=forever"
volumes:
- .:/rails
- node_modules:/rails/node_modules

volumes:
redis:
postgres:
bundler:
node_modules:

0 comments on commit 25363da

Please sign in to comment.