Skip to content

Commit af9e3bf

Browse files
committed
add docker
1 parent 71f6e8f commit af9e3bf

File tree

8 files changed

+110
-15
lines changed

8 files changed

+110
-15
lines changed

.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.git/
2+
.github/
3+
types_/
4+
config.template.toml
5+
Dockerfile
6+
LICENSE
7+
migration.sql
8+
pyproject.toml
9+
README.md

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
POSTGRES_USER=mystbin
2+
POSTGRES_PASSWORD=mystbin

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM python:3.12-slim
2+
3+
LABEL org.opencontainers.image.source=https://github.com/pythonistaguild/mystbin-backend
4+
LABEL org.opencontainers.image.description="Mystbin's Python Backend"
5+
LABEL org.opencontainers.image.licenses=GPLv3
6+
7+
RUN mkdir -p /etc/apt/keyrings \
8+
&& apt update -y \
9+
&& apt-get install --no-install-recommends -y \
10+
# deps for building python deps
11+
git \
12+
build-essential \
13+
libcurl4-gnutls-dev \
14+
gnutls-dev \
15+
libmagic-dev \
16+
&& rm -rf /var/lib/apt/lists/*
17+
18+
# copy project requirement files here to ensure they will be cached.
19+
WORKDIR /app
20+
COPY requirements.txt ./
21+
22+
# install runtime deps
23+
RUN pip install -Ur requirements.txt
24+
25+
COPY . /app/
26+
ENTRYPOINT python -O launcher.py

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Easily share code and text.
66
[Staging Website](https://staging.mystb.in)
77

88

9-
### Running Locally
9+
### Running without Docker
1010
**Requirements:**
1111
- Postgres
1212

@@ -20,3 +20,17 @@ Easily share code and text.
2020
- Install dependencies (Preferably to a `venv`): `pip install -Ur requirements.txt`
2121
- Optionally in `core/server.py` set `ignore_localhost=` to `False` in the RateLimit Middleware for testing.
2222
- Run: `python launcher.py`
23+
24+
### Running with Docker
25+
**Requirements**
26+
- Docker
27+
- Docker Compose
28+
29+
**Setup:**
30+
- Clone
31+
- Copy `config.template.toml` into `config.toml`
32+
- The default config for database (and redis) should work Out of Box.
33+
- Optionally in `core/server.py` set `ignore_localhost=` to `False` in the RateLimit Middleware for testing.
34+
- Run `docker compose up -d` to start the services.
35+
- If you want to use redis for session/limit handling, run with the redis profile: `docker compose --profile redis up -d`
36+
- The redis container doesn't expose connections outside of the network, but for added security edit `redis.conf` and change the password.

config.template.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
host = "localhost"
33
port = 8181
44
domain = "https://mystb.in"
5-
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
5+
session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64))
66

77
[DATABASE]
8-
dsn = "postgres://USER:PASSWORD@localhost:5432/mystbin"
8+
dsn = "postgres://mystbin:mystbin@database:5432/mystbin"
99

1010
[REDIS]
11-
limiter = "" # Optional
12-
sessions = "" # Optional
11+
limiter = "redis://redis:6739/0" # Optional
12+
sessions = "redis://redis:6739/1" # Optional
1313

1414
[LIMITS]
15-
paste_get = {rate=30, per=60, priority=1, bucket="ip"}
16-
paste_get_day = {rate=7200, per=86400, priority=2, bucket="ip"}
17-
paste_post = {rate=10, per=60, priority=1, bucket="ip"}
18-
paste_post_day = {rate=1440, per=86400, priority=2, bucket="ip"}
19-
global_limit = {rate=21600, per=86400, priority=1, bucket="ip"}
15+
paste_get = { rate = 30, per = 60, priority = 1, bucket = "ip" }
16+
paste_get_day = { rate = 7200, per = 86400, priority = 2, bucket = "ip" }
17+
paste_post = { rate = 10, per = 60, priority = 1, bucket = "ip" }
18+
paste_post_day = { rate = 1440, per = 86400, priority = 2, bucket = "ip" }
19+
global_limit = { rate = 21600, per = 86400, priority = 1, bucket = "ip" }
2020

2121
[PASTES]
2222
char_limit = 300_000
2323
file_limit = 5
24-
name_limit = 25
24+
name_limit = 25

docker-compose.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
services:
2+
mystbin:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: mystbin
7+
ports:
8+
- 8181:8181
9+
restart: unless-stopped
10+
depends_on:
11+
database:
12+
condition: service_healthy
13+
restart: true
14+
15+
database:
16+
image: postgres:latest
17+
container_name: mystbin-database
18+
restart: unless-stopped
19+
healthcheck:
20+
test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"]
21+
interval: 10s
22+
timeout: 5s
23+
retries: 5
24+
env_file: .env
25+
environment:
26+
- PG_DATA=/var/lib/postgresql/data
27+
- POSTGRES_DB=mystbin
28+
volumes:
29+
- mystbin_pg_data:/var/lib/postgresql/data
30+
31+
redis:
32+
image: redis:latest
33+
container_name: mystbin-redis
34+
restart: unless-stopped
35+
profiles:
36+
- redis
37+
volumes:
38+
- "./redis.conf:/config/redis.conf:ro"
39+
command: ["redis-server", "/config/redis.conf"]
40+
41+
volumes:
42+
mystbin_pg_data:

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tool.ruff]
22
line-length = 120
33
indent-width = 4
4-
exclude = ["venv"]
4+
exclude = ["venv", ".venv"]
55

66
[tool.ruff.lint]
77
select = [
@@ -34,7 +34,7 @@ ignore = [
3434
"ANN401",
3535
"UP031",
3636
"PTH123",
37-
"E203",
37+
"E203",
3838
"E501",
3939
]
4040

@@ -56,9 +56,9 @@ skip-magic-trailing-comma = false
5656
line-ending = "auto"
5757

5858
[tool.pyright]
59-
exclude = ["venv"]
59+
exclude = ["venv", ".venv"]
6060
useLibraryCodeForTypes = true
6161
typeCheckingMode = "strict"
6262
reportImportCycles = false
6363
reportPrivateUsage = false
64-
pythonVersion = "3.11"
64+
pythonVersion = "3.11"

redis.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
port 6379
2+
requirepass ChangeMeHere!!

0 commit comments

Comments
 (0)