Skip to content

Commit

Permalink
Merge aba13d3 into 535ffe6
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Mar 18, 2022
2 parents 535ffe6 + aba13d3 commit 30e21ec
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Expand Up @@ -17,8 +17,8 @@ RUN sa-update

WORKDIR /backend

COPY pyproject.toml /backend
COPY poetry.lock /backend
COPY pyproject.toml poetry.lock /backend/
COPY gunicorn.conf.py /backend
COPY app /backend/app

RUN pip3 install poetry && poetry config virtualenvs.create false && poetry install --no-dev
Expand Down
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -25,6 +25,23 @@ docker run -i -d -p 8000:8000 eml_analyzer

The application is running at: http://localhost:8000/ in your browser.

### Docker Compose

```bash
git clone https://github.com/ninoseki/eml_analyzer.git
cd eml_analyzer
docker-compose up
```

### Docker vs. Docker compose

- Docker:
- Run [Uvicorn](https://www.uvicorn.org/) and [SpamAssassin](https://spamassassin.apache.org/) in the same container. (The processes are managed by [Circus](https://circus.readthedocs.io/en/latest/))
- Docker Compose:
- Run [Gunicorn](https://gunicorn.org/) and SpamAssassin in each container.

Thus Docker Compose is suitable for the production use.

### Heroku

Alternatively, you can deploy the application on Heroku.
Expand Down
30 changes: 30 additions & 0 deletions app.Dockerfile
@@ -0,0 +1,30 @@
# build env
FROM node:16-alpine as build

COPY ./frontend /frontend
WORKDIR /frontend
RUN npm install && npm run build && rm -rf node_modules

# prod env
FROM python:3.9-slim-buster

RUN apt-get update \
&& apt-get install -y libmagic-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /backend

COPY pyproject.toml poetry.lock /backend/
COPY gunicorn.conf.py /backend
COPY app /backend/app

RUN pip3 install poetry && poetry config virtualenvs.create false && poetry install --no-dev

COPY --from=build /frontend /backend/frontend

ENV PORT 8000

EXPOSE $PORT

CMD gunicorn -k uvicorn.workers.UvicornWorker app:app
25 changes: 25 additions & 0 deletions docker-compose.yml
@@ -0,0 +1,25 @@
version: "3.7"
services:
spamassassin:
container_name: eml_analyzer_spamassassin
image: instantlinux/spamassassin:3.4.6-1
ports:
- ${PORT_SPAMASSASSIN:-783}:783
restart: always

eml_analyzer:
container_name: eml_analyzer_app
build:
context: ./
dockerfile: app.Dockerfile
ports:
- ${PORT:-8000}:8000
environment:
- VIRUSTOTAL_API_KEY=${VIRUSTOTAL_API_KEY}
- URLSCAN_API_KEY=${URLSCAN_API_KEY}
- INQUEST_API_KEY=${INQUEST_API_KEY}
- SPAMASSASSIN_HOST=spamassassin
- SPAMASSASSIN_PORT=${PORT_SPAMASSASSIN:-783}
restart: always
depends_on:
- spamassassin
43 changes: 43 additions & 0 deletions gunicorn.conf.py
@@ -0,0 +1,43 @@
import multiprocessing
import os

workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
max_workers_str = os.getenv("MAX_WORKERS")
use_max_workers = None
if max_workers_str:
use_max_workers = int(max_workers_str)
web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)

host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", "8000")
use_loglevel = os.getenv("LOG_LEVEL", "info")
use_bind = f"{host}:{port}"

accesslog_var = os.getenv("ACCESS_LOG", "-")
use_accesslog = accesslog_var or None
errorlog_var = os.getenv("ERROR_LOG", "-")
use_errorlog = errorlog_var or None
graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120")
timeout_str = os.getenv("TIMEOUT", "120")
keepalive_str = os.getenv("KEEP_ALIVE", "5")

cores = multiprocessing.cpu_count()
workers_per_core = float(workers_per_core_str)
default_web_concurrency = workers_per_core * cores
if web_concurrency_str:
web_concurrency = int(web_concurrency_str)
assert web_concurrency > 0
else:
web_concurrency = max(int(default_web_concurrency), 2)
if use_max_workers:
web_concurrency = min(web_concurrency, use_max_workers)

# Gunicorn config variables
loglevel = use_loglevel
workers = web_concurrency
bind = use_bind
errorlog = use_errorlog
accesslog = use_accesslog
graceful_timeout = int(graceful_timeout_str)
timeout = int(timeout_str)
keepalive = int(keepalive_str)

0 comments on commit 30e21ec

Please sign in to comment.