-
Notifications
You must be signed in to change notification settings - Fork 34
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 #225 from hotosm/enhance/dockerfile
fix(dockerfile): Enhance building speed on source code changes
- Loading branch information
Showing
5 changed files
with
418 additions
and
393 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,31 @@ FROM docker.io/python:${PYTHON_VERSION}-slim-bookworm as base | |
ARG [email protected] | ||
ENV DEBIAN_FRONTEND=noninteractive | ||
|
||
FROM base as builder | ||
FROM base as runner | ||
|
||
WORKDIR /home/appuser | ||
ENV PIP_NO_CACHE_DIR=1 | ||
ENV PYTHONUNBUFFERED=1 | ||
ENV PATH="/home/appuser/.local/bin:$PATH" | ||
ENV PYTHON_LIB="/home/appuser/.local/lib/python$PYTHON_VERSION/site-packages" | ||
|
||
# Install runtime dependencies | ||
RUN apt-get update \ | ||
&& apt-get -y upgrade \ | ||
&& apt-get --no-install-recommends -y install libpq5 gdal-bin \ | ||
&& apt-get -y autoremove \ | ||
&& apt-get clean \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
|
||
|
||
FROM ghcr.io/hotosm/tippecanoe:main as tippecanoe-builder | ||
|
||
FROM runner as with-tippecanoe | ||
COPY --from=tippecanoe-builder /usr/local/bin/tippecanoe* /usr/local/bin/ | ||
COPY --from=tippecanoe-builder /usr/local/bin/tile-join /usr/local/bin/ | ||
|
||
# Builder stage , python dependencies and project setup | ||
FROM base as python-builder | ||
|
||
ENV PIP_NO_CACHE_DIR=1 | ||
ENV PYTHONUNBUFFERED=1 | ||
|
@@ -23,49 +47,30 @@ COPY pyproject.toml . | |
COPY requirements.txt . | ||
COPY README.md . | ||
COPY LICENSE . | ||
|
||
RUN pip install --user --no-cache-dir --upgrade pip setuptools wheel\ | ||
&& pip install --user --no-cache-dir GDAL=="$(gdal-config --version)" \ | ||
&& pip install --user --no-cache-dir -r requirements.txt | ||
|
||
RUN pip install --user -e . | ||
RUN python setup.py install --user | ||
|
||
FROM base as runner | ||
WORKDIR /home/appuser | ||
ENV PIP_NO_CACHE_DIR=1 | ||
ENV PYTHONUNBUFFERED=1 | ||
ENV PATH="/home/appuser/.local/bin:$PATH" | ||
ENV PYTHON_LIB="/home/appuser/.local/lib/python$PYTHON_VERSION/site-packages" | ||
|
||
RUN apt-get update \ | ||
&& apt-get -y upgrade \ | ||
&& apt-get --no-install-recommends -y install libpq5 gdal-bin \ | ||
&& apt-get -y autoremove \ | ||
&& apt-get clean \ | ||
&& rm -rf /var/lib/apt/lists/* | ||
FROM with-tippecanoe as prod | ||
COPY --from=python-builder /root/.local /home/appuser/.local | ||
|
||
COPY --from=builder /root/.local /home/appuser/.local | ||
COPY README.md . | ||
RUN useradd --system --uid 900 --home-dir /home/appuser --shell /bin/false appuser \ | ||
&& chown -R appuser:appuser /home/appuser | ||
|
||
# Enable this if you are using config.txt | ||
# COPY config.txt ./config.txt | ||
USER appuser | ||
|
||
# API and source code, changes here don't invalidate previous layers , You can overwrite this block with -v | ||
|
||
# Copy config.txt if you have your configuration setup in config | ||
# COPY config.txt . | ||
COPY README.md . | ||
COPY setup.py . | ||
COPY pyproject.toml . | ||
COPY API/ ./API/ | ||
COPY src/ ./src/ | ||
# Use a separate stage to pull the tippecanoe image | ||
FROM ghcr.io/hotosm/tippecanoe:main as tippecanoe-builder | ||
|
||
FROM runner as prod | ||
|
||
# Copy tippecanoe binaries from the tippecanoe stage | ||
COPY --from=tippecanoe-builder /usr/local/bin/tippecanoe* /usr/local/bin/ | ||
COPY --from=tippecanoe-builder /usr/local/bin/tile-join /usr/local/bin/ | ||
|
||
RUN useradd --system --uid 900 --home-dir /home/appuser --shell /bin/false appuser \ | ||
&& chown -R appuser:appuser /home/appuser | ||
|
||
USER appuser | ||
|
||
# CMD ["/bin/bash"] | ||
CMD ["uvicorn", "API.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000", "--no-use-colors", "--proxy-headers"] | ||
CMD ["uvicorn", "API.main:app", "--reload", "--host", "0.0.0.0", "--port", "8000", "--no-use-colors", "--proxy-headers"] |
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,21 @@ | ||
[DB] | ||
PGHOST=pgsql | ||
PGUSER=postgres | ||
PGPASSWORD=admin | ||
PGDATABASE=raw | ||
PGPORT=5432 | ||
|
||
[API_CONFIG] | ||
RATE_LIMITER_STORAGE_URI=redis://redis:6379 | ||
|
||
[CELERY] | ||
CELERY_BROKER_URL=redis://redis:6379/0 | ||
CELERY_RESULT_BACKEND=redis://redis:6379/0 | ||
|
||
[OAUTH] | ||
OSM_CLIENT_ID= | ||
OSM_CLIENT_SECRET= | ||
OSM_URL=https://www.openstreetmap.org | ||
OSM_PERMISSION_SCOPE=read_prefs | ||
LOGIN_REDIRECT_URI=http://127.0.0.1:8000/v1/auth/callback | ||
APP_SECRET_KEY=replace_this_with_your_trusted_secret_key |
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,58 +1,78 @@ | ||
version: '3.8' | ||
version: "3.8" | ||
|
||
services: | ||
|
||
postgres: | ||
restart: always | ||
image: postgis/postgis | ||
container_name: pgsql | ||
environment: | ||
- POSTGRES_DB=raw | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=admin | ||
ports: | ||
- '5434:5432' | ||
volumes: | ||
- ./postgres-data:/var/lib/postgresql/data | ||
- ./tests/fixtures/pokhara.sql:/docker-entrypoint-initdb.d/pokhara.sql | ||
restart: always | ||
image: postgis/postgis | ||
container_name: pgsql | ||
environment: | ||
- POSTGRES_DB=raw | ||
- POSTGRES_USER=postgres | ||
- POSTGRES_PASSWORD=admin | ||
ports: | ||
- "5434:5432" | ||
volumes: | ||
- ./postgres-data:/var/lib/postgresql/data | ||
- ./tests/fixtures/pokhara.sql:/docker-entrypoint-initdb.d/pokhara.sql | ||
healthcheck: | ||
test: ["CMD-SHELL", "pg_isready -U postgres -d raw"] | ||
interval: 60s | ||
timeout: 30s | ||
retries: 5 | ||
start_period: 10s | ||
|
||
redis: | ||
image: redis | ||
container_name: redis | ||
ports: | ||
- "6379:6379" | ||
- "6379:6379" | ||
|
||
app: | ||
api: | ||
build: . | ||
container_name: api | ||
container_name: rawdataapi | ||
command: uvicorn API.main:app --reload --host 0.0.0.0 --port 8000 --no-use-colors --proxy-headers | ||
ports: | ||
- 8000:8000 | ||
volumes: | ||
- .:/app | ||
- "./API:/home/appuser/API" | ||
- "./src:/home/appuser/src" | ||
- "./docker-compose-config.txt:/home/appuser/config.txt" | ||
depends_on: | ||
- redis | ||
- postgres | ||
redis: | ||
condition: service_started | ||
postgres: | ||
condition: service_healthy | ||
|
||
worker: | ||
build: . | ||
container_name: worker | ||
command: celery --app API.api_worker worker --loglevel=INFO | ||
container_name: rawdata-worker | ||
command: celery --app API.api_worker worker --loglevel=INFO --queues="raw_ondemand" -n 'default_worker' | ||
volumes: | ||
- .:/app | ||
- "./API:/home/appuser/API" | ||
- "./src:/home/appuser/src" | ||
- "./docker-compose-config.txt:/home/appuser/config.txt" | ||
depends_on: | ||
- app | ||
- redis | ||
- postgres | ||
api: | ||
condition: service_started | ||
redis: | ||
condition: service_started | ||
postgres: | ||
condition: service_healthy | ||
|
||
|
||
worker-dashboard: | ||
flower: | ||
build: . | ||
container_name: flower | ||
command: celery --broker=redis://redis:6379// --app API.api_worker flower --address=0.0.0.0 --port=5000 | ||
container_name: rawdata-flower | ||
command: celery --broker=redis://redis:6379// --app API.api_worker flower --port=5555 --queues="raw_daemon,raw_ondemand" | ||
ports: | ||
- 5000:5000 | ||
- 5555:5555 | ||
volumes: | ||
- "./API:/home/appuser/API" | ||
- "./src:/home/appuser/src" | ||
- "./docker-compose-config.txt:/home/appuser/config.txt" | ||
depends_on: | ||
- app | ||
- redis | ||
- worker | ||
api: | ||
condition: service_started | ||
redis: | ||
condition: service_started | ||
postgres: | ||
condition: service_healthy |
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
Oops, something went wrong.