-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from max-pfeiffer/feature/dockerfile_optimisations
Feature/dockerfile optimisations
- Loading branch information
Showing
9 changed files
with
80 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ def test_custom_config(cookies) -> None: | |
assert result.project_path.name == "seriously-silly-project-name" | ||
assert result.project_path.is_dir() | ||
|
||
# Check if pyproject.toml became expanded correctly | ||
pyproject_toml_file: Path = result.project_path / "pyproject.toml" | ||
toml_data: dict = toml.load(pyproject_toml_file) | ||
|
||
|
@@ -39,7 +40,9 @@ def test_custom_config(cookies) -> None: | |
"Jane Doe <[email protected]>" | ||
] | ||
|
||
# Check if Dockerfile became expanded correctly | ||
dockerfile: Path = result.project_path / "Dockerfile" | ||
dfp = DockerfileParser(path=str(dockerfile)) | ||
|
||
assert "3.10.13-bookworm" in dfp.baseimage | ||
assert dfp.is_multistage | ||
assert all(["3.10.13-bookworm" in image for image in dfp.parent_images]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,59 @@ | ||
# Be aware that you need to specify these arguments before the first FROM | ||
# see: https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact | ||
FROM pfeiffermax/uvicorn-poetry:3.2.0-python{{ cookiecutter.python_version }}-{{ cookiecutter.operating_system_variant }} | ||
# Using an image for dependency build stage which provides Poetry | ||
# see: https://github.com/max-pfeiffer/python-poetry/blob/main/build/Dockerfile | ||
FROM pfeiffermax/python-poetry:1.8.0-poetry1.7.1-python{{ cookiecutter.python_version }}-{{ cookiecutter.operating_system_variant }} as dependencies-build-stage | ||
ENV POETRY_VIRTUALENVS_IN_PROJECT=true \ | ||
POETRY_CACHE_DIR="/application_root/.cache" \ | ||
PYTHONPATH=/application_root | ||
|
||
# Set the WORKDIR to the application root. | ||
# https://www.uvicorn.org/settings/#development | ||
# https://docs.docker.com/engine/reference/builder/#workdir | ||
WORKDIR ${PYTHONPATH} | ||
|
||
# install [tool.poetry.dependencies] | ||
# this will install virtual environment into /.venv because of POETRY_VIRTUALENVS_IN_PROJECT=true | ||
# see: https://python-poetry.org/docs/configuration/#virtualenvsin-project | ||
COPY --chown=python_application:python_application ./poetry.lock ./pyproject.toml /application_root/ | ||
COPY ./pyproject.toml ${PYTHONPATH} | ||
RUN poetry install --no-interaction --no-root --without dev | ||
|
||
# Using the standard Python image here to have the least possible image size | ||
FROM python:{{ cookiecutter.python_version }}-{{ cookiecutter.operating_system_variant }} as production-image | ||
ARG APPLICATION_SERVER_PORT=8000 | ||
|
||
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONUNBUFFERED | ||
ENV PYTHONUNBUFFERED=1 \ | ||
# https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
PYTHONPATH=/application_root \ | ||
VIRTUAL_ENVIRONMENT_PATH="/application_root/.venv" \ | ||
APPLICATION_SERVER_PORT=$APPLICATION_SERVER_PORT | ||
|
||
# Adding the virtual environment to PATH in order to "activate" it. | ||
# https://docs.python.org/3/library/venv.html#how-venvs-work | ||
ENV PATH="$VIRTUAL_ENVIRONMENT_PATH/bin:$PATH" | ||
|
||
# Principle of least privilege: create a new user for running the application | ||
RUN groupadd -g 1001 python_application && \ | ||
useradd -r -u 1001 -g python_application python_application | ||
|
||
# Set the WORKDIR to the application root. | ||
# https://www.uvicorn.org/settings/#development | ||
# https://docs.docker.com/engine/reference/builder/#workdir | ||
WORKDIR ${PYTHONPATH} | ||
RUN chown python_application:python_application ${PYTHONPATH} | ||
|
||
# Copy virtual environment | ||
COPY --from=dependencies-build-stage --chown=python_application:python_application ${VIRTUAL_ENVIRONMENT_PATH} ${VIRTUAL_ENVIRONMENT_PATH} | ||
|
||
# Copy application files | ||
COPY --chown=python_application:python_application /app /application_root/app/ | ||
COPY --chown=python_application:python_application /app ${PYTHONPATH}/app/ | ||
|
||
# Document the exposed port | ||
# https://docs.docker.com/engine/reference/builder/#expose | ||
EXPOSE ${APPLICATION_SERVER_PORT} | ||
|
||
# Use the unpriveledged user to run the application | ||
USER 1001 | ||
|
||
# Run the uvicorn application server. | ||
CMD exec uvicorn --workers 1 --host 0.0.0.0 --port $APPLICATION_SERVER_PORT app.main:app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# {{ cookiecutter.project_name }} | ||
{{ cookiecutter.project_description }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters