advent22/Dockerfile

130 lines
3 KiB
Docker
Raw Permalink Normal View History

ARG NODE_VERSION=24
ARG PYTHON_VERSION=3.14-slim
#############
# build api #
#############
ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS build-api
# env setup
WORKDIR /usr/local/src/advent22_api
ENV \
PATH="/root/.local/bin:${PATH}"
# install poetry with export plugin
RUN set -ex; \
\
python -m pip --no-cache-dir install --upgrade pip wheel; \
\
apt-get update; apt-get install --yes --no-install-recommends \
curl \
; rm -rf /var/lib/apt/lists/*; \
\
curl -sSL https://install.python-poetry.org | python3 -; \
poetry self add poetry-plugin-export;
# build dependency wheels
COPY api/pyproject.toml api/poetry.lock ./
RUN set -ex; \
\
# # buildtime dependencies
# apt-get update; apt-get install --yes --no-install-recommends \
# build-essential \
# ; rm -rf /var/lib/apt/lists/*; \
\
# generate requirements.txt
poetry export \
--format requirements.txt \
--output requirements.txt; \
\
python3 -m pip --no-cache-dir wheel \
--wheel-dir ./dist \
--requirement requirements.txt;
# build advent22_api wheel
COPY api ./
RUN poetry build --format wheel --output ./dist
2023-09-18 18:16:34 +00:00
############
# build ui #
############
2023-09-16 21:57:19 +00:00
ARG NODE_VERSION
2023-10-27 15:10:25 +00:00
FROM node:${NODE_VERSION} AS build-ui
2023-09-19 16:00:31 +00:00
# env setup
2023-09-18 18:16:34 +00:00
WORKDIR /usr/local/src/advent22_ui
2023-09-16 21:57:19 +00:00
2023-09-19 16:00:31 +00:00
# install advent22_ui dependencies
2023-09-18 19:03:38 +00:00
COPY ui/package*.json ui/yarn*.lock ./
RUN set -ex; \
corepack enable; \
yarn install;
2023-09-18 18:16:34 +00:00
2023-09-19 16:00:31 +00:00
# copy and build advent22_ui
2023-09-18 19:03:38 +00:00
COPY ui ./
RUN set -ex; \
yarn dlx update-browserslist-db@latest; \
yarn build --dest /tmp/advent22_ui/html; \
# exclude webpack-bundle-analyzer output
rm -f /tmp/advent22_ui/html/report.html;
######################
# python preparation #
######################
ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS uvicorn-gunicorn
# where credit is due ...
LABEL maintainer="Sebastián Ramirez <tiangolo@gmail.com>"
WORKDIR /usr/local/share/uvicorn-gunicorn
# install uvicorn-gunicorn
COPY ./scripts/mini-tiangolo ./
RUN set -ex; \
chmod +x start.sh; \
python3 -m pip --no-cache-dir install gunicorn;
CMD ["/usr/local/share/uvicorn-gunicorn/start.sh"]
2023-09-18 18:16:34 +00:00
###########
# web app #
###########
FROM uvicorn-gunicorn AS production
2023-09-18 18:16:34 +00:00
# env setup
2023-09-16 21:57:19 +00:00
ENV \
2023-09-18 18:16:34 +00:00
PRODUCTION_MODE="true" \
2023-09-19 16:00:31 +00:00
PORT="8000" \
2023-09-18 19:03:38 +00:00
MODULE_NAME="advent22_api.app"
2023-09-19 16:00:31 +00:00
EXPOSE 8000
2023-09-18 18:16:34 +00:00
WORKDIR /opt/advent22
VOLUME [ "/opt/advent22" ]
COPY --from=build-api /usr/local/src/advent22_api/dist /usr/local/share/advent22_api.dist
2023-09-18 19:03:38 +00:00
RUN set -ex; \
2023-09-19 16:00:31 +00:00
# remove example app
2023-09-18 19:03:38 +00:00
rm -rf /app; \
\
# # runtime dependencies
# apt-get update; apt-get install --yes --no-install-recommends \
# ; rm -rf /var/lib/apt/lists/*; \
\
# install advent22_api wheels
python3 -m pip --no-cache-dir install --no-deps /usr/local/share/advent22_api.dist/*.whl; \
\
# prepare data directory
chown nobody:nogroup ./
2023-09-16 21:57:19 +00:00
2023-10-29 16:28:34 +00:00
# add prepared advent22_ui
COPY --from=build-ui /tmp/advent22_ui /usr/local/share/advent22_ui
2023-09-18 19:03:38 +00:00
# run as unprivileged user
USER nobody