Raw-fi Data

Deploy Writer Framework with Docker and routing a request via Nginx

2024-11-15

Dockerizing Writer Framework

In this article, we will create a Docker container and deploy the Writer Framework-based web application that displays table data in an image, as introduced in the previous article.

Dockerizing Writer Framework is easy. There is already an instruction page that shows how to do.

In addition to this, installing img2table and tesseract to extract table data from images.

FROM python:3.9-slim
RUN apt-get update -y && mkdir /app
RUN apt-get install build-essential cmake python3-dev tesseract-ocr libtesseract-dev libleptonica-dev libgl1 libglib2.0-0 -y
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT [ "python", "serve.py" ]
EXPOSE 8502
CMD [ ".",  "--port", "8502", "--host", "0.0.0.0" ]

Routing under a path

As same as deploying Streamlit app, we deploy this Writer Framework application and make it accessible via nginx with routing access to this under a certain path.

There is also an instruction page already on how to adjust the request URI.

Following the instructions on this page, I created serve.py to receive requests at a certain path via nginx:

import uvicorn
import writer.serve
from fastapi import FastAPI, Response

root_asgi_app = FastAPI(lifespan=writer.serve.lifespan)
img2csv_app = writer.serve.get_asgi_app("./image2table-app", "run")

root_asgi_app.mount("/img-to-csv", img2csv_app)

@root_asgi_app.get("/")
async def init():
    return Response("""
    <h1>Welcome to the App Hub</h1>
    """)

uvicorn.run(root_asgi_app,
    host="0.0.0.0",
    port=8502,
    log_level="warning",
    ws_max_size=writer.serve.MAX_WEBSOCKET_MESSAGE_SIZE)

That is why ENTRYPOINT in the Dockerfile mentioned above is [ "python", "serve.py" ].

Next, I updated my nginx default.conf:

  location /img-to-csv {
    proxy_pass http://img-to-csv:8502;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

All that's left now is to start the built container and check that it can be accessed via nginx.

Conclusion

I explained how to deploy Writer Framework on your own server and routing a request via Nginx.

In addition to what is introduced in this article, there is a lot of documentation for Writer Framework, so if you have any problems, it is a good idea to first look at the documentation.