-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Containerize Frontend & Backend #16
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
build | ||
.dockerignore | ||
Dockerfile | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,5 @@ venv.bak/ | |
# mypy | ||
.mypy_cache/ | ||
*.db | ||
|
||
docker-compose.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
ARG REACT_APP_NAME=frontend | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'd be better of splitting this into two dockerfiles and putting them into their respective directories (frontend and backend) |
||
ARG FLASK_APP_NAME=backend | ||
ARG REACT_APP_PATH=/opt/$REACT_APP_NAME | ||
ARG FLASK_APP_PATH=/opt/$FLASK_APP_NAME | ||
ARG PYTHON_VERSION=3.10.0-alpine | ||
ARG POETRY_VERSION=1.1.13 | ||
ARG NGINX_VERSION=1.23.0-alpine | ||
|
||
########## STAGE 1: STAGING ########## | ||
FROM node:18-alpine AS node-staging | ||
ARG REACT_APP_NAME | ||
ARG REACT_APP_PATH | ||
|
||
WORKDIR $REACT_APP_PATH | ||
COPY ./frontend ./ | ||
|
||
FROM python:3.10 as python-staging | ||
|
||
ARG FLASK_APP_NAME | ||
ARG FLASK_APP_PATH | ||
|
||
ENV \ | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
PYTHONUNBUFFERED=1 \ | ||
PYTHONFAULTHANDLER=1 | ||
ENV \ | ||
POETRY_VERSION=$POETRY_VERSION \ | ||
POETRY_HOME="/opt/poetry" \ | ||
POETRY_VIRTUALENVS_IN_PROJECT=true \ | ||
POETRY_NO_INTERACTION=1 | ||
|
||
# Install Poetry - respects $POETRY_VERSION & $POETRY_HOME | ||
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python | ||
ENV PATH="$POETRY_HOME/bin:$PATH" | ||
|
||
WORKDIR $FLASK_APP_PATH | ||
COPY ./$FLASK_APP_NAME/poetry.lock ./$FLASK_APP_NAME/pyproject.toml ./$FLASK_APP_NAME/tests ./ ./$FLASK_APP_NAME/configmodule.py ./ | ||
COPY ./$FLASK_APP_NAME/$FLASK_APP_NAME ./$FLASK_APP_NAME | ||
|
||
######### DEV STAGE 2: serve frontend ######### | ||
FROM node-staging as frontend-dev | ||
ARG REACT_APP_NAME | ||
ARG REACT_APP_PATH | ||
#ENV NODE_ENV="development" | ||
#ENV HTTP_PROXY="http://host.docker.internal:1337" | ||
|
||
WORKDIR $REACT_APP_PATH | ||
RUN npm install | ||
CMD ["npm", "run", "start"] | ||
|
||
########## DEV STAGE 3: serve backend ######### | ||
FROM python-staging as backend-dev | ||
ARG FLASK_APP_NAME | ||
ARG FLASK_APP_PATH | ||
|
||
WORKDIR $FLASK_APP_PATH | ||
RUN poetry install | ||
|
||
# CONFIG VARS | ||
# ENV DEVEL=true \ | ||
# DB_URI="postgresql://eelbot:[email protected]:5432/eelbot" \ | ||
# BACKEND_HOST="0.0.0.0" \ | ||
# BACKEND_PORT_DEV='1337' | ||
|
||
CMD ["poetry", "run", "server"] | ||
|
||
######## PROD STAGE 1: build backend ######### | ||
FROM python-staging as python-build-env | ||
ARG FLASK_APP_PATH | ||
ARG FLASK_APP_NAME | ||
|
||
WORKDIR $FLASK_APP_PATH | ||
RUN poetry build --format wheel | ||
RUN poetry export --format requirements.txt --output constraints.txt --without-hashes | ||
|
||
######## PROD STAGE 2: serve backend ######### | ||
FROM python:$PYTHON_VERSION as backend-prod | ||
ARG FLASK_APP_NAME | ||
ARG FLASK_APP_PATH | ||
|
||
ENV \ | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
PYTHONUNBUFFERED=1 \ | ||
PYTHONFAULTHANDLER=1 | ||
|
||
ENV \ | ||
PIP_NO_CACHE_DIR=off \ | ||
PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
PIP_DEFAULT_TIMEOUT=100 | ||
|
||
# CONFIG VARS | ||
# ENV DB_URI="postgresql://eelbot:[email protected]:5432/eelbot" \ | ||
# BACKEND_HOST="0.0.0.0" \ | ||
# BACKEND_PORT="1338" | ||
|
||
WORKDIR $FLASK_APP_PATH | ||
COPY --from=python-build-env $FLASK_APP_PATH/dist/*.whl ./ | ||
COPY --from=python-build-env $FLASK_APP_PATH/constraints.txt ./ | ||
RUN pip install ./${FLASK_APP_NAME}*.whl --constraint constraints.txt | ||
|
||
ENV FLASK_APP_NAME $FLASK_APP_NAME | ||
|
||
WORKDIR "/usr/local/lib/python3.10/site-packages/$FLASK_APP_NAME" | ||
|
||
CMD python -m $FLASK_APP_NAME | ||
|
||
######## PROD STAGE 3: build frontend ######### | ||
FROM node-staging as node-build-env | ||
ARG REACT_APP_NAME | ||
ARG REACT_APP_PATH | ||
|
||
WORKDIR $REACT_APP_PATH | ||
|
||
ENV NODE_ENV="production" | ||
|
||
RUN npm install --production | ||
RUN npm run build | ||
|
||
######## PROD STAGE 4: serve frontend ######### | ||
FROM nginx:$NGINX_VERSION as frontend-prod | ||
ARG REACT_APP_NAME | ||
ARG REACT_APP_PATH | ||
|
||
COPY --from=node-build-env $REACT_APP_PATH/build /usr/share/nginx/html | ||
CMD ["nginx", "-g", "daemon off;"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.dockerignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this line needed? I always assumed |
||
Dockerfile | ||
Dockerfile.prod |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +0,0 @@ | ||
import os | ||
__version__ = '0.1.0' | ||
|
||
def main(): | ||
from backend.app import DevApp, ProdApp | ||
app = DevApp() if 'DEVEL' in os.environ else ProdApp() | ||
app.serve() | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
__version__ = '0.1.0' | ||
|
||
def main(): | ||
from backend.app import DevApp, ProdApp | ||
app = DevApp() if 'DEVEL' in os.environ else ProdApp() | ||
app.serve() | ||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import os | ||
basedir = os.path.abspath(os.path.dirname(__file__)) | ||
|
||
class Config: | ||
SQLALCHEMY_DATABASE_URI = os.environ.get('DB_URI') or "postgresql://eelbot:[email protected]:5432/eelbot" | ||
HOST = os.environ.get('BACKEND_HOST') or "127.0.0.1" | ||
|
||
class DevelopmentConfig(Config): | ||
ENV = 'development' | ||
PORT = os.environ.get('BACKEND_PORT_DEV') or "1337" | ||
|
||
class ProductionConfig(Config): | ||
PORT = os.environ.get('BACKEND_PORT') or "1338" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another good candidate to ignore is the
.git/
folder, as well as the.gitignore
file.