Docker image not connecting with a database running on same machine

Is --network="hosts" a typo?

Either this is a bridge/overlay network, then the published ports make sense, but localhost:9042 can not work. Or this is --network=host and the publised port will be ignored - but then localhost:9042 should work

When a bridged/overlay network is used, localhost inside a container is local to the container, not to the host. You need to use the host ip or hostname to access services from the host.

When host (not hosts!) network is used, localhost inside the container actualy is localhost from the host - network-wise processes in the container behave then like if they run as a local process on the host.

1 Like