diff --git a/Dockerfile-dev b/Dockerfile-dev index e8c00d0..f2533f2 100644 --- a/Dockerfile-dev +++ b/Dockerfile-dev @@ -2,6 +2,10 @@ 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 @@ -9,8 +13,7 @@ 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"] diff --git a/Makefile b/Makefile index ba37f6b..850beeb 100644 --- a/Makefile +++ b/Makefile @@ -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..." @@ -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 diff --git a/README.md b/README.md index 0a7b27c..e95b7bf 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/dev b/bin/dev index 5f91c20..7e817f6 100755 --- a/bin/dev +++ b/bin/dev @@ -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 diff --git a/bin/docker-entrypoint b/bin/docker-entrypoint index 57567d6..e62a954 100755 --- a/bin/docker-entrypoint +++ b/bin/docker-entrypoint @@ -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 diff --git a/config/cable.yml b/config/cable.yml index 3897fb2..0a3061a 100644 --- a/config/cable.yml +++ b/config/cable.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index fb076fd..3f0b2ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: