Deploying on Docker

In this article we will deploy a simple Lona script using Docker and optionally Docker Compose.

See the lona-project-template for an example on how to run a Lona Project in Docker.

This article assumes that all files (hello-world.py, Dockerfile and optionally docker-compose.yml) are in the same directory, and all commands are run in this directory.

# hello-world.py

from lona_picocss import install_picocss

from lona.html import HTML, H1
from lona import View, App

app = App(__file__)

install_picocss(app)  # optional

app.settings.MAX_RUNTIME_THREADS = 50
app.settings.MAX_WORKER_THREADS = 100
app.settings.MAX_STATIC_THREADS = 20


@app.route('/')
class Index(View):
    def handle_request(self, request):
        return HTML(
            H1('Hello World'),
        )


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
# Dockerfile

FROM python:3.11

RUN pip install lona lona-picocss  # lona-picocss is optional

COPY hello-world.py .

CMD ["python", "hello-world.py"]

Run these two commands:

docker build -t lona-hello-world .
docker run -it -p 8080:8080 lona-hello-world

Docker Compose

To use Docker Compose, add this this config to the same directory.

# docker-compose.yml

version: "3"

services:
    app:
        build:
            context: ./
            dockerfile: Dockerfile

        volumes:
            - ./:/app

        working_dir: "/app"

        ports:
            - 8080:8080

        command: ["python", "hello-world.py"]

Then run this command:

docker compose up  # on your system it might be 'docker-compose'