0

I have a simple gradio app that runs fine inside Docker, but fails when launched via docker-compose.

app.py

import gradio as gr


def greet(name):
    return f"Hello {name}!"


iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch(server_name="0.0.0.0", server_port=7860)

Dockerfile

FROM python:3.12-slim

WORKDIR /app

COPY . /app

RUN pip install -r requirements.txt

ENV GRADIO_SERVER_PORT=7860
ENV GRADIO_SERVER_NAME=0.0.0.0
EXPOSE 7860

CMD ["python", "app.py"]

I created then Dockerfile, and built it using docker build -t my_gradio .

Then launched it using docker run -p 7860:7860 -e GRADIO_SERVER_NAME="0.0.0.0" my_gradio

So far so good; I can access the gradio UI on localhost:7860

docker-compose.yaml

services:  
  my_gradio:
    build: .
    ports:
      - "7860:7860"
    networks:
      - net
    environment:
      - GRADIO_SERVER_NAME="0.0.0.0"
      - GRADIO_SERVER_PORT=7860

networks:
  net:

When I run this docker-compose.yaml file, I get an error

Attaching to my_gradio-1
my_gradio-1  | Traceback (most recent call last):
my_gradio-1  |   File "/app/app.py", line 9, in <module>
my_gradio-1  |     iface.launch(server_name="0.0.0.0", server_port=7860)
my_gradio-1  |   File "/usr/local/lib/python3.12/site-packages/gradio/blocks.py", line 2368, in launch
my_gradio-1  |     ) = http_server.start_server(
my_gradio-1  |         ^^^^^^^^^^^^^^^^^^^^^^^^^
my_gradio-1  |   File "/usr/local/lib/python3.12/site-packages/gradio/http_server.py", line 154, in start_server
my_gradio-1  |     raise OSError(
my_gradio-1  | OSError: Cannot find empty port in range: 7860-7860. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.
my_gradio-1 exited with code 1

I launched it using docker compose -f docker-compose.yaml up --build

requirements.txt

gradio

I do not understand what is the problem ! The app works fine using simple Dockerfile, but does not work when using docker-compose.

I tried changing the port, setting a range of ports and still get the same problem !

Please help !

2
  • 1
    There's extra double quotes in the GRADIO_SERVER_NAME setting, and in the Compose syntax it's possible these are getting included in the environment variable value the application sees. Does deleting these quotes help? Commented Sep 3, 2024 at 10:50
  • I have never thought that docker-compose treats double-quotes differelently. You were right :) Thank you very much ! Commented Sep 3, 2024 at 12:30

1 Answer 1

1

docker-compose.yml

services:
  my_gradio:
    build: .
    ports:
      - "7860:7860"
    networks:
      - net
    environment:
      - GRADIO_SERVER_NAME=0.0.0.0
      - GRADIO_SERVER_PORT=7860

networks:
  net:

i tested in my machine and as per the comment removing the double quote from GRADIO_SERVER_NAME value worked fine and the app was running fine with docker compose.

enter image description here

browser: enter image description here

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.