Skip to content

Commit c7c4d37

Browse files
committed
Docker production script and minor readme changes
1 parent 8ac8d17 commit c7c4d37

File tree

10 files changed

+234
-1
lines changed

10 files changed

+234
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Note that this is a template project. You will need to make changes to this appl
88

99
## Development setup
1010

11-
1. (Optional) Install Pyenv and the Python version for this project:
11+
1. (Optional) Install Pyenv ([Linux](https://github.com/pyenv/pyenv), [Windows](https://github.com/pyenv-win/pyenv-win)) and the [Python installation](.python-version) needed for this project:
1212
```shell
1313
pyenv install
1414
```

TEMPLATE.md

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The project is called `sample-fastapi` and the package is called `sample_fastapi
55

66
If you want to use this template, you will need to rename the package name and project name occurrences to your project name. A good place to start is the `pyproject.toml` file.
77

8+
## How to use the template
9+
10+
(TODO)
11+
812
## Features
913

1014
This Application implements the following:
@@ -28,3 +32,7 @@ This Application implements the following:
2832
- Development setup script to quickly install all dependencies needed.
2933
- A `.env.example` configuration example that needs to be cloned into `.env`
3034
- Examples (Only Docker script for now)
35+
36+
## Pending features to add to repo
37+
38+
- Async database using SQLAlchemy ORM

docker/docker-prod/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Files for generating the Docker image
2+
docker-data/

docker/docker-prod/Dockerfile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Create Docker image for production use.
2+
# This docker file installs the app package and dependencies at run-time.
3+
# A pre-built wheel is packaged with the Docker image, and a requirements.txt file
4+
# along with it to install the package. An entrypoint script does the setup.
5+
# You can speed up installation by mounting pip-cache folder (or just /root/.cache).
6+
7+
FROM python:3.11.9-slim
8+
9+
LABEL maintainer="Narayan Bandodker <[email protected]>"
10+
11+
RUN apt-get update -y && \
12+
apt-get install -y openssl dumb-init
13+
14+
RUN python3 -m pip install --upgrade pip
15+
16+
WORKDIR /app
17+
18+
# Copy container data files
19+
COPY ./docker-data ./
20+
21+
# Copy entrypoint script
22+
COPY ./entrypoint.sh ./dependencies.sh ./
23+
RUN chmod +x ./entrypoint.sh ./dependencies.sh
24+
25+
ENTRYPOINT ["./entrypoint.sh"]

docker/docker-prod/dependencies.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Install any dependencies the app requires (eg. using apt-get).
4+
#
5+
# Add mountpoint to this file to add your dependency setup:
6+
# `docker run -v $(pwd)/dependencies.sh:/app/dependencies.sh ...`
7+
# Or in docker-compose.yml:
8+
#
9+
# ...
10+
# volumes:
11+
# - ./dependencies.sh:/app/dependencies.sh:ro
12+
#

docker/docker-prod/entrypoint.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/dumb-init /bin/sh
2+
3+
echo '[Entrypoint] Installing server package requirements...'
4+
python3 -m pip install --upgrade -r ./requirements.txt
5+
6+
echo '[Entrypoint] Installing other dependencies...'
7+
. ./dependencies.sh
8+
9+
echo '[Entrypoint] Starting app server...'
10+
exec python3 -m uvicorn --factory tredex_server:init_app "$@"

examples/deploy/.env.example

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Database connection "DSN". The app is configured for async use, so use an async library version if possible.
2+
DATABASE_URL = "sqlite+aiosqlite:///dummy-database-please-change.db"
3+
4+
# SQLAlchemy can print debug output of all queries it is executing. Set to `true` to enable this.
5+
ECHO_SQL = false

examples/deploy/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
version: 1
2+
disable_existing_loggers: True
3+
4+
__anchors__:
5+
- &date_format "%Y-%m-%d %H:%M:%S"
6+
7+
__files__:
8+
- &root_log_file "logs/root.log"
9+
- &app_log_file "logs/sample-fastapi.log"
10+
- &uvicorn_error_log_file "logs/sample-fastapi.error.log"
11+
- &uvicorn_access_log_file "logs/sample-fastapi.access.log"
12+
13+
# Logging formatters
14+
formatters:
15+
# Generic formatter
16+
generic_console_fmt:
17+
(): 'uvicorn.logging.DefaultFormatter'
18+
fmt: '%(levelprefix)-9s [%(name)s] (%(filename)s:%(lineno)d) %(message)s'
19+
datefmt: *date_format
20+
21+
generic_file_fmt:
22+
format: '%(levelname)-9s %(asctime)s - [%(name)s] (%(filename)s:%(lineno)d) %(message)s'
23+
datefmt: *date_format
24+
25+
# Uvicorn console formatters
26+
uvicorn_error_console_fmt:
27+
(): 'uvicorn.logging.DefaultFormatter'
28+
fmt: '%(levelprefix)-9s [Uvicorn] %(message)s'
29+
30+
uvicorn_access_console_fmt:
31+
(): 'uvicorn.logging.AccessFormatter'
32+
fmt: '%(levelprefix)-9s [Uvicorn/Access] %(client_addr)s - "%(request_line)s" %(status_code)s'
33+
34+
# Uvicorn log file formatters
35+
uvicorn_error_file_fmt:
36+
format: '%(levelname)-9s %(asctime)s - [Uvicorn] %(message)s'
37+
datefmt: *date_format
38+
39+
uvicorn_access_file_fmt:
40+
(): 'uvicorn.logging.AccessFormatter'
41+
fmt: '%(levelname)-9s %(asctime)s - [Uvicorn/Access] %(client_addr)s - "%(request_line)s" %(status_code)s'
42+
datefmt: *date_format
43+
use_colors: false
44+
45+
46+
handlers:
47+
# Root logger
48+
global_console:
49+
class: logging.StreamHandler
50+
formatter: generic_console_fmt
51+
52+
global_rotating_file:
53+
class: logging.handlers.TimedRotatingFileHandler
54+
level: INFO
55+
formatter: generic_file_fmt
56+
57+
# Arguments
58+
filename: *root_log_file
59+
when: midnight
60+
interval: 1
61+
backupCount: 6
62+
encoding: utf-8
63+
delay: true
64+
65+
sample_fastapi_rotating_file:
66+
class: logging.handlers.TimedRotatingFileHandler
67+
level: INFO
68+
formatter: generic_file_fmt
69+
70+
# Arguments
71+
filename: *app_log_file
72+
when: midnight
73+
interval: 1
74+
backupCount: 6
75+
encoding: utf-8
76+
delay: true
77+
78+
uvicorn_error_console:
79+
class: logging.StreamHandler
80+
formatter: uvicorn_error_console_fmt
81+
82+
# Arguments
83+
stream: ext://sys.stderr
84+
85+
uvicorn_access_console:
86+
class: logging.StreamHandler
87+
formatter: uvicorn_access_console_fmt
88+
89+
# Arguments
90+
stream: ext://sys.stdout
91+
92+
uvicorn_error_file:
93+
class: logging.handlers.TimedRotatingFileHandler
94+
formatter: uvicorn_error_file_fmt
95+
96+
# Arguments
97+
filename: *uvicorn_error_log_file
98+
when: midnight
99+
interval: 1
100+
backupCount: 6
101+
encoding: utf-8
102+
delay: true
103+
104+
uvicorn_access_file:
105+
class: logging.handlers.TimedRotatingFileHandler
106+
formatter: uvicorn_access_file_fmt
107+
108+
# Arguments
109+
filename: *uvicorn_access_log_file
110+
when: midnight
111+
interval: 1
112+
backupCount: 6
113+
encoding: utf-8
114+
delay: true
115+
116+
117+
# Configure root logger
118+
root:
119+
level: INFO
120+
handlers:
121+
- global_console
122+
- global_rotating_file
123+
124+
125+
# Configure loggers by name
126+
loggers:
127+
# Our application package logger tree
128+
sample_fastapi:
129+
level: INFO
130+
handlers:
131+
- sample_fastapi_rotating_file
132+
133+
# Auto-reload messages get annoying, so set it to WARN level
134+
watchfiles.main:
135+
level: WARN
136+
137+
# Re-define Uvicorn loggers
138+
uvicorn:
139+
level: INFO
140+
141+
uvicorn.error:
142+
level: INFO
143+
propagate: False
144+
handlers:
145+
- uvicorn_error_console
146+
- uvicorn_error_file
147+
148+
uvicorn.access:
149+
level: INFO
150+
propagate: False
151+
handlers:
152+
- uvicorn_access_console
153+
- uvicorn_access_file

examples/deploy/docker-compose.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
server:
3+
image: ghcr.io/nb-programmer/sample-fastapi:latest
4+
5+
command:
6+
- "--host=0.0.0.0"
7+
- "--port=8000"
8+
- "--timeout-graceful-shutdown=10"
9+
- "--log-config=config/server-logger-config.yml"
10+
11+
volumes:
12+
- ./config:/app/config:ro
13+
- ./data/logs/server:/app/logs
14+
- ./data/cache:/root/.cache
15+
16+
ports:
17+
- 8000:8000

0 commit comments

Comments
 (0)