|
| 1 | +# Caddy Docker Proxy Demo |
| 2 | + |
| 3 | +This project demonstrates how to use Docker labels to dynamically configure Caddy as a reverse proxy for multiple services. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Automatic service discovery |
| 8 | +- Dynamic reverse proxy configuration |
| 9 | +- Multiple services with automatic HTTPS |
| 10 | +- Global middleware support |
| 11 | +- Health checks |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- Docker and Docker Compose |
| 16 | +- sudo access (for modifying /etc/hosts) |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +1. Run the setup script (requires sudo): |
| 21 | + ```bash |
| 22 | + sudo make setup |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Start the services: |
| 26 | + ```bash |
| 27 | + make up |
| 28 | + ``` |
| 29 | + |
| 30 | +3. Access the services: |
| 31 | + - Web: [https://web.demo.local](https://web.demo.local) |
| 32 | + - API: [https://api.demo.local](https://api.demo.local) |
| 33 | + |
| 34 | +## Available Commands |
| 35 | + |
| 36 | +- `make up` - Start all services in detached mode |
| 37 | +- `make down` - Stop and remove all containers |
| 38 | +- `make logs` - View container logs |
| 39 | +- `make restart` - Restart all services |
| 40 | +- `make setup` - Initial setup (modifies /etc/hosts) |
| 41 | +- `make clean` - Remove containers, networks, and volumes |
| 42 | + |
| 43 | +## Project Structure |
| 44 | + |
| 45 | +``` |
| 46 | +. |
| 47 | +├── docker-compose.yml # Docker Compose configuration |
| 48 | +├── api/ # Python API service |
| 49 | +│ ├── app.py |
| 50 | +│ ├── Dockerfile |
| 51 | +│ └── requirements.txt |
| 52 | +├── web/ # Static web files |
| 53 | +│ └── index.html |
| 54 | +└── Makefile # Project commands |
| 55 | +``` |
| 56 | + |
| 57 | +## Adding New Services |
| 58 | + |
| 59 | +To add a new service, add it to `docker-compose.yml` with the appropriate labels: |
| 60 | + |
| 61 | +```yaml |
| 62 | +services: |
| 63 | + my-service: |
| 64 | + image: nginx:alpine |
| 65 | + labels: |
| 66 | + - caddy=myservice.demo.local |
| 67 | + - caddy.reverse_proxy={{upstreams 80}} |
| 68 | + - caddy.@default # Apply global middleware |
| 69 | + networks: |
| 70 | + - caddy_network |
| 71 | +``` |
| 72 | +
|
| 73 | +## Troubleshooting |
| 74 | +
|
| 75 | +### Certificate Warnings |
| 76 | +For local development, you'll need to accept the self-signed certificate in your browser. |
| 77 | +
|
| 78 | +### Hosts File |
| 79 | +If you can't access the services: |
| 80 | +1. Verify the entries in `/etc/hosts`: |
| 81 | + ``` |
| 82 | + 127.0.0.1 web.demo.local api.demo.local |
| 83 | + ``` |
| 84 | +2. Make sure you ran `sudo make setup` |
| 85 | +
|
| 86 | +### Port Conflicts |
| 87 | +If ports 80/443 are in use: |
| 88 | +1. Stop other web servers |
| 89 | +2. Or update the port mappings in `docker-compose.yml` |
| 90 | +
|
| 91 | +## License |
| 92 | +
|
| 93 | +MIT |
0 commit comments