1

I want to use a static HTML page in my docker to show logfiles via websockets and FastAPI inside a docker container. As FastAPI has already a webserver, I mount my static page with:

root_path = "/qgis"
app = FastAPI(
    title="API",
    description="API",
    version=VERSION,
    root_path=root_path
)
app.mount("/static", StaticFiles(directory="static"), name="static")

My folder structure is:

/api
|- main.py 
|- Dockerfile
|- docker-compose.yml
|-static 
    |- index.html 
    |- style.css
    |- Bilder
        |-.... 

My docker-compose.yml contains:

services:
  api:
    image: api:entw
    build:
      context: .
      dockerfile: Dockerfile
    restart: unless-stopped
    volumes:
      - ./static:/app/static:rw
    networks:
      geonet_entw:
    ports:
     - "8000:8000"

networks:
  entw:
    external: true

However, when I start with https://my.url.de/qgis/docs I see all data provided by Swagger and can use them. If I use https://my.url.de/qgis/static/index.html I get an Error 404.

If I use putty to go inside the docker folder with docker exec -it docker_api sh and use lsI see the folder static with all data included.

Edit

When I use:

python3 -c "import urllib.request as r; print(r.urlopen('http://localhost:8000/qgis/static/index.html').read().decode())"

Inside the container (accessed via docker exec -it docker_api sh) I get the error:

urllib.error.URLError: <urlopen error [Errno 111] Connection refused>

From outside the container, I used curl -l https://my.url.de/qgis/static/index.html I get the message:

HTTP/1.1 404 Not Found
Server: nginx
...
9
  • What if you use FastAPI(root_path="/qgis") and app.mount("/qgis/static", ...) Commented Apr 8 at 10:05
  • Thanks @AliSheikhpour I just changed the code to app.mount("/qgis/static", StaticFiles(directory="static"), name="static") and restarted the container, but the page my.url.de/qgis/static/index.html still shows a 404 Commented Apr 8 at 10:30
  • also make sure your app was initialized with app = FastAPI(root_path="/qgis") Commented Apr 8 at 10:42
  • Sorry, this part I missed, I just edited it in the code above. I have app = FastAPI(...root_path=root_path) in my code. Commented Apr 8 at 12:12
  • 1
    @Chris this seems helpful as I could set html=True Commented Apr 9 at 8:08

1 Answer 1

0

I think if you're running from the /api folder, and change your docker mount to /api/static this should work (this is a separate question though).

I set up something similar to your example and it's fine. Mine uses an absolute path but it works with a relative one too.

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles

root_path = "/qgis"
app = FastAPI(title="API", description="API", version="0.0.1", root_path=root_path)
#            \/ this is the web path            \/ this is the filesystem
app.mount("/static", StaticFiles(directory="/api/static", html=True), name="static")

@app.get("/helloworld", tags=["Debug"])
def hello_world():
    return {"hello": "world"}
(venv) [0s] 1053 static-test-case$ podman container ls
CONTAINER ID  IMAGE                        COMMAND               CREATED        STATUS        PORTS                   NAMES
53d227cdac3a  localhost/test/static:0.0.1  fastapi dev /api/...  2 minutes ago  Up 2 minutes  0.0.0.0:8000->8000/tcp  static-test-case_api_1

(venv) [0s] 1055 static-test-case$ podman exec -it static-test-case_api_1 bash
53d227cdac3a:/api# curl localhost:8000/qgis/static/
<html>
    <body>
        <ul>
            <li><a href="a.jpg">a.jpg</a></li>
            <li><a href="b.txt">b.txt</a></li>
            
        </ul>
    </body>
</html>
53d227cdac3a:/api# curl localhost:8000/qgis/static/b.txt
test file
53d227cdac3a:/api# curl localhost:8000/qgis/helloworld
{"hello":"world"}

Dockerfile - this has a possible solution as well.

FROM python:3.13-alpine3.21

RUN mkdir /api
RUN mkdir /api/static

COPY ./requirements.txt /api/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /api/requirements.txt

RUN apk add -f --no-cache bash vim curl

COPY ./*.py /api/
COPY ./run.sh /api/

COPY ./static/* /api/static

EXPOSE 8000

WORKDIR /api

# this could also be your problem
# by default, dev mode binds to 127.0.0.1, which is not visible outside the container
# use 0.0.0.0 instead
CMD ["fastapi", "dev", "/api/main.py", "--port", "8000", "--host", "0.0.0.0"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.