Skip to main content
Docker provides a consistent way to package and deploy Gradio apps across different environments. This guide shows you how to containerize your Gradio applications and deploy them anywhere.

Why use Docker?

Deploying Gradio apps with Docker offers several advantages:
  • Consistency: Your app runs identically across development, staging, and production
  • Portability: Move containers between local machines, cloud providers, and servers
  • Scalability: Use orchestration tools like Kubernetes to scale horizontally
  • Isolation: Dependencies are contained within the image
  • Reproducibility: Anyone can run your app with a single command

Prerequisites

Before you begin:
  • Install Docker Desktop (includes Docker Engine and Docker Compose)
  • Basic familiarity with Docker concepts
  • A working Gradio application

Quick start

Create a simple Gradio app and containerize it:
1

Create your Gradio app

Create app.py:
2

Create a Dockerfile

Create Dockerfile in the same directory:
3

Create requirements.txt

List your Python dependencies:
4

Build and run

Build the Docker image:
Run the container:
Access your app at http://localhost:7860

Dockerfile explained

Let’s break down the Dockerfile:

Key elements

  • FROM: Base image (use python:3.10-slim for smaller size)
  • WORKDIR: Sets /app as the working directory
  • COPY requirements.txt: Copies dependencies first (better caching)
  • RUN pip install: Installs Python packages
  • COPY .: Copies all app files
  • EXPOSE 7860: Documents that the app listens on port 7860
  • ENV GRADIO_SERVER_NAME: Makes Gradio accept external connections
  • CMD: Command to run when container starts
Setting GRADIO_SERVER_NAME="0.0.0.0" is crucial - it allows connections from outside the container. Without this, you won’t be able to access the app.

Production-ready Dockerfile

For production deployments, use this enhanced Dockerfile:
Improvements:
  • Non-root user for security
  • System dependencies handling
  • Health check for monitoring
  • Proper file permissions

Multi-stage builds

Reduce image size with multi-stage builds:

Docker Compose

For apps with multiple services (database, cache, etc.):
Run with:

Environment variables

Manage configuration with environment variables:

In Dockerfile

At runtime

Using .env file

Create .env:
Run with:

In your app

Volume mounting

Persist data between container restarts:

Mount data directory

Mount model cache

GPU support

For apps using GPU models:

Dockerfile with CUDA

Run with GPU

Requires NVIDIA Container Toolkit installed on the host.

Deployment scenarios

AWS ECS

  1. Push image to ECR:
  2. Create ECS task definition with your image
  3. Create ECS service
  4. Configure load balancer with session stickiness
Enable session stickiness on your load balancer! Gradio requires multiple connections from the same client to route to the same instance. Set sessionAffinity: ClientIP or equivalent.

Google Cloud Run

  1. Build and push:
  2. Deploy:

Azure Container Instances

DigitalOcean App Platform

  1. Push to Docker Hub or DigitalOcean Container Registry
  2. Create new app in App Platform
  3. Select Docker Hub as source
  4. Configure HTTP port: 7860
  5. Deploy

Behind a reverse proxy

When deploying behind Nginx or similar:

Nginx configuration

Docker Compose with Nginx

Best practices

1

Use .dockerignore

Create .dockerignore to exclude unnecessary files:
2

Pin dependency versions

In requirements.txt:
3

Use specific base image tags

Instead of python:3.10, use python:3.10.13-slim
4

Minimize layers

Combine RUN commands:
5

Add health checks

Monitor container health:

Troubleshooting

Can’t access app from browser

  • Ensure GRADIO_SERVER_NAME="0.0.0.0" is set
  • Verify port mapping: -p 7860:7860
  • Check firewall rules

Out of memory

  • Increase Docker memory limit in Docker Desktop settings
  • Use smaller base image (-slim or -alpine)
  • Reduce model size or use quantization

Slow builds

  • Use .dockerignore
  • Order Dockerfile commands from least to most frequently changed
  • Use multi-stage builds
  • Enable BuildKit: DOCKER_BUILDKIT=1 docker build

Container exits immediately

  • Check logs: docker logs <container-id>
  • Verify app.py runs locally
  • Ensure all dependencies are in requirements.txt

Next steps

Hugging Face Spaces

Deploy to managed infrastructure

Sharing apps

Add authentication and embedding