Docker-ify development process

This commit is contained in:
Jack Jackson 2024-03-03 16:23:57 -08:00
parent 817f52b359
commit 6f0e7cd593
4 changed files with 26 additions and 65 deletions

View File

@ -1,10 +1,3 @@
# Run locally
In increasing complexity:
* `./basic-run.sh` - just raw-dog it
* `docker compose up --build`
# Pre-commit
This repo uses [pre-commit](https://pre-commit.com/). Install the tool with `pip install pre-commit` (or just `pip install -r requirements-dev.txt`), then install the hooks with `pre-commit install`

View File

@ -1,9 +1,5 @@
# syntax=docker/dockerfile:1
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/go/dockerfile-reference/
ARG PYTHON_VERSION=3.9.6
FROM python:${PYTHON_VERSION}-slim as base
@ -36,12 +32,6 @@ RUN --mount=type=cache,target=/root/.cache/pip \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
python -m pip install -r requirements.txt
# Switch to the non-privileged user to run the application.
USER appuser
# Copy the source code into the container.
COPY . .
# Create writeable directory for database
USER root
RUN mkdir database
@ -49,7 +39,25 @@ RUN chmod 755 database
RUN chown appuser:appuser database
USER appuser
# Expose the port that the application listens on.
EXPOSE 8000
########
# Targets diverge from here
########
FROM base as prod
COPY . .
CMD uvicorn app:app --host 0.0.0.0
###
FROM base as dev
# You probably want sqlite3 to poke around with the database anyway
USER root
RUN apt update
RUN apt install sqlite3
USER appuser
# Expects that the source code will be mounted into the container
CMD uvicorn app:app --reload --host 0.0.0.0

View File

@ -1,5 +0,0 @@
#!/usr/bin/env bash
# Idempotent
source .venv/bin/activate
uvicorn app:app --reload --log-config ./local-run-log-config.yaml

View File

@ -1,49 +1,14 @@
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker compose reference guide at
# https://docs.docker.com/go/compose-spec-reference/
# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
# This Docker Compose is intended for development, therefore it loads the code directories into the container.
services:
server:
build:
context: .
target: dev
volumes:
- type: bind
source: ./app/
# Yes, really - we're using `/app` as the WD within the container, but `uvicorn` requires an import path.
target: /app/app
ports:
- 8000:8000
# The commented out section below is an example of how to define a PostgreSQL
# database that your application can use. `depends_on` tells Docker Compose to
# start the database before your application. The `db-data` volume persists the
# database data between container restarts. The `db-password` secret is used
# to set the database password. You must create `db/password.txt` and add
# a password of your choosing to it before running `docker compose up`.
# depends_on:
# db:
# condition: service_healthy
# db:
# image: postgres
# restart: always
# user: postgres
# secrets:
# - db-password
# volumes:
# - db-data:/var/lib/postgresql/data
# environment:
# - POSTGRES_DB=example
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
# expose:
# - 5432
# healthcheck:
# test: [ "CMD", "pg_isready" ]
# interval: 10s
# timeout: 5s
# retries: 5
# volumes:
# db-data:
# secrets:
# db-password:
# file: db/password.txt