Use Ngrok with Docker and Docker Compose
Intermediatev1.0.0
Integrate ngrok with Docker Compose — expose containerized services, configure tunnels as containers, and set up development environments with automatic tunnel creation.
Content
Overview
Run ngrok as a Docker container alongside your services in Docker Compose. This creates tunnels automatically when you start your development environment, with no manual ngrok commands needed.
Why This Matters
- -One command setup —
docker compose upstarts everything including tunnels - -Consistent environment — tunnel configuration is part of the stack
- -Team onboarding — new developers get tunnels automatically
- -Service discovery — ngrok container connects to other containers by name
How It Works
Step 1: Docker Compose with Ngrok
Step 2: Ngrok Configuration for Docker
Step 3: Get Tunnel URL Programmatically
Step 4: Use Environment Variables
Best Practices
- -Use Docker service names (app, api) as tunnel addresses
- -Mount ngrok.yml as a volume for complex configurations
- -Expose port 4040 for the inspection UI
- -Use environment variables for authtoken (not in docker-compose.yml)
- -Add ngrok as a depends_on for the service it tunnels
- -Use .env file for secrets (gitignored)
Common Mistakes
- -Using localhost:3000 instead of service-name:3000 in Docker
- -Committing ngrok authtoken in docker-compose.yml
- -Not exposing port 4040 (no access to inspection UI)
- -Missing depends_on (ngrok starts before the app is ready)
- -Forgetting to gitignore .env file with authtoken
FAQ
Discussion
Loading comments...