Skip to content
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

build: use mamba to setup project in Docker #287

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,37 @@ RUN mkdir -p /sketch_map_tool/static/bundles
RUN npm run build


FROM ubuntu:22.04
FROM condaforge/mambaforge:latest


# install libzbar (neccessary for pyzbar to read the QR codes)
# install gdal
# install libgl1 (for openCV)
# to reduce image size, clean up the apt cache by removing /var/lib/apt/lists.
RUN apt-get update \
&& apt-get install -y --no-upgrade \
python3-pip \
libzbar0 \
libgdal-dev \
libgl1 \
&& rm -rf /var/lib/apt/lists/*

# update C env vars so compiler can find gdal
ENV CPLUS_INCLUDE_PATH=/usr/include/gdal
ENV C_INCLUDE_PATH=/usr/include/gdal

# libzbar0 \
# libgdal-dev \
# within docker container: run without root privileges
RUN useradd -md /home/smt smt
WORKDIR /opt/smt
RUN chown smt:smt . -R
USER smt:smt

# make poetry binaries available to the docker container user
ENV PATH=$PATH:/home/smt/.local/bin
COPY --chown=smt:smt environment.yml environment.yml
RUN mamba env create --file environment.yml

# make RUN commands use the new environment:
SHELL ["mamba", "run", "--no-capture-output", "--name", "smt", "/bin/bash", "-c"]

COPY --chown=smt:smt pyproject.toml pyproject.toml
COPY --chown=smt:smt poetry.lock poetry.lock
COPY --chown=smt:smt setup.cfg setup.cfg
# install Python dependencies
RUN pip3 install --no-cache-dir poetry
# these versions have to be fixed for now, since poetry and (py)gdal packages can't work together
RUN python3 -m poetry run pip install numpy==1.23.5
RUN python3 -m poetry run pip install pygdal==3.4.1.10
RUN python3 -m poetry install --no-ansi --no-interaction --no-root

# copy all the other files and install the project
RUN which python
RUN python -m poetry install --no-ansi --no-interaction --no-root

COPY --chown=smt:smt sketch_map_tool sketch_map_tool
COPY --chown=smt:smt data/ data/
COPY --chown=smt:smt tests/ tests/
RUN python3 -m poetry install --no-ansi --no-interaction
RUN python -m poetry install --no-ansi --no-interaction

# get JS dependencies
COPY --from=bundler --chown=smt:smt /sketch_map_tool/static/bundles sketch_map_tool/static/bundles

# Use entry-points defined in docker-compose file
# use entry-points defined in docker-compose file
38 changes: 32 additions & 6 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ services:
SMT-RESULT-BACKEND: "db+postgresql://smt:smt@postgres:5432"
ports:
- "127.0.0.1:8081:8080"
entrypoint: ["poetry", "run", "waitress-serve", "sketch_map_tool.routes:app"]
entrypoint:
[
"mamba",
"run",
"--no-capture-output",
"--name",
"smt",
"waitress-serve",
"sketch_map_tool.routes:app",
]
celery:
# Task queue
build:
Expand All @@ -28,7 +37,24 @@ services:
environment:
SMT-BROKER-URL: "redis://redis:6379"
SMT-RESULT-BACKEND: "db+postgresql://smt:smt@postgres:5432"
entrypoint: ["poetry", "run", "celery", "--app", "sketch_map_tool.tasks", "worker", "--beat", "--concurrency", "4", "--loglevel", "INFO", "-E"]
entrypoint:
[
"mamba",
"run",
"--no-capture-output",
"--name",
"smt",
"celery",
"--app",
"sketch_map_tool.tasks",
"worker",
"--beat",
"--concurrency",
"4",
"--loglevel",
"INFO",
"-E",
]
flower:
# Monitoring of Celery
image: mher/flower
Expand All @@ -46,7 +72,7 @@ services:
# dump redis db every 10 minutes (if 1 value changed)
entrypoint: ["redis-server", "--save", "\"\""]
volumes:
- redis_data:/data
- redis_data:/data
postgres:
# Result backend
image: postgres:15
Expand All @@ -57,7 +83,7 @@ services:
ports:
- "5432:5432"
volumes:
- pg_data:/var/lib/postgresql/data
- pg_data:/var/lib/postgresql/data
volumes:
redis_data:
pg_data:
redis_data:
pg_data:
5 changes: 3 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ channels:
dependencies:
- python=3.10.*
- poetry=1.*
- gdal=3.7.1 # same as in Dockerfile and pyproject.toml
- pyzbar=0.1.9 # same as in Dockerfile and pyproject.toml
- gdal=3.7.1
- pyzbar=0.1.9
- psycopg2=2.9.7
36 changes: 1 addition & 35 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ opencv-python = "^4.6.0"
qrcode = "^7.3.1"
svglib = "^1.4.1"
PyMuPDF = {extras = ["Pillow"], version = "^1.21.0"}
psycopg2 = "^2.9.5"
plotly = "^5.15.0"
kaleido = "0.2.1" # Not working with '^', cf. https://github.com/plotly/Kaleido/issues/125
geojson = "^2.5.0"
Expand Down
2 changes: 1 addition & 1 deletion sketch_map_tool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def make_flask() -> Flask:
"task_serializer": "pickle",
"task_track_started": True, # report ‘started’ status worker executes task
"task_send_sent_event": True,
"result_serializer": "json",
"result_serializer": "pickle",
"result_extended": True, # save result attributes to backend (e.g. name)
"result_compression": "gzip",
"result_chord_join_timeout": 10.0, # default: 3.0 seconds
Expand Down
7 changes: 2 additions & 5 deletions sketch_map_tool/database/client_flask.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from datetime import datetime, timezone
from uuid import UUID

import psycopg2
Expand Down Expand Up @@ -31,16 +30,14 @@ def _insert_id_map(uuid: str, map_: dict):
create_query = """
CREATE TABLE IF NOT EXISTS uuid_map(
uuid uuid PRIMARY KEY,
ts timestamptz;
map json NOT NULL
)
"""
insert_query = "INSERT INTO uuid_map(uuid, ts, map) VALUES (%s, %s, %s)"
insert_query = "INSERT INTO uuid_map(uuid, map) VALUES (%s, %s)"
db_conn = open_connection()
with db_conn.cursor() as curs:
curs.execute(create_query)
dt = datetime.now(timezone.utc)
curs.execute(insert_query, [uuid, dt, json.dumps(map_)])
curs.execute(insert_query, [uuid, json.dumps(map_)])


def _delete_id_map(uuid: str):
Expand Down
Loading