Skip to content
Open
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
9 changes: 6 additions & 3 deletions Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ FROM ruby:3.4

WORKDIR /usr/src/app

RUN apt-get update -qq && \
apt-get install --no-install-recommends -y build-essential git pkg-config libpq-dev && \
rm -rf /var/lib/apt/lists/*

COPY Gemfile* .
RUN bundle install

COPY . .

# Expose the port the app runs on
EXPOSE 3000
# Set the environment variable for Rails
ENV RAILS_ENV=development

# To start the app with just this file, uncomment the next line
# CMD ["bin/rails", "server", "-b", "0.0.0.0", "-p", "3000"]
ENTRYPOINT ["/usr/src/app/bin/docker-entrypoint"]
CMD ["./bin/rails", "server", "--binding=0.0.0.0", "--port=3000"]
30 changes: 19 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# On linux
# - ensure valkey is running before starting the app
# - maybe shut it down when stopping
ifeq ($(CONTAINERIZED),1)
up: up-container

down: down-container

tail: tail-container
else
up:
@if [ "$(shell systemctl is-active valkey)" != "active" ]; then \
echo "valkey is not running. Starting valkey..."; \
systemctl start valkey; \
else \
echo "valkey is already running."; \
fi
@echo "Starting the application..."
bundle exec rails server --binding=0.0.0.0
@bin/dev

down:
@echo "Stopping the application..."
Expand All @@ -19,4 +15,16 @@ down:
tail:
@echo "Tailing the application logs..."
tail -f log/*.log
endif

up-container:
@docker compose up --build

down-container:
@echo "Stopping the application..."
@docker compose down

tail-container:
@echo "Tailing the application logs..."
@docker compose logs -f web

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Developer Notes

- until containerized, ensure Valkey or Redis server is running
- run the whole stack with `make up CONTAINERIZED=1` (or `make up-container` / `docker compose up --build`)
- local `make up` / `bin/dev` requires Valkey to already be listening on the configured Redis port

# README

Expand Down
25 changes: 25 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
#!/usr/bin/env ruby
require "socket"
require "uri"

def redis_endpoint
uri = URI.parse(ENV.fetch("REDIS_URL", "redis://127.0.0.1:6379/1"))
[uri.host, uri.port]
rescue URI::InvalidURIError
["127.0.0.1", 6379]
end

def port_open?(host, port, timeout: 1)
Socket.tcp(host, port, connect_timeout: timeout) { true }
rescue StandardError
false
end

if ENV["CONTAINERIZED"] != "1"
host, port = redis_endpoint

unless port_open?(host, port)
warn "Valkey is not listening on #{host}:#{port}. Start it first, or run make up CONTAINERIZED=1."
exit 1
end
end

exec "./bin/rails", "server", *ARGV
5 changes: 3 additions & 2 deletions bin/docker-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ if [ -z "${LD_PRELOAD+x}" ]; then
export LD_PRELOAD
fi

# If running the rails server then create or migrate existing database
if [ "${@: -2:1}" == "./bin/rails" ] && [ "${@: -1:1}" == "server" ]; then
# If running the rails server then create or migrate existing database.
# Allow extra server flags after the command.
if [ "${1:-}" = "./bin/rails" ] && [ "${2:-}" = "server" ]; then
./bin/rails db:prepare
fi

Expand Down
2 changes: 1 addition & 1 deletion config/cable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# to make the web console appear.
development:
adapter: redis
url: redis://localhost:6379/1
url: <%= ENV.fetch("REDIS_URL", "redis://localhost:6379/1") %>

test:
adapter: test
Expand Down
55 changes: 36 additions & 19 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,45 @@ services:
build:
context: ./
dockerfile: Dockerfile-dev
# depends_on:
# - db
command: ["./bin/rails", "server", "--binding=0.0.0.0", "--port=3000"]
depends_on:
db:
condition: service_healthy
redis:
condition: service_healthy
ports:
- 3000:3000
volumes:
- .:/usr/src/app
environment:
- RAILS_ENV=production
command: rails server --binding=0.0.0.0
RAILS_ENV: development
DB_HOST: db
REDIS_URL: redis://redis:6379/1

# Database for developer setup is sqlite3
# This is a try at configuring for postgresql
# db:
# container_name: kbc-db
# environment:
# - POSTGRES_PASSWORD=rootpass
# image: postgres:14.17
# networks:
# - kbc-net
# ports:
# -
# volumes:
# - ${PWD}/storage/postgresql.conf:/etc/postgresql/postgresql.conf
# - ./storage/db_data:/var/lib/postgresql/data
# command: postgres -c 'config_file=/etc/postgresql/postgresql.conf'
db:
image: postgres:16-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: kbc_development
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres -d kbc_development"]
interval: 5s
timeout: 5s
retries: 10
volumes:
- postgres-data:/var/lib/postgresql/data

redis:
image: redis:7-alpine
healthcheck:
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
interval: 5s
timeout: 5s
retries: 10
volumes:
- redis-data:/data

volumes:
postgres-data:
redis-data: