[Python] ngrok installationn hangs when using pyngrok in a docker container

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have an API written in FastAPI python and a React frontend that interacts with the API. I need to run an ngrok tunnel using pyngrok for my frontend inside the app container and add the generated url to allow_origins in CORSMiddleware, but when the script to generate the link is run, ngrok starts installing and at 99% the installation hangs and nothing happens. Locally, the script works perfectly, but only in the container such a problem occurs, there are no errors, just the installation does not finish.

    Downloading ngrok: 82%
    Downloading ngrok: 83%
    Downloading ngrok: 84%
    Downloading ngrok: 85%
    Downloading ngrok: 86%
    Downloading ngrok: 87%
    Downloading ngrok: 88%
    Downloading ngrok: 89%
    Downloading ngrok: 90%
    Downloading ngrok: 91%
    Downloading ngrok: 92%
    Downloading ngrok: 93%
    Downloading ngrok: 94%
    Downloading ngrok: 95%
    Downloading ngrok: 96%
    Downloading ngrok: 97%
    Downloading ngrok: 98%
    Downloading ngrok: 99%
    Installing ngrok ...


    I wait for installation 20 min and nothing happens

    This my docker-compose.yml

    version: '3.7'


    services:
    re:
    image: redis
    container_name: re
    command: redis-server --requirepass redistest
    ports:
    - "6379:6379"

    db:
    image: postgres
    restart: always
    container_name: db
    command: -p 5435
    expose:
    - 5435
    ports:
    - "5435:5432"
    environment:
    - POSTGRES_PASSWORD=test
    - POSTGRES_USER=test
    - POSTGRES_DB=test

    api:
    container_name: qrmenu
    image: dchnkoo/qr-sys
    env_file:
    - qr-sys/.env
    ports:
    - "8000:8080"
    - "5555:5555"
    depends_on:
    - db
    - re


    Dockerfile:

    FROM python:3.10.12

    RUN apt-get update && apt-get install -y \
    supervisor \
    && rm -rf /vat/lib/apt/lists/*

    RUN mkdir -p /etc/supervisor/conf.d

    COPY supervisord.conf /etc/supervisor/supervisord.conf

    WORKDIR /qrsystem

    COPY requirements.txt .

    RUN pip install -r requirements.txt

    COPY . .

    RUN chmod +x ./*.sh

    CMD [ "./app.sh" ]


    app.sh:

    #! /bin/bash

    sleep 5

    python3 -m app.SECRET_KEY.generate --set-secret

    sleep 5

    alembic revision --autogenerate

    alembic upgrade head

    /usr/bin/supervisord -c /etc/supervisor/supervisord.conf



    supervisor.conf

    [supervisord]
    nodaemon=true

    [program:ngrok]
    command=python3 -m ngrok.ngrok
    directory=/qrsystem
    autostart=true
    stdout_logfile=/dev/fd/1
    stdout_logfile_maxbytes=0
    stderr_logfile=/dev/fd/2
    stderr_logfile_maxbytes=0

    [program:API]
    command=gunicorn app.API.api:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind=0.0.0.0:8080
    directory=/qrsystem
    autostart=true
    autorestart=true
    stdout_logfile=/var/log/gunicorn.log
    stderr_logfile=/var/log/gunicorn_err.log

    [program:celery]
    command=celery --app=app.framework.celery.object:celery worker -l INFO
    directory=/qrsystem
    autostart=true
    autorestart=true
    stdout_logfile=/var/log/celery.log
    stderr_logfile=/var/log/celery_err.log

    [program:flower]
    command=celery --app=app.framework.celery.object:celery flower
    directory=/qrsystem
    autostart=true
    autorestart=true
    stdout_logfile=/var/log/flower.log
    stderr_logfile=/var/log/flower_err.log


    pyngrok script:

    from pyngrok import ngrok
    from app.settings import origins
    import os


    env = os.environ

    AUTH_TOKEN = env.get("NGROK_AUTHTOKEN", None)

    if AUTH_TOKEN is not None:
    PORT = env.get("NGROK_PORT", "5173")
    HOST = env.get("NGROK_HOST", "127.0.0.1")

    ngrok.set_auth_token(AUTH_TOKEN)

    tunnel = ngrok.connect(f"{HOST}:{PORT}", "http")


    url = tunnel.public_url

    print(f"""\n\n\n\n\n
    ## ## ## ## ## ## ## ## ## ## ## ##
    #### ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ## ## ##
    ## #### ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ##
    ## ## ## ## ## ## ## ## ## ## ## ##

    URL {url} -> http://{HOST}:{PORT}
    \n\n\n\n\n
    """)

    try:
    origins.remove("*")
    except:
    pass

    origins.append(url)

    while True:
    ...


    i tried setting the network_mode for the container api to “host” but that didn't fix the ngrok install problem

    Continue reading...

Compartilhe esta Página