I’m having a similar problem. After the suggestions here, I fixed it, but autodetection is a little slow.
Perhaps a silly question, but if I want to do this in YAML, would I do it for this specific service? IE:
services:
- type: web
name: foo
... other bits and bobs ...
envVars:
- key: PORT
value: 4242