Skip to content

Conversation

@AntoSalazar
Copy link

@AntoSalazar AntoSalazar commented Apr 29, 2025

feat: Dockerize application with automated data updates

This PR introduces robust Docker support for local development of the API-Codigos-Postales project, enabling developers to seamlessly set up and run the application, its PostgreSQL database, and automated data updates using Docker Compose.
All previous issues with database seeding and dependency loading have been resolved.
Summary of Changes

Comprehensive Docker Setup:
Added a Dockerfile for the Ruby app and a docker-compose.yml to orchestrate the app (web), PostgreSQL (db), and the new cron_worker service.

Resolved Dependency Loading (csv):
Added gem 'csv' to the Gemfile and ensured correct bundle install during build, fixing the LoadError that previously affected data seeding.

Streamlined Database Connection:
Updated environment configuration to use DATABASE_URL, resolving PG::ConnectionBad issues and simplifying setup.

Automated Daily Data Updates:
Introduced a cron_worker service that runs a cron job daily at 3:00 AM UTC to run rake sepomex:update and keep postal code data current.

Updated README.md:
Includes detailed Docker setup instructions for building, running, migrating, seeding, and verifying the cron job.

How to Use (Quick Summary)

(See the updated README.md under "Running with Docker Compose" for full details.)

Ensure Docker & Docker Compose are installed.

Clone the repo.

Build images:
docker compose build

Start DB:
docker compose up -d db

Wait for DB to be healthy:
docker compose ps

Run migrations:
docker compose run --rm web bundle exec rake db:migrate

Seed DB (initial data load):
docker compose run --rm web bundle exec rake sepomex:update

Start the app and cron job:
docker compose up -d web cron_worker

Access the API at:
http://localhost:3001
Use auth headers from docker-compose.yml, e.g., X-API-TOKEN: very-secret-local-token

Cron Job Verification

The daily update will run automatically at 3:00 AM UTC.
You can:

View logs:
docker compose logs -f cron_worker

Or manually trigger the update:
docker compose exec cron_worker /app/update_sepomex.sh

This Docker setup provides a reproducible, containerized, and self-updating environment for the API-Codigos-Postales project — ideal for both development and onboarding.

Includes Dockerfile, Docker Compose configuration, and a
workaround script (run_sepomex_update.rb) for database seeding
due to a persistent 'csv' load error under 'bundle exec'.

Updates README with setup instructions and documents the
seeding issue and workaround.
@acrogenesis acrogenesis requested a review from Copilot April 29, 2025 17:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds Docker support for local development of the API-Codigos-Postales project and includes a workaround for a CSV load issue during database seeding.

  • Added a Dockerfile and docker-compose.yml to orchestrate the web and db services.
  • Created a workaround script (run_sepomex_update.rb) and updated documentation in README.md.
  • Made adjustments in the Rakefile and Gemfile to support the changes.

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
run_sepomex_update.rb Introduces a new script to download, extract, and seed postal codes.
docker-compose.yml Defines development containers with proper service dependencies and healthchecks.
Readme.md Updates setup instructions for local Docker-based development.
Rakefile Adjusts require order for CSV to work around the loading issue.
Gemfile Minor formatting changes.
Files not reviewed (1)
  • Dockerfile: Language not supported

response_post = Net::HTTP.post_form(uri, params)

puts 'Writing Zip'
File.open('latest.zip', 'w') do |file|
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using binary write mode ('wb') when opening 'latest.zip' to correctly handle binary data from the downloaded zip file.

Suggested change
File.open('latest.zip', 'w') do |file|
File.open('latest.zip', 'wb') do |file|

Copilot uses AI. Check for mistakes.
- app-network
healthcheck:
# Ensure username matches POSTGRES_USER above
test: ["CMD-SHELL", "pg_isready -U your_postgres_user -d api-codigos-postales"]
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The healthcheck command uses 'your_postgres_user' whereas the service environment sets POSTGRES_USER to 'postgres'. Please update to use the correct username.

Suggested change
test: ["CMD-SHELL", "pg_isready -U your_postgres_user -d api-codigos-postales"]
test: ["CMD-SHELL", "pg_isready -U postgres -d api-codigos-postales"]

Copilot uses AI. Check for mistakes.
Rakefile Outdated
@@ -1,6 +1,10 @@
$LOAD_PATH.unshift('/usr/local/lib/ruby/3.4.0') unless $LOAD_PATH.include?('/usr/local/lib/ruby/3.4.0')
Copy link

Copilot AI Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding the Ruby version in the $LOAD_PATH may lead to compatibility issues; consider using a dynamic approach based on RUBY_VERSION.

Suggested change
$LOAD_PATH.unshift('/usr/local/lib/ruby/3.4.0') unless $LOAD_PATH.include?('/usr/local/lib/ruby/3.4.0')
$LOAD_PATH.unshift("/usr/local/lib/ruby/#{RUBY_VERSION}") unless $LOAD_PATH.include?("/usr/local/lib/ruby/#{RUBY_VERSION}")

Copilot uses AI. Check for mistakes.
This commit introduces comprehensive Docker support for the API-Codigos-Postales application, enabling easy setup and execution using Docker Compose.

Key changes and problem resolutions include:

- **Initial Dockerfile and docker-compose.yml setup:** Configured the application and PostgreSQL database services for containerization.
- **Resolved Ruby version mismatch:** Corrected the Ruby version in the Dockerfile to `3.4.2` to align with the `Gemfile` specification, fixing build failures.
- **Fixed PostgreSQL password authentication:** Addressed persistent `PG::ConnectionBad` errors by ensuring the database volume was properly reset and recreated with the correct password, resolving connection issues.
- **Addressed `LoadError: cannot load such file -- csv`:** Explicitly added `gem 'csv'` to the `Gemfile` to ensure the `csv` library is correctly bundled and available within the Docker environment, resolving the original data seeding failure.
- **Updated `Readme.md`:** Added detailed instructions for building, running, migrating, and seeding the database using `docker compose`, ensuring clarity and ease of use for new developers.
This commit introduces a daily cron job to automatically update the postal code data using the `rake sepomex:update` task.

- **Added `cron` to Docker image:** Modified `Dockerfile` to install `cron` and copy necessary scripts and configuration.
- **Created `update_sepomex.sh`:** A shell script to execute the `rake sepomex:update` command within the container.
- **Configured cron schedule:** Created `cron/sepomex_update_cron` to schedule the `update_sepomex.sh` script to run daily at 3:00 AM.
- **Introduced `cron_worker` service:** Added a new service to `docker-compose.yml` to run the cron daemon, ensuring the scheduled task executes within the Docker environment.
- **Updated `Readme.md`:** Documented the cron job setup, including verification and testing instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant