OneBusAway / onebusaway-docker

Docker configuration for the OneBusAway Application Modules (https://github.com/OneBusAway/onebusaway-application-modules)
Apache License 2.0
18 stars 32 forks source link

feat: allow adjusting configurations through environment variables #73

Closed Altonhe closed 6 months ago

Altonhe commented 6 months ago

Before, I used a two-step build process to prevent unnecessary libraries from being included in the final runtime image. However, this approach meant that configuration changes could only be made during the build phase, requiring a rebuild for any modifications. To improve flexibility, I suggest allowing users to update the configuration through environment variables, instead of needing to rebuild everything locally.

In this pull request, I've incorporated Supervisor to oversee both the bootstrap.sh script and the tomcat server. Here's how it works:

  1. If we set the Docker command to CMD ['bootstrap.sh'], Docker executes the script as its first process (PID 1). When the script completes, the container stops running.
  2. We want the tomcat server to start only after bootstrap.sh has finished executing. As mentioned in issue #34, it's better for security reasons to run the container as a non-root user.
  3. Supervisor solves these problems by ensuring a sequential execution: it starts the tomcat server only after bootstrap.sh has completed, and it runs the server under a non-root user account(oba_user).
# ps -ef
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 19:19 ?        00:00:00 /usr/bin/python3 /usr/bin/supervisord -n
oba_user     8     1 24 19:19 ?        00:03:20 /opt/java/openjdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKe
root       177     0  0 19:21 pts/0    00:00:00 /bin/sh
root       270   177  0 19:33 pts/0    00:00:00 ps -ef

now, you can start the server by environment variables, instead of build args:

  oba_app:
    container_name: oba_app
    depends_on:
      - oba_database
    build:
      context: ./oba
    environment:
      - JDBC_URL=jdbc:mysql://oba_database:3306/oba_database
      - JDBC_USER=oba_user
      - JDBC_PASSWORD=oba_password
      - TEST_API_KEY=test # For test only, remove in production
      - VEHICLE_POSITIONS_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_VehiclePositions.pb
      - TRIP_UPDATES_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_TripUpdates.pb
      - ALERTS_URL=https://opendata.burlington.ca/gtfs-rt/GTFS_ServiceAlerts.pb
      - REFRESH_INTERVAL=30
      - AGENCY_ID=Burlington
      - GOOGLE_MAPS_API_KEY=<YOUR_KEY_HERE>
      - GOOGLE_MAPS_CLIENT_ID=<YOUR_CHANNEL_ID_HERE>
      - GOOGLE_MAPS_CHANNEL_ID=<YOUR_CLIENT_ID_HERE>
    volumes:
      # Share the host's `bundle` directory
      # with the filesystem of the OBA service.
      - ./bundle:/bundle
    ports:
      # Access the webapp on your host machine at a path like
      # http://localhost:8080/onebusaway-api-webapp/api/where/agency/${YOUR_AGENCY}.json?key=TEST
      - "8080:8080"
    # restart: always

For users who want to configure their data-source file manually, just set environment USER_CONFIGURED=1

Altonhe commented 6 months ago

this looks great! Can you add a small example of how to correctly use USER_CONFIGURED=1 to the README?

Sure.