Skip to content

Commit

Permalink
add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
tmpayton committed Jan 2, 2025
1 parent 02b2fbd commit 1a6312d
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 7 deletions.
26 changes: 26 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
FROM python:3.10-slim

# Set a working directory inside the container
WORKDIR /app

RUN apt-get clean

RUN apt-get update \
-o Acquire::http::No-Cache=true \
-o Acquire::http::Pipeline-Depth=0 \
-o Acquire::BrokenProxy=true \
&& apt-get install -y --no-install-recommends \
-o Acquire::http::No-Cache=true \
-o Acquire::http::Pipeline-Depth=0 \
-o Acquire::BrokenProxy=true \
wget \
tar \
unzip \
gcc \
make \
libpq-dev \
git \
postgresql-server-dev-all \
&& rm -rf /var/lib/apt/lists/* # Clean up to reduce the image size

# Download Flyway CLI
RUN wget https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/11.0.0/flyway-commandline-11.0.0-linux-x64.tar.gz && \
tar -xvf flyway-commandline-11.0.0-linux-x64.tar.gz && \
mv flyway-11.0.0 /flyway && \
ln -s /flyway/flyway /usr/local/bin/flyway && \
rm flyway-commandline-11.0.0-linux-x64.tar.gz


# Copy application files
COPY . /app


# Install Python dependencies from requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install --no-cache-dir -r requirements-dev.txt

# Expose ports for flask and locust
EXPOSE 5000
EXPOSE 8089

# Add Python path environment variable
ENV PYTHONPATH "${PYTHONPATH}:/app"

CMD ["flask", "run"]
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ We are always trying to improve our documentation. If you have suggestions or ru
- See [Database migrations](https://github.com/fecgov/openFEC#database-migration) for more information on installing and configuring flyway
2. Set up your Node environment— learn how to do this with our [Javascript Ecosystem Guide](https://github.com/18F/dev-environment-standardization/blob/18f-pages/pages/languages/javascript.md).
2. Optionally, you can use [Docker Desktop](https://docs.docker.com/desktop/) to setup Python, Postgres, Elastic Search, and Flyway.
3. Set up your Python environment— learn how to do this with our [Python Ecosystem Guide](https://github.com/18F/dev-environment-standardization/blob/18f-pages/pages/languages/python.md).
3. Set up your Node environment— learn how to do this with our [Javascript Ecosystem Guide](https://github.com/18F/dev-environment-standardization/blob/18f-pages/pages/languages/javascript.md).
4. Clone this repository.
4. Set up your Python environment— learn how to do this with our [Python Ecosystem Guide](https://github.com/18F/dev-environment-standardization/blob/18f-pages/pages/languages/python.md).
5. Clone this repository.
### Project dependencies
Expand Down Expand Up @@ -338,6 +340,44 @@ To update the version-controlled test subset after rebuilding, run:
invoke dump postgresql://:@/cfdm_test data/subset.dump
```
## Docker Laptop Setup
To run the application locally using Docker:
1. Clone the repository
2. Follow the instructions above to install JavaScript dependencies
3. Set the required environment variables:
```
export SQLA_CONN=<your dev database connection string>
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
```
3. Build the image:
```
docker-compose build
```
4. Start all services:
```
docker-compose up -d openfec db elasticsearch
```
5. Create and populate your local development database:
```
docker-compose exec -it db createdb -U postgres cfdm_test
docker-compose exec openfec invoke create_sample_db
```
6. Visit your local version of the site http://localhost:5000
See the wiki for more information on configuring [Docker for local setup](https://github.com/fecgov/openFEC/wiki/Docker-Setup-for-Local-Development), running and tesing [Elastic Search](https://github.com/fecgov/openFEC/wiki/Docker-Setup-and-Test-Elasticsearch-Locally), [Redis, and Celery](https://github.com/fecgov/openFEC/wiki/Docker-Setup-and-Test-Redis-and-Celery-Locally) locally.
## Deployment (FEC team only)
### Deployment prerequisites
Expand Down
26 changes: 26 additions & 0 deletions data/flyway.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# More information on the parameters can be found here: https://documentation.red-gate.com/flyway/flyway-cli-and-api/configuration/parameters

[environments.local]
url = "jdbc:postgresql://db:5432/cfdm_test"
user = "postgres"
password = "password"
# jarDirs = ["path/to/java/migrations"]
# driver =
# schemas =
# connectRetries =
# connectRetriesInterval =
# initSql =
# jdbcProperties =
# resolvers =

[flyway]
environment = "local"
locations = ["filesystem:/app/data/migrations"]

# [environments.build]
# url = "jdbc:sqlite::memory:"
# user = "buildUser"
# password = "buildPassword"

# [flyway.check]
# buildEnvironment = "build"
95 changes: 95 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
services:
openfec:
platform: linux/amd64
build:
context: .
ports:
- "5000:5000"
- "8089:8089"
environment:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_PUBLIC_BUCKET=${AWS_PUBLIC_BUCKET}
- AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
- FEC_API_URL=http://localhost:5000
- FEC_CMS_URL=http://localhost:8000
- FEC_DOWNLOAD_API_KEY=${FEC_DOWNLOAD_API_KEY}
- FEC_WEB_API_KEY_PUBLIC=${FEC_WEB_API_KEY_PUBLIC}
- FLASK_APP=webservices.rest.py
- FLASK_DEBUG=1
- FLASK_RUN_HOST=0.0.0.0
- IS_USING_DOCKER=true
- SLACK_HOOK=${SLACK_HOOK}
- SQLA_CONN=${SQLA_CONN}
- SQLA_DOCKER_DB_CONN=${SQLA_DOCKER_DB_CONN}
- SQLA_SAMPLE_DB_CONN=${SQLA_SAMPLE_DB_CONN}
- SQLA_TEST_CONN=${SQLA_TEST_CONN}
volumes:
- .:/app

db:
image: postgres:13
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: localdb
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"

elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.4.0
platform: linux/amd64
environment:
- discovery.type=single-node
- http.cors.enabled=true
- http.cors.allow-origin=*
- http.cors.allow-headers=X-Requested-With, Content-Type, Content-Length, Authorization
- http.cors.allow-methods=OPTIONS, HEAD, GET, POST, PUT, DELETE
- discovery.type=single-node
volumes:
- esdata:/usr/share/elasticsearch/data
ports:
- "9200:9200"

redis:
image: redis:6.2
command: redis-server --save 60 1 --loglevel warning
ports:
- "6379:6379"

celery-worker:
build:
context: .
command: celery -A webservices.tasks worker --loglevel=info
environment:
- AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- AWS_PUBLIC_BUCKET=${AWS_PUBLIC_BUCKET}
- AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}
- CELERY_BROKER_URL=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0
- IS_USING_DOCKER=true
- SQLA_DOCKER_DB_CONN=${SQLA_DOCKER_DB_CONN}
depends_on:
- redis
volumes:
- .:/app

celery-beat:
build:
context: .
command: celery -A webservices.tasks beat --loglevel=info
environment:
- CELERY_BROKER_URL=redis://redis:6379/0
- CELERY_RESULT_BACKEND=redis://redis:6379/0
- IS_USING_DOCKER=true
depends_on:
- redis
volumes:
- .:/app

volumes:
pgdata:
esdata:
2 changes: 1 addition & 1 deletion webservices/legal_docs/advisory_opinions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
import logging
import re
from webservices.rest import db
from webservices.common.models import db
from webservices.utils import (
create_es_client,
DateTimeEncoder,
Expand Down
2 changes: 1 addition & 1 deletion webservices/legal_docs/archived_murs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from elasticsearch_dsl import Search
import logging
import re
from webservices.rest import db
from webservices.common.models import db
from webservices.utils import (
create_es_client,
create_eregs_link,
Expand Down
2 changes: 1 addition & 1 deletion webservices/legal_docs/current_cases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import re
from webservices.rest import db
from webservices.common.models import db
from webservices.utils import (
extend,
create_es_client,
Expand Down
6 changes: 6 additions & 0 deletions webservices/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@

def sqla_conn_string():
sqla_conn_string = env.get_credential('SQLA_CONN')

if not sqla_conn_string and env.get_credential("IS_USING_DOCKER"):
print("Environment variable SQLA_CONN is empty; trying SQLA_DOCKER_DB_CONN")
sqla_conn_string = env.get_credential('SQLA_DOCKER_DB_CONN')

Check warning on line 73 in webservices/rest.py

View check run for this annotation

Codecov / codecov/patch

webservices/rest.py#L72-L73

Added lines #L72 - L73 were not covered by tests

if not sqla_conn_string:
print("Environment variable SQLA_CONN is empty; running against " + "local `cfdm_test`")
sqla_conn_string = 'postgresql://:@/cfdm_test'

return sqla_conn_string


Expand Down
2 changes: 2 additions & 0 deletions webservices/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def redis_url():
redis_url = redis_env.credentials.get("uri")

return redis_url
elif env.get_credential("IS_USING_DOCKER"):
return "redis://redis:6379/0"

Check warning on line 85 in webservices/tasks/__init__.py

View check run for this annotation

Codecov / codecov/patch

webservices/tasks/__init__.py#L85

Added line #L85 was not covered by tests

return env.get_credential("FEC_REDIS_URL", "redis://localhost:6379/0")

Expand Down
5 changes: 4 additions & 1 deletion webservices/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,10 @@ def create_es_client():
)
else:
# create local elasticsearch client
url = "http://localhost:9200"
if env.get_credential("IS_USING_DOCKER"):
url = "http://elasticsearch:9200"
else:
url = "http://localhost:9200"
es_client = Elasticsearch(
url,
timeout=30,
Expand Down

0 comments on commit 1a6312d

Please sign in to comment.