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:7860Dockerfile explained
Let’s break down the Dockerfile:Key elements
- FROM: Base image (use
python:3.10-slimfor smaller size) - WORKDIR: Sets
/appas 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:- 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.):Environment variables
Manage configuration with environment variables:In Dockerfile
At runtime
Using .env file
Create.env:
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
-
Push image to ECR:
- Create ECS task definition with your image
- Create ECS service
- Configure load balancer with session stickiness
Google Cloud Run
-
Build and push:
-
Deploy:
Azure Container Instances
DigitalOcean App Platform
- Push to Docker Hub or DigitalOcean Container Registry
- Create new app in App Platform
- Select Docker Hub as source
- Configure HTTP port: 7860
- 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-slim4
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 (
-slimor-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.pyruns locally - Ensure all dependencies are in
requirements.txt
Next steps
Hugging Face Spaces
Deploy to managed infrastructure
Sharing apps
Add authentication and embedding