nginx in front of docker swarm services
I'd recommend swapping out the statically defined nginx reverse proxy for traefik which is swarm aware and can dynamically reconfigure itself as services are deployed and destroyed.
Here's a sample implementation:
create a network for the traefik to talk to containers:
docker network create proxy
Make a traefik.toml, here's an example:
traefik.toml
accessLogsFile = "/proc/1/fd/1"
defaultEntryPoints = ["http"]
[entryPoints]
[entryPoints.http]
address = ":80"
[web]
address = ":8080"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "localhost"
watch = true
swarmmode = true
constraints = ["tag==frontend"]
- Sample compose file for traefik:
docker-compose.traefik.yml
version: '3'
networks:
proxy:
external:
name: proxy
services:
traefik:
image: traefik:latest
volumes:
- ./traefik.toml:/etc/traefik/traefik.toml:ro
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 80:80
- 8080:8080
networks:
- proxy
restart: unless-stopped
- Configure your app with labels and on the same network.
docker-compose.app.yml
version: '3'
networks:
proxy:
external: true
services:
webtest:
image: nginx:latest
networks:
- default
- proxy
labels:
- traefik.frontend.rule=PathPrefixStrip:/webtest
- traefik.port=80
- traefik.docker.network=proxy
- traefik.tags=frontend
restart: unless-stopped
The rule above uses path prefixes for the container for simplicity, but you can use any rule you prefer to proxy your application.