Skip to content

Commit

Permalink
Improve Docker setup
Browse files Browse the repository at this point in the history
  • Loading branch information
greyli committed Jun 12, 2024
1 parent 4576c95 commit bb956f6
Show file tree
Hide file tree
Showing 7 changed files with 453 additions and 445 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL=sqlite:////home/greybook/database/data.db
24 changes: 11 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@ FROM python:3.12-slim
RUN groupadd -r greybook && useradd -r -g greybook greybook

WORKDIR /home/greybook
RUN apt-get update
RUN apt-get install -y gcc g++

COPY requirements.txt requirements.txt
RUN python3 -m venv venv
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn
COPY pyproject.toml pdm.lock ./
RUN pip install -U pdm
ENV PDM_CHECK_UPDATE=false
RUN pdm install --check --prod --no-editable
ENV PATH="/home/greybook/.venv/bin:$PATH"

COPY greybook greybook
COPY migrations migrations
COPY logs logs
COPY uploads uploads
COPY app.py .

ENV FLASK_APP app
ENV FLASK_CONFIG production
COPY app.py docker-entrypoint.sh ./

RUN chown -R greybook:greybook .
USER greybook

ENV FLASK_APP app.py
ENV FLASK_CONFIG production
ENV GREYBOOK_LOGGING_PATH stream

EXPOSE 5000
RUN venv/bin/flask db upgrade
ENTRYPOINT ["venv/bin/gunicorn", "-b", ":5000", "--access-logfile", "-", "--error-logfile", "-", "app:app"]
ENTRYPOINT ["./docker-entrypoint.sh"]
24 changes: 12 additions & 12 deletions Dockerfile.multi-stage
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,36 @@ ARG base_image=python:3.12-slim
FROM ${base_image} AS build

WORKDIR /home/greybook
RUN apt-get update && apt-get install -y gcc g++

# install dependencies
COPY requirements.txt requirements.txt
RUN python3 -m venv venv
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn
COPY pyproject.toml pdm.lock ./
RUN pip install -U pdm
ENV PDM_CHECK_UPDATE=false
RUN pdm install --check --prod --no-editable

# stage 2: production
FROM ${base_image}

RUN groupadd -r greybook && useradd -r -g greybook greybook

WORKDIR /home/greybook

# copy the installed dependencies from the previous stage
COPY --from=build /home/greybook/venv/ venv/
COPY --from=build /home/greybook/.venv/ .venv/
ENV PATH="/home/greybook/.venv/bin:$PATH"

# copy the application source code from the previous stage
COPY greybook greybook
COPY migrations migrations
COPY logs logs
COPY uploads uploads
COPY app.py .

ENV FLASK_APP app
ENV FLASK_CONFIG production

RUN chown -R greybook:greybook .
USER greybook

ENV FLASK_APP app
ENV FLASK_CONFIG production
ENV GREYBOOK_LOGGING_PATH stream

EXPOSE 5000
RUN venv/bin/flask db upgrade
ENTRYPOINT ["venv/bin/gunicorn", "-b", ":5000", "--access-logfile", "-", "--error-logfile", "-", "app:app"]
ENTRYPOINT ["./docker-entrypoint.sh"]
16 changes: 16 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
greybook:
build:
context: .
dockerfile: Dockerfile
env_file:
- .env
ports:
- "8000:5000"
volumes:
- greybook-database:/home/greybook/database
- greybook-uploads:/home/greybook/uploads

volumes:
greybook-database:
greybook-uploads:
8 changes: 8 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

# run database migrations
flask db upgrade

# start Gunicorn
exec gunicorn --workers 4 --bind 0.0.0.0:5000 --access-logfile - --error-logfile - app:app
824 changes: 404 additions & 420 deletions pdm.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies = [
"flask-debugtoolbar>=0.13.1",
"python-dotenv>=0.21.1",
"flask-mailman>=0.3.0",
"gunicorn>=22.0.0",
]
requires-python = ">=3.8"
license = {text = "MIT"}
Expand Down

0 comments on commit bb956f6

Please sign in to comment.