Skip to content

Commit 9b34d61

Browse files
Merge pull request #304 from NCEAS/feature-303-dockerfile-refactor
Feature-303: Dockerfile `venv`
2 parents 5d4c256 + 017e1b8 commit 9b34d61

File tree

7 files changed

+683
-63
lines changed

7 files changed

+683
-63
lines changed

.github/workflows/python-app.yaml

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Python CI with uv and pytest
2+
3+
on:
4+
push:
5+
branches: [ "main", "develop" ]
6+
pull_request:
7+
branches: [ "main", "develop" ]
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
python-version: ["3.13", "3.14" ]
19+
steps:
20+
- uses: actions/checkout@v5
21+
- name: Setup uv
22+
uses: astral-sh/setup-uv@v7
23+
with:
24+
version: '0.9.15'
25+
python-version: ${{ matrix.python-version }}
26+
27+
- name: Install the project
28+
run: uv sync --all-extras --dev
29+
30+
- name: Run tests with pytest
31+
run: uv run pytest tests

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ ENV/
151151
env.bak/
152152
venv.bak/
153153

154+
# uv package manager
155+
bin/
156+
154157
# Spyder project settings
155158
.spyderproject
156159
.spyproject

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,64 @@ commits can be run, for example, for the mac with:
7777

7878
- `act --container-architecture linux/amd64`
7979

80+
### Installing `vegbank` using the `uv` package manager
81+
82+
We are using `uv` as our python environment and dependency manager. To get started locally, follow these instructions below:
83+
84+
```sh
85+
# Step 1: Set your `VIRTUAL_ENV` path (if not already set)
86+
$ export VIRTUAL_ENV=/data/venv
87+
88+
# Step 2: Install `uv` (if not already installed)
89+
$ python -m pip install uv --root-user-action ignore
90+
91+
# Step 3: Navigate to your project root
92+
$ cd /path/to/vegbank2
93+
94+
# Step 4: Create/reuse the venv and activate it
95+
$ uv venv --allow-existing ${VIRTUAL_ENV}
96+
$ source ${VIRTUAL_ENV}/bin/activate
97+
98+
# Step 5: Install all dependencies into the venv
99+
$ uv sync --active
100+
101+
```
102+
103+
### Adding project dependencies
104+
105+
To add a dependency to this project, you will need to update `pyproject.toml` with the library that you are trying to add, as well as the minimum version required.
106+
- Note: An easy way to add dependencies to pyproject.toml is to add them with `uv` or other package management tools using a command like `uv add some_package`.
107+
108+
- If the library is required at runtime, add it under `dependencies`
109+
- If the library is used only for development or testing, add it under `dependency-groups.dev`
110+
111+
```py
112+
# pyproject.toml
113+
114+
...
115+
116+
dependencies = [
117+
"psycopg>=3.3.2",
118+
"flask>=3.1.2",
119+
"pandas>=3.0.0",
120+
"pyarrow>=23.0.0",
121+
"numpy>=2.4.2",
122+
"DEPENDENCYNAME>=#.#.#", # Add runtime dependencies here
123+
]
124+
125+
[dependency-groups]
126+
dev = [
127+
# Add development dependencies here
128+
"pytest>=8.3.3",
129+
"pylint>=3.2.7",
130+
"black>=24.8.0",
131+
]
132+
133+
```
134+
135+
Lastly, build and deploy your Docker image. If you are unsure of how to, please follow the `README.md` under `/docker`.
136+
137+
80138
## Current Contributors
81139

82140
- Matthew B. Jones (jones@nceas.ucsb.edu): [ORCID: 0000-0003-0077-4738](https://orcid.org/0000-0003-0077-4738)

docker/Dockerfile

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@ FROM python:3.13
22

33
LABEL org.opencontainers.image.title="vegbank2 Flask API"
44
LABEL org.opencontainers.image.source="http://github.com/nceas/vegbank2"
5-
LABEL org.opencontainers.image.version="0.0.3-289-importbugs"
5+
LABEL org.opencontainers.image.version="0.0.3-303-docker-venv"
6+
7+
# Add Docker VENV path
8+
ENV VIRTUAL_ENV=/data/venv
9+
# Do not create __pycache__/ directories
10+
ENV PYTHONDONTWRITEBYTECODE=1
11+
# Ensure logs behave correctly by flushing out stdout/stderr immediately
12+
ENV PYTHONUNBUFFERED=1
613

714
WORKDIR /app
815

16+
COPY pyproject.toml uv.lock* /app/
17+
18+
# Install uv
19+
RUN python -m pip install uv --root-user-action ignore
20+
21+
# Create (or reuse) the venv at $VIRTUAL_ENV
22+
RUN uv venv --allow-existing "${VIRTUAL_ENV}"
23+
24+
# Activate uv venv
25+
RUN . "${VIRTUAL_ENV}/bin/activate" && uv sync --active
26+
27+
# TODO: We should be able to simply copy the application code instead of two directories
28+
# Ex. COPY . /app
929
COPY vegbank /app/vegbank
1030
COPY queries /app/queries
1131

12-
RUN pip install flask
13-
RUN pip install psycopg
14-
RUN pip install pandas
15-
RUN pip install pyarrow
16-
1732
ENV PYTHONPATH=/app
18-
CMD [ "flask", "--app", "vegbank.vegbankapi:app", "run", "--host=0.0.0.0", "--port=80"]
33+
CMD ["/data/venv/bin/python", "-m", "flask", "--app", "vegbank.vegbankapi:app", "run", "--host=0.0.0.0", "--port=80"]

pyproject.toml

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
[tool.poetry]
1+
[project]
22
name = "vegbank"
33
version = "2.0.0-alpha1"
44
description = "VegBank database and data access service"
55
authors = [
6-
"Robert Shelton <shelton@nceas.ucsb.edu>",
7-
"Dou Mok <mok@nceas.ucsb.edu>",
8-
"Jim Regetz <regetz@nceas.ucsb.edu>",
9-
"Matthew B. Jones <jones@nceas.ucsb.edu>"
10-
]
11-
license = "Apache2"
12-
readme = "README.md"
6+
{ name = "Robert Shelton", email = "shelton@nceas.ucsb.edu" },
7+
{ name = "Dou Mok", email = "douming.mok@gmail.com" },
8+
{ name = "Jim Regetz", email = "regetz@nceas.ucsb.edu" },
9+
{ name = "Matthew B. Jones", email = "jones@nceas.ucsb.edu" },
10+
]
11+
license = { text = "Apache-2.0" }
12+
requires-python = ">=3.13"
13+
14+
dependencies = [
15+
"psycopg>=3.3.2",
16+
"flask>=3.1.2",
17+
"pandas>=3.0.0",
18+
"pyarrow>=23.0.0",
19+
"numpy>=2.4.2",
20+
]
1321

14-
[tool.poetry.dependencies]
15-
python = "^3.12"
22+
[dependency-groups]
23+
dev = [
24+
"pytest>=8.3.3",
25+
"pylint>=3.2.7",
26+
"black>=24.8.0",
27+
]
1628

17-
[tool.poetry.group.dev.dependencies]
18-
poetry = ">=1.8.3"
19-
pytest = ">=8.3.3"
20-
pylint = ">=3.2.7"
21-
black = ">=24.8.0"
22-
psycopg = ">=3.3.2"
23-
flask = ">=3.1.2"
24-
pandas = ">=3.0.0"
25-
pyarrow = ">=23.0.0"
26-
numpy = ">=2.4.2"
29+
[tool.hatch.build.targets.wheel]
30+
packages = ["vegbank"]
2731

2832
[build-system]
29-
requires = ["poetry-core"]
30-
build-backend = "poetry.core.masonry.api"
33+
requires = ["hatchling"]
34+
build-backend = "hatchling.build"
3135

3236
[tool.pytest.ini_options]
3337
addopts = [

0 commit comments

Comments
 (0)