Skip to content

Docker Setup for Local Development

tmpayton edited this page Jan 2, 2025 · 9 revisions

This guide will walk through the process of setting up the API locally using Docker. It will show how to configure the environment variables, provide commonly used Docker commands, configure the database, and run pytest and locust.

Project Prerequisites:

  1. Clone this repository

  2. Install Docker for personal use

  3. Install Brew

    • Use brew to download nvm: brew install nvm
    • Use nvm to install our current node version: nvm install node <node version> (version can be found in our package-lock.json file)
    • In the project directory install dependencies and build the project: npm install && npm run build

Environment Variables:

Set the following environment variables:

Required:

       export SQLA_CONN=<XXXX>
       export SQLA_SAMPLE_DB_CONN=postgresql://postgres:password@db:5432/cfdm_test
       export SQLA_DOCKER_DB_CONN=postgresql://postgres:password@db:5432/cfdm_test
       export SQLA_TEST_CONN=postgresql://postgres:password@db:5432/cfdm_unit_test

Optionally set environment variables for running redis/celery and locust locally:

       export AWS_ACCESS_KEY_ID=<XXXX>
       export AWS_SECRET_ACCESS_KEY=<XXXX>
       export AWS_PUBLIC_BUCKET=<XXXX>
       export AWS_DEFAULT_REGION=<XXXX>
       export FEC_DOWNLOAD_API_KEY=<XXXX>
       export FEC_WEB_API_KEY_PUBLIC=<XXXX>
       export SLACK_HOOK=<XXXX>

Running Docker:

  1. Build the image:

    • with cache (faster): docker-compose build
    • without cache: docker-compose build --no-cache
  2. Start Services:

    • Start database, flask-app, and elasticsearch (Recommended): docker-compose up openfec db elasticsearch
    • Start services in background: docker-compose up -d openfec db elasticsearch
    • Start all services: docker-compose up
    • Start a singular service (openfec, db, redis, celery-worker, celery-beat, or elasticsearch): docker-compose up <Service> -d
    • Stop all services: docker-compose down
    • Stop a singular service (oopenfec, db, redis, celery-worker, celery-beat, or elasticsearch): docker-compose down <Service>
    • Restart all services: docker-compose restart
  3. View local version of API at http://127.0.0.1:5000

Setting up Local Database and Running Flyway:

To populate local database:

  1. Ensure database and API are running: docker-compose up -d db openfec
  2. Drop database docker-compose exec -it db dropdb -U postgres cfdm_test
  3. Recreate database docker-compose exec -it db createdb -U postgres cfdm_test
  4. Run task: docker-compose exec openfec invoke create_sample_db
    • Or populate db using flyway: docker-compose exec openfec flyway migrate -configFiles=/app/data/flyway.toml

To use local database:

  1. remove SQLA_CONN: unset SQLA_CONN
  2. Stop services: docker-compose down
  3. Start services again: docker-compose up -d openfec db

Running Pytest

  1. Ensure services are running: docker-compose up -d openfec db elasticsearch
  2. Run pytest: docker-compose exec openfec pytest

Running Locust

  1. Ensure openfec is running: docker-compose up -d openfec
  2. Run locust:
    • Dev: docker-compose exec openfec locust --host=https://fec-dev-api.app.cloud.gov/v1/ --logfile=locustfile.log
    • Stage: docker-compose exec openfec locust --host=https://api-stage.open.fec.gov/v1/ --logfile=locustfile.log
    • Prod: docker-compose exec openfec locust --host=https://api.open.fec.gov/v1/ --logfile=locustfile.log
  3. Open localhost:8089 to run tests

Running Commands in Service

The above examples the command docker-compose exec <Service> <Command> to run commands in service containers. Alternately, commands can be run in the Exec tab of a service in docker desktop. For pytest could be run like this in the openfec container:

image