Elasticsearch and Kibana on Docker using non-default ports

How to make Kibana discover and work with Elasticsearch in your local development environment when using non-default ports for these services. Ready to use snippets for Docker Compose and explanation how does it work under the hood.