{"guide": {"name": "deploying-gradio-with-docker", "category": "other-tutorials", "pretty_category": "Other Tutorials", "guide_index": null, "absolute_index": 46, "pretty_name": "Deploying Gradio With Docker", "content": "# Deploying a Gradio app with Docker\n\n\n\n\n### Introduction\n\nGradio is a powerful and intuitive Python library designed for creating web apps that showcase machine learning models. These web apps can be run locally, or [deployed on Hugging Face Spaces ](https://huggingface.co/spaces)for free. Or, you can deploy them on your servers in Docker containers. Dockerizing Gradio apps offers several benefits:\n\n- **Consistency**: Docker ensures that your Gradio app runs the same way, irrespective of where it is deployed, by packaging the application and its environment together.\n- **Portability**: Containers can be easily moved across different systems or cloud environments.\n- **Scalability**: Docker works well with orchestration systems like Kubernetes, allowing your app to scale up or down based on demand.\n\n## How to Dockerize a Gradio App\n\nLet's go through a simple example to understand how to containerize a Gradio app using Docker.\n\n#### Step 1: Create Your Gradio App\n\nFirst, we need a simple Gradio app. Let's create a Python file named `app.py` with the following content:\n\n```python\nimport gradio as gr\n\ndef greet(name):\n    return f\"Hello {name}!\"\n\niface = gr.Interface(fn=greet, inputs=\"text\", outputs=\"text\").launch()\n```\n\nThis app creates a simple interface that greets the user by name.\n\n#### Step 2: Create a Dockerfile\n\nNext, we'll create a Dockerfile to specify how our app should be built and run in a Docker container. Create a file named `Dockerfile` in the same directory as your app with the following content:\n\n```dockerfile\nFROM python:3.8-slim\n\nWORKDIR /usr/src/app\nCOPY . .\nRUN pip install --no-cache-dir gradio\nEXPOSE 7860\nENV GRADIO_SERVER_NAME=\"0.0.0.0\"\n\nCMD [\"python\", \"app.py\"]\n```\n\nThis Dockerfile performs the following steps:\n- Starts from a Python 3.8 slim image.\n- Sets the working directory and copies the app into the container.\n- Installs Gradio (you should install all other requirements as well).\n- Exposes port 7860 (Gradio's default port).\n- Sets the `GRADIO_SERVER_NAME` environment variable to ensure Gradio listens on all network interfaces.\n- Specifies the command to run the app.\n\n#### Step 3: Build and Run Your Docker Container\n\nWith the Dockerfile in place, you can build and run your container:\n\n```bash\ndocker build -t gradio-app .\ndocker run -p 7860:7860 gradio-app\n```\n\nYour Gradio app should now be accessible at `http://localhost:7860`.\n\n## Important Considerations\n\nWhen running Gradio applications in Docker, there are a few important things to keep in mind:\n\n#### Running the Gradio app on `\"0.0.0.0\"` and exposing port 7860\n\nIn the Docker environment, setting `GRADIO_SERVER_NAME=\"0.0.0.0\"` as an environment variable (or directly in your Gradio app's `launch()` function) is crucial for allowing connections from outside the container. And the `EXPOSE 7860` directive in the Dockerfile tells Docker to expose Gradio's default port on the container to enable external access to the Gradio app. \n\n#### Enable Stickiness for Multiple Replicas\n\nWhen deploying Gradio apps with multiple replicas, such as on AWS ECS, it's important to enable stickiness with `sessionAffinity: ClientIP`. This ensures that all requests from the same user are routed to the same instance. This is important because Gradio's communication protocol requires multiple separate connections from the frontend to the backend in order for events to be processed correctly. (If you use Terraform, you'll want to add a [stickiness block](https://registry.terraform.io/providers/hashicorp/aws/3.14.1/docs/resources/lb_target_group#stickiness) into your target group definition.)\n\n#### Deploying Behind a Proxy\n\nIf you're deploying your Gradio app behind a proxy, like Nginx, it's essential to configure the proxy correctly. Gradio provides a [Guide that walks through the necessary steps](https://www.gradio.app/guides/running-gradio-on-your-web-server-with-nginx). This setup ensures your app is accessible and performs well in production environments.\n\n", "tags": ["DEPLOYMENT", "DOCKER"], "spaces": [], "url": "/guides/deploying-gradio-with-docker/", "contributor": null}}