how to connect to localhost:9092 from docker container using docker-compose and not using docker bridge

With docker-compose:

Use the network_mode option to allow connection to localhost ports

network_mode: "host"

Without docker-compose:

Use the --net flag to allow connection to localhost ports

docker run -it --net=host

You can also use --network flag

--network="host"

According to the official Docker documentation these "give the container full access to local system services such as D-bus and is therefore considered insecure."

Of course if you containerise your service that is running on localhost:9092 then you could run that in a Docker container too and link your two Docker containers together using the --link flag:

docker run -t -d myService
docker run -t -d --link myService:myService_ref myOtherService

You could fix the IP address of your host and pass this to docker via docker-compose using the 'extra_hosts' option:

Something like:

sudo ifconfig lo0 alias 172.16.123.1 

Then in docker compose:

extra_hosts:
 - "service.localhost:172.16.123.1"

Then within the container you should be able to:

ping service.localhost