Skip to content

Commit 2d09f66

Browse files
committed
This commit adds a new configuration setting for granian, updates dependencies for granian and typer packages, adds Docker Compose commands for deployment and container management and updates dependencies to their latest versions.
- Added new configuration setting for granian, setting it to "*" - Updated versions of granian and typer packages in Dockerfile dependencies - Added Docker Compose commands for managing containers, building and running in detached mode, entering Bash Shell for migrations, and viewing container logs - Updated dependencies in requirements file to include latest versions of click, django, granian, orjson, and typer while removing older versions not needed
1 parent 15ecb4a commit 2d09f66

File tree

8 files changed

+281
-6
lines changed

8 files changed

+281
-6
lines changed

.dockerignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generated by docker init
2+
# Include any files or directories that you don't want to be copied to your
3+
# container here (e.g., local build artifacts, temporary files, etc.).
4+
#
5+
# For more help, visit the .dockerignore file reference guide at
6+
# https://docs.docker.com/go/build-context-dockerignore/
7+
8+
9+
**/.DS_Store
10+
**/__pycache__
11+
**/.venv
12+
**/.classpath
13+
**/.dockerignore
14+
# **/.env
15+
**/.git
16+
**/.gitignore
17+
**/.project
18+
**/.settings
19+
**/.toolstarget
20+
**/.vs
21+
**/.vscode
22+
**/*.*proj.user
23+
**/*.dbmdl
24+
**/*.jfm
25+
**/bin
26+
**/charts
27+
**/docker-compose*
28+
**/compose*
29+
**/Dockerfile*
30+
**/node_modules
31+
**/npm-debug.log
32+
**/obj
33+
**/secrets.dev.yaml
34+
**/values.dev.yaml
35+
LICENSE
36+
README.md

Docker_HOWTO.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- Generated by docker init -->
2+
### Building and running your application
3+
4+
When you're ready, start your application by running:
5+
`docker compose up --build`.
6+
7+
Your application will be available at http://localhost:8000.
8+
9+
### Deploying your application to the cloud
10+
11+
First, build your image, e.g.: `docker build -t myapp .`.
12+
If your cloud uses a different CPU architecture than your development
13+
machine (e.g., you are on a Mac M1 and your cloud provider is amd64),
14+
you'll want to build the image for that platform, e.g.:
15+
`docker build --platform=linux/amd64 -t myapp .`.
16+
17+
Then, push it to your registry, e.g. `docker push myregistry.com/myapp`.
18+
19+
Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/)
20+
docs for more detail on building and pushing.
21+
22+
### References
23+
* [Docker's Python guide](https://docs.docker.com/language/python/)

Dockerfile

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Generated by docker init
2+
# syntax=docker/dockerfile:1
3+
4+
# Comments are provided throughout this file to help you get started.
5+
# If you need more help, visit the Dockerfile reference guide at
6+
# https://docs.docker.com/go/dockerfile-reference/
7+
8+
# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7
9+
10+
ARG PYTHON_VERSION=3.11
11+
FROM python:${PYTHON_VERSION}-slim as base
12+
13+
# Prevents Python from writing pyc files.
14+
ENV PYTHONDONTWRITEBYTECODE=1
15+
16+
# Keeps Python from buffering stdout and stderr to avoid situations where
17+
# the application crashes without emitting any logs due to buffering.
18+
ENV PYTHONUNBUFFERED=1
19+
20+
WORKDIR /app
21+
22+
# Create a non-privileged user that the app will run under.
23+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
24+
ARG UID=10001
25+
RUN adduser \
26+
--disabled-password \
27+
--gecos "" \
28+
--home "/nonexistent" \
29+
--shell "/sbin/nologin" \
30+
--no-create-home \
31+
--uid "${UID}" \
32+
appuser
33+
34+
# Download dependencies as a separate step to take advantage of Docker's caching.
35+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
36+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
37+
# into this layer.
38+
RUN --mount=type=cache,target=/root/.cache/pip \
39+
--mount=type=bind,source=requirement.txt,target=requirement.txt \
40+
python -m pip install -r requirement.txt
41+
42+
# Switch to the non-privileged user to run the application.
43+
USER appuser
44+
45+
# Copy the source code into the container.
46+
COPY . .
47+
48+
# Expose the port that the application listens on.
49+
EXPOSE 8000
50+
51+
52+
# Run the application.
53+
# For testing
54+
# CMD ["python", "apidemo/manage.py", "runserver", "0.0.0.0:8000"]
55+
# For WSGI interface
56+
CMD granian --interface wsgi apidemo/apidemo.wsgi:application --host 0.0.0.0 --port 8000 --workers 4 --threading-mode workers --http auto --log --log-level info
57+
# For ASGI interface
58+
# CMD granian --interface asgi apidemo/apidemo.asgi:application --host 0.0.0.0 --port 8000 --workers 4 --threading-mode workers --http auto --log --log-level info

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ python-dotenv = "*"
1818
sqlparse = "==0.4.4"
1919
typing-extensions = "==4.9.0"
2020
tzdata = "*"
21+
granian = "*"
2122

2223
[dev-packages]
2324

Pipfile.lock

Lines changed: 84 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
# django_ninja_tutorial
22
Django Ninja with SQLite
33
Basic CRUD API for Employees and Department using Django Ninja.
4+
Deployed using Granian
5+
# Docker Compose Commands
6+
7+
### Get running containers:
8+
**docker ps**
9+
10+
### Build and Run in detached mode:
11+
**docker compose up --build -d**
12+
13+
### To enter Bash Shell for migrating db changes or static files
14+
**docker exec -it <container id from docker ps> bash**
15+
16+
### To view logs of container. You can pass head to view first N lines or tail to view last N lines specified by argument after head/tail
17+
18+
**docker logs --tail 1000 -f <container id from docker ps>**

docker-compose.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Generated by docker init
2+
# Comments are provided throughout this file to help you get started.
3+
# If you need more help, visit the Docker Compose reference guide at
4+
# https://docs.docker.com/go/compose-spec-reference/
5+
6+
# Here the instructions define your application as a service called "server".
7+
# This service is built from the Dockerfile in the current directory.
8+
# You can add other services your application may depend on here, such as a
9+
# database or a cache. For examples, see the Awesome Compose repository:
10+
# https://github.com/docker/awesome-compose
11+
services:
12+
server:
13+
build:
14+
context: .
15+
env_file:
16+
- .env
17+
ports:
18+
- 8000:8000
19+
healthcheck:
20+
test: ["CMD", "python", "apidemo/manage.py", "check"]
21+
interval: 1m30s
22+
timeout: 30s
23+
retries: 5
24+
start_period: 30s
25+
26+
27+
# The commented out section below is an example of how to define a PostgreSQL
28+
# database that your application can use. `depends_on` tells Docker Compose to
29+
# start the database before your application. The `db-data` volume persists the
30+
# database data between container restarts. The `db-password` secret is used
31+
# to set the database password. You must create `db/password.txt` and add
32+
# a password of your choosing to it before running `docker compose up`.
33+
# depends_on:
34+
# db:
35+
# condition: service_healthy
36+
# db:
37+
# image: postgres
38+
# restart: always
39+
# user: postgres
40+
# secrets:
41+
# - db-password
42+
# volumes:
43+
# - db-data:/var/lib/postgresql/data
44+
# environment:
45+
# - POSTGRES_DB=example
46+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
47+
# expose:
48+
# - 5432
49+
# healthcheck:
50+
# test: [ "CMD", "pg_isready" ]
51+
# interval: 10s
52+
# timeout: 5s
53+
# retries: 5
54+
# volumes:
55+
# db-data:
56+
# secrets:
57+
# db-password:
58+
# file: db/password.txt
59+

requirement.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
-i https://pypi.org/simple
22
annotated-types==0.6.0; python_version >= '3.8'
33
asgiref==3.7.2; python_version >= '3.7'
4+
click==8.1.7; python_version >= '3.7'
45
colorama==0.4.6; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6'
5-
django==5.0.2; python_version >= '3.10'
6+
django==5.0.3; python_version >= '3.10'
67
django-ninja==1.1.0; python_version >= '3.7'
7-
orjson==3.9.13; python_version >= '3.8'
8+
granian==1.1.1; python_version >= '3.8'
9+
orjson==3.9.15; python_version >= '3.8'
810
packaging==23.2; python_version >= '3.7'
911
pltable==1.1.0
1012
pydantic==2.5.3; python_version >= '3.7'
1113
pydantic-core==2.14.6; python_version >= '3.7'
1214
python-dotenv==1.0.1; python_version >= '3.8'
1315
sqlparse==0.4.4; python_version >= '3.5'
16+
typer==0.9.0; python_version >= '3.6'
1417
typing-extensions==4.9.0; python_version >= '3.8'
1518
tzdata==2024.1; python_version >= '2'

0 commit comments

Comments
 (0)