Skip to content

Commit cbecc38

Browse files
authored
feat: docker build enhancements. (#37)
* feat: docker build enhancements. * fix: refactored build * fix: linting
1 parent 255638c commit cbecc38

11 files changed

+273
-397
lines changed

.dockerignore

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ venv
33
node_modules
44
__pycache__
55
*.pyc
6+
dist/
7+
tmp/

.env.example

+1-16
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11

22
LITESTAR_APP=app.asgi:create_app
33
SECRET_KEY='secret-key'
4-
BUILD_NUMBER=0
5-
DEBUG=False
6-
DEV_MODE=false
7-
DEFAULT_PAGINATION_LIMIT=100
8-
ENVIRONMENT=local
9-
LOG_LEVEL=20
10-
NAME=Litestar Application
4+
ENVIRONMENT=docker
115
OPENAPI_CONTACT_EMAIL=admin@app
126
OPENAPI_CONTACT_NAME="Administrator"
137
OPENAPI_TITLE="Reference Application for Litestar"
14-
OPENAPI_VERSION=1.0.0
158
# Database
16-
DB_USER=app
17-
DB_PASSWORD=app
18-
DB_HOST=db
19-
DB_PORT=5432
20-
DB_NAME=app
21-
DB_ECHO=true
22-
DB_ECHO_POOL=true
23-
DB_POOL_DISABLE=false
249
DB_POOL_MAX_OVERFLOW=10
2510
DB_POOL_SIZE=5
2611
DB_POOL_TIMEOUT=30

.pre-commit-config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: mixed-line-ending
1212
- id: trailing-whitespace
1313
- repo: https://github.com/charliermarsh/ruff-pre-commit
14-
rev: "v0.0.275"
14+
rev: "v0.0.277"
1515
hooks:
1616
- id: ruff
1717
args: ["--fix"]
@@ -33,12 +33,12 @@ repos:
3333
rev: v0.3.0
3434
hooks:
3535
- id: ensure-dunder-all
36-
exclude: "test*|examples*|tools"
36+
exclude: "test*|examples*|tools|scripts"
3737
- repo: https://github.com/ariebovenberg/slotscheck
3838
rev: v0.16.5
3939
hooks:
4040
- id: slotscheck
41-
exclude: "test_*|docs|migrations"
41+
exclude: "test_*|docs|migrations|scripts"
4242
entry: env PYTHONPATH=src slotscheck
4343
- repo: https://github.com/pre-commit/mirrors-mypy
4444
rev: "v1.4.1"

Dockerfile

-133
This file was deleted.

Makefile

+5-16
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
.ONESHELL:
33
ENV_PREFIX=$(shell python3 -c "if __import__('pathlib').Path('.venv/bin/pip').exists(): print('.venv/bin/')")
44
USING_POETRY=$(shell grep "tool.poetry" pyproject.toml && echo "yes")
5-
USING_DOCKER=$(shell grep "USE_DOCKER=true" .env && echo "yes")
6-
USING_PNPM=$(shell python3 -c "if __import__('pathlib').Path('pnpm-lock.yaml').exists(): print('yes')")
7-
USING_YARN=$(shell python3 -c "if __import__('pathlib').Path('yarn.lock').exists(): print('yes')")
85
USING_NPM=$(shell python3 -c "if __import__('pathlib').Path('package-lock.json').exists(): print('yes')")
96
VENV_EXISTS=$(shell python3 -c "if __import__('pathlib').Path('.venv/bin/activate').exists(): print('yes')")
107
NODE_MODULES_EXISTS=$(shell python3 -c "if __import__('pathlib').Path('node_modules').exists(): print('yes')")
11-
PYTHON_PACKAGES=$(shell if poetry --version > /dev/null; then poetry export -f requirements.txt --without-hashes |cut -d'=' -f1 |cut -d ' ' -f1; fi)
128
VERSION := $(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)
139
SRC_DIR=src
1410
BUILD_DIR=dist
@@ -20,9 +16,6 @@ ifndef VERBOSE
2016
endif
2117

2218

23-
REPO_INFO ?= $(shell git config --get remote.origin.url)
24-
COMMIT_SHA ?= git-$(shell git rev-parse --short HEAD)
25-
2619
help: ## Display this help
2720
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
2821

@@ -32,8 +25,6 @@ upgrade: ## Upgrade all dependencies to the latest stable versions
3225
@if [ "$(USING_POETRY)" ]; then poetry update; fi
3326
@echo "Python Dependencies Updated"
3427
@if [ "$(USING_NPM)" ]; then npm upgrade --latest; fi
35-
@if [ "$(USING_YARN)" ]; then yarn upgrade; fi
36-
@if [ "$(USING_PNPM)" ]; then pnpm upgrade --latest; fi
3728
@echo "Node Dependencies Updated"
3829
$(ENV_PREFIX)pre-commit autoupdate
3930
@echo "Updated Pre-commit"
@@ -44,10 +35,8 @@ install: ## Install the project in dev mode.
4435
@if [ "$(VENV_EXISTS)" ]; then echo "Removing existing virtual environment"; fi
4536
@if [ "$(NODE_MODULES_EXISTS)" ]; then echo "Removing existing node environment"; fi
4637
if [ "$(VENV_EXISTS)" ]; then rm -Rf .venv; fi
47-
if [ "$(USING_POETRY)" ]; then poetry config virtualenvs.in-project true --local && poetry config virtualenvs.options.always-copy true --local && python3 -m venv --copies .venv && source .venv/bin/activate && .venv/bin/pip install -U wheel setuptools cython pip && poetry install --with lint,dev,docs && mkdir -p {./src/app/domain/web/public,./src/app/domain/web/resources}; fi
48-
if [ "$(USING_NPM)" ]; then npm ci && npm run build; fi
49-
if [ "$(USING_YARN)" ]; then yarn install && yarn run build; fi
50-
if [ "$(USING_PNPM)" ]; then pnpm install && pnpm run build; fi
38+
if [ "$(USING_POETRY)" ]; then poetry config virtualenvs.in-project true --local && poetry config virtualenvs.options.always-copy true --local && python3 -m venv --copies .venv && source .venv/bin/activate && .venv/bin/pip install -U wheel setuptools cython pip && poetry install --with lint,dev,docs; fi
39+
if [ "$(USING_NPM)" ]; then npm ci; fi
5140
@echo "=> Install complete. ** If you want to re-install re-run 'make install'"
5241

5342

@@ -60,20 +49,20 @@ migrations: ## Generate database migrations
6049
.PHONY: migrate
6150
migrate: ## Generate database migrations
6251
@echo "ATTENTION: Will apply all database migrations."
63-
@env PYTHONPATH=src $(ENV_PREFIX)/app database upgrade-database
52+
@env PYTHONPATH=src $(ENV_PREFIX)app database upgrade-database
6453

6554
.PHONY: squash-migrations
6655
squash-migrations: ## Generate database migrations
6756
@echo "ATTENTION: This operation will wipe all migrations and recreate from an empty state."
6857
@env PYTHONPATH=src $(ENV_PREFIX)app database purge-database --no-prompt
6958
rm -Rf src/app/lib/db/migrations/versions/*.py
7059
@while [ -z "$$MIGRATION_MESSAGE" ]; do read -r -p "Initial migration message: " MIGRATION_MESSAGE; done ;
71-
@env PYTHONPATH=src $(ENV_PREFIX)/alembic -c src/app/lib/db/alembic.ini revision --autogenerate -m "$${MIGRATION_MESSAGE}"
60+
@env PYTHONPATH=src $(ENV_PREFIX)alembic -c src/app/lib/db/alembic.ini revision --autogenerate -m "$${MIGRATION_MESSAGE}"
7261

7362

7463
.PHONY: build
7564
build:
76-
if [ "$(USING_NPM)" ]; then npm run build; fi
65+
@echo "=> Building package..."
7766
if [ "$(USING_POETRY)" ]; then poetry build; fi
7867

7968
.PHONY: test

docker-compose.override.yml

+46-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
11
version: "3.3"
2+
x-development-volumes: &development-volumes
3+
volumes:
4+
- ./docs:/workspace/app/docs/
5+
- ./tests:/workspace/app/tests/
6+
- ./src:/workspace/app/src/
7+
- ./Makefile:/workspace/app/Makefile
8+
- ./pyproject.toml:/workspace/app/pyproject.toml
9+
- ./poetry.lock:/workspace/app/poetry.lock
10+
- ./mkdocs.yml:/workspace/app/mkdocs.yml
11+
- ./tsconfig.json:/workspace/app/tsconfig.json
12+
- ./package.json:/workspace/app/package.json
13+
- ./vite.config.ts:/workspace/app/vite.config.ts
214
services:
15+
localmail:
16+
image: mailhog/mailhog:v1.0.0
17+
container_name: localmail
18+
ports:
19+
- "8025:8025"
320
app:
421
build:
522
context: .
6-
target: build-image
7-
dockerfile: ./Dockerfile
23+
dockerfile: deploy/docker/dev/Dockerfile
824
args:
925
# remove this to build for production.
1026
POETRY_INSTALL_ARGS: --with=dev,docs,lint
11-
ports:
12-
- "8000:8000"
13-
volumes:
14-
- ./docs:/workspace/app/docs/
15-
- ./tests:/workspace/app/tests/
16-
- ./src:/workspace/app/src/
17-
- ./Makefile:/workspace/app/Makefile
18-
- ./pyproject.toml:/workspace/app/pyproject.toml
19-
- ./poetry.lock:/workspace/app/poetry.lock
20-
- ./mkdocs.yml:/workspace/app/mkdocs.yml
21-
command: run-all --reload
22-
23-
localmail:
24-
image: mailhog/mailhog:v1.0.0
25-
container_name: localmail
26-
ports:
27-
- "8025:8025"
27+
image: app:latest-dev
28+
command: litestar run --reload
29+
restart: always
30+
<<: *development-volumes
31+
worker:
32+
image: app:latest-dev
33+
command: litestar worker run
34+
restart: always
35+
<<: *development-volumes
36+
depends_on:
37+
db:
38+
condition: service_healthy
39+
cache:
40+
condition: service_healthy
41+
env_file:
42+
- .env.example
43+
migrator:
44+
image: app:latest-dev
45+
command: litestar database upgrade-database
46+
restart: "no"
47+
<<: *development-volumes
48+
env_file:
49+
- .env.example
50+
depends_on:
51+
db:
52+
condition: service_healthy
53+
cache:
54+
condition: service_healthy

docker-compose.yml

+13-6
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ services:
5151
app:
5252
build:
5353
context: .
54-
dockerfile: Dockerfile
54+
dockerfile: deploy/docker/run/Dockerfile
5555
image: app:latest
5656
restart: always
5757
depends_on:
@@ -63,14 +63,21 @@ services:
6363
- "8000:8000"
6464
env_file:
6565
- .env.example
66-
66+
worker:
67+
image: app:latest
68+
command: litestar worker run
69+
restart: always
70+
depends_on:
71+
db:
72+
condition: service_healthy
73+
cache:
74+
condition: service_healthy
75+
env_file:
76+
- .env.example
6777
migrator:
68-
build:
69-
context: .
70-
dockerfile: Dockerfile
7178
image: app:latest
7279
restart: "no"
73-
command: database upgrade-database
80+
command: litestar database upgrade-database
7481
env_file:
7582
- .env.example
7683
depends_on:

0 commit comments

Comments
 (0)