FROM python:3.13-alpine@sha256:7984cf56ceaddebe4fefeb75d64000b9f45d8b77b11a09b24524da6cdc3af0c8
ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1 RELEASE=v1
WORKDIR /app
RUN addgroup -S -g 10001 app && adduser -S -u 10001 -G app app \
&& mkdir -p /app/data && chown 10001:10001 /app/data && chmod 0750 /app/data
COPY <<'PY' /app/server.py
"""Dependency-free deployment example: release configuration and persistent state."""
import fcntl
import html
import json
import os
import pathlib
import tempfile
import uuid
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from urllib.parse import urlsplit
DATA = pathlib.Path(os.environ.get("DATA_DIR", "/app/data"))
RELEASE = os.environ.get("RELEASE", "v1")[:80]
DATA.mkdir(parents=True, exist_ok=True)
STATE_FILE = DATA / "state.json"
# Fail visibly on corrupt/unwritable data instead of replacing an existing identity.
with (DATA / ".state.lock").open("a") as lock:
fcntl.flock(lock, fcntl.LOCK_EX)
if STATE_FILE.exists():
state = json.loads(STATE_FILE.read_text(encoding="utf-8"))
uuid.UUID(state["instance_id"])
if type(state["start_count"]) is not int or state["start_count"] < 1:
raise ValueError("Invalid persistent start_count")
else:
state = {"instance_id": str(uuid.uuid4()), "start_count": 0}
state["start_count"] += 1
fd, temporary_path = tempfile.mkstemp(prefix=".state-", dir=DATA)
try:
with os.fdopen(fd, "w", encoding="utf-8") as target:
json.dump(state, target, sort_keys=True)
target.write("\n")
target.flush()
os.fsync(target.fileno())
os.replace(temporary_path, STATE_FILE)
directory_fd = os.open(DATA, os.O_RDONLY | os.O_DIRECTORY)
try:
os.fsync(directory_fd)
finally:
os.close(directory_fd)
finally:
if os.path.exists(temporary_path):
os.unlink(temporary_path)
PAGE = """
FibaCloud Deployment Lab
FibaCloud · Deployment Lab
Your application is running.
This small application demonstrates a real deployment, a runtime environment variable, and data that survives a container replacement.
Application ready
Release
__RELEASE__
Read from the RELEASE environment variable when the application starts.
Persistent starts
__COUNT__
Increases once at application startup. Refreshing this page does not change it.
Persistent instance ID
__IDENTITY__Stored in /app/data/state.json on the configured persistent volume.
""".replace("__RELEASE__", html.escape(RELEASE)).replace("__COUNT__", str(state["start_count"])).replace("__IDENTITY__", html.escape(state["instance_id"]))
class Handler(BaseHTTPRequestHandler):
server_version = "DeploymentLab"
sys_version = ""
def do_HEAD(self):
self.do_GET()
def do_GET(self):
path = urlsplit(self.path).path
if path == "/health":
healthy = STATE_FILE.is_file() and os.access(DATA, os.W_OK)
status, content_type, body = (200 if healthy else 503), "application/json", json.dumps({"status": "ok" if healthy else "storage-unavailable"})
elif path == "/api/status":
status, content_type, body = 200, "application/json", json.dumps({"release": RELEASE, **state})
elif path == "/":
status, content_type, body = 200, "text/html; charset=utf-8", PAGE
else:
status, content_type, body = 404, "application/json", '{"error":"not-found"}'
payload = body.encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(payload)))
self.send_header("Cache-Control", "no-store")
self.send_header("X-Content-Type-Options", "nosniff")
self.end_headers()
if self.command != "HEAD":
self.wfile.write(payload)
ThreadingHTTPServer((os.environ.get("BIND_ADDRESS", "0.0.0.0"), int(os.environ.get("PORT", "8080"))), Handler).serve_forever()
PY
USER 10001:10001
EXPOSE 8080
HEALTHCHECK --interval=15s --timeout=3s --start-period=10s --retries=3 CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health', timeout=2).read()"]
CMD ["python", "/app/server.py"]