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
Answers
It seems that the previous Gradio, which was launched without a docker-compose, is still working. Have you stopped other containers using Port 7860?
docker-compose.yml
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 withdocker compose
.browser: