Skip to content

Commit

Permalink
Merge pull request #57 from codeforjapan/chore/issue-54-dockerize-api
Browse files Browse the repository at this point in the history
APIのDocker化
  • Loading branch information
osoken authored May 16, 2024
2 parents 1db863f + d7186f4 commit 6640c6e
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests
44 changes: 44 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ARG PYTHON_VERSION_CODE=3.10
ARG ENVIRONMENT="dev"
# ENVIRONMENT: dev or prod, refer to project.optional-dependencies in pyproject.toml

FROM python:${PYTHON_VERSION_CODE}-bookworm as builder
ARG PYTHON_VERSION_CODE
ARG ENVIRONMENT

WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

COPY pyproject.toml README.md ./
COPY birdxplorer_api/__init__.py ./birdxplorer_api/

RUN if [ "${ENVIRONMENT}" = "prod" ]; then \
apt-get update && apt-get install -y --no-install-recommends \
postgresql-client-15 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*; \
fi
RUN pip install --no-cache-dir -e ".[${ENVIRONMENT}]"

FROM python:${PYTHON_VERSION_CODE}-slim-bookworm as runner
ARG PYTHON_VERSION_CODE
ARG ENVIRONMENT

WORKDIR /app

RUN if [ "${ENVIRONMENT}" = "prod" ]; then \
apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*; \
fi

RUN groupadd -r app && useradd -r -g app app
RUN chown -R app:app /app
USER app

COPY --from=builder /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages
COPY --chown=app:app . ./

ENTRYPOINT ["python", "-m", "uvicorn", "birdxplorer_api.main:app", "--host", "0.0.0.0"]
2 changes: 2 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

# BirdXplorer API
9 changes: 5 additions & 4 deletions api/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ authors = [
{name = "osoken"},
]
dynamic = [
"version",
"version",
]
readme = "../README.md"
readme = "README.md"
license = {file = "../LICENSE"}
requires-python = ">=3.10"

Expand Down Expand Up @@ -62,6 +62,7 @@ dev=[
"httpx",
]
prod=[
"psycopg2",
]

[tool.pytest.ini_options]
Expand Down Expand Up @@ -100,11 +101,11 @@ legacy_tox_ini = """
envlist = py310
[testenv]
setenv =
setenv =
VIRTUALENV_PIP = 24.0
deps =
-e .[dev]
commands =
commands =
black birdxplorer_api tests
isort birdxplorer_api tests
pytest
Expand Down
37 changes: 37 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,47 @@ services:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${BX_STORAGE_SETTINGS__PASSWORD}
POSTGRES_DB: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
ports:
- '5432:5432'
volumes:
- postgres_data:/var/lib/postgresql/data
app:
depends_on:
db:
condition: service_healthy
build:
args:
- ENVIRONMENT=dev
context: ./api
dockerfile: Dockerfile
env_file:
- .env
ports:
- '8000:8000'
develop:
watch:
- action: rebuild
path: ./api
target: /app/api
migrate:
depends_on:
db:
condition: service_healthy
build:
args:
- ENVIRONMENT=dev
context: ./migrate
dockerfile: Dockerfile
environment:
- WAIT_HOSTS=db:5432
env_file:
- .env


volumes:
postgres_data:
45 changes: 45 additions & 0 deletions migrate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
ARG PYTHON_VERSION_CODE=3.10
ARG ENVIRONMENT="dev"
# ENVIRONMENT: dev or prod, refer to project.optional-dependencies in pyproject.toml

FROM python:${PYTHON_VERSION_CODE}-bookworm as builder
ARG PYTHON_VERSION_CODE
ARG ENVIRONMENT

WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1

COPY pyproject.toml README.md ./
COPY birdxplorer_migration/__init__.py ./birdxplorer_migration/

RUN if [ "${ENVIRONMENT}" = "prod" ]; then \
apt-get update && apt-get install -y --no-install-recommends \
postgresql-client-15 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*; \
fi

RUN pip install --no-cache-dir -e ".[${ENVIRONMENT}]"

FROM python:${PYTHON_VERSION_CODE}-slim-bookworm as runner
ARG PYTHON_VERSION_CODE
ARG ENVIRONMENT

WORKDIR /app

RUN if [ "${ENVIRONMENT}" = "prod" ]; then \
apt-get update && apt-get install -y --no-install-recommends \
libpq5 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*; \
fi

RUN groupadd -r app && useradd -r -g app app
RUN chown -R app:app /app
USER app

COPY --from=builder /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages /usr/local/lib/python${PYTHON_VERSION_CODE}/site-packages
COPY --chown=app:app . ./

ENTRYPOINT ["python", "birdxplorer_migration/scripts/migrate_all.py", "birdxplorer_migration/data/appv1/"]
2 changes: 2 additions & 0 deletions migrate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

# BirdXplorer Migrations
1 change: 1 addition & 0 deletions migrate/birdxplorer_migration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
File renamed without changes.
File renamed without changes.
File renamed without changes.
104 changes: 104 additions & 0 deletions migrate/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
[build-system]
build-backend = "flit_core.buildapi"
requires = ["flit_core >=3.8.0,<4"]


[project]
name = "birdxplorer_migration"
description = "The Migration Scripts for BirdXplorer project."
authors = [
{name = "osoken"},
]
dynamic = [
"version",
]
readme = "README.md"
license = {file = "../LICENSE"}
requires-python = ">=3.10"

classifiers = [
"Development Status :: 3 - Alpha",
"Natural Language :: Japanese",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.10",
]

dependencies = [
"birdxplorer_common @ git+https://github.com/codeforjapan/BirdXplorer.git@feature/issue-53-divide-python-packages#subdirectory=common",
"sqlalchemy",
"python-dotenv",
]

[project.urls]
Source = "https://github.com/codeforjapan/BirdXplorer"

[tool.setuptools]
packages=["birdxplorer"]

[tool.setuptools.package-data]
birdxplorer = ["py.typed"]

[project.optional-dependencies]
dev=[
"black",
"flake8",
"pyproject-flake8",
"pytest",
"mypy",
"tox",
"isort",
"pytest-mock",
"pytest-cov",
"freezegun",
"types-python-dateutil",
"psycopg2-binary",
"factory_boy",
"uvicorn",
"polyfactory",
"httpx",
]
prod=[
"psycopg2"
]

[tool.black]
line-length = 120
target-version = ['py310']

[tool.flake8]
max-line-length = 120
extend-ignore = "E203,E701"

[tool.mypy]
python_version = "3.10"
warn_return_any = true
warn_unused_configs = true
plugins = ["pydantic.mypy"]

[tool.pydantic.mypy]
init_typed = true

[tool.isort]
profile = "black"
known_first_party = "birdxplorer_api,birdxplorer_common,birdxplorer_etl"

[tool.tox]
legacy_tox_ini = """
[tox]
skipsdist = true
envlist = py310
[testenv]
setenv =
VIRTUALENV_PIP = 24.0
deps =
-e .[dev]
commands =
black birdxplorer_api tests
isort birdxplorer_api tests
pytest
pflake8 birdxplorer_api/ tests/
mypy birdxplorer_api --strict
mypy tests --strict
"""

0 comments on commit 6640c6e

Please sign in to comment.