Docker Mosquitto container not working

At least for the bridge-network the container’s localhost is not the same as your computer’s localhost (at least if you don’t apply some “tweaks” - see Connecting redis to a container image - #5 by rimelek). To talk to the computer’s localhost from within a container use the 172.17.0.1 (on linux this is the host in docker’s default-network) or host.docker.internal (on Windows).
Don’t use container’s ip-addresses as they change if you start the container the next time. Instead use the container’s name as DNS-name to talk from a container to the other one.
Or if you have both containers in one docker-compose.yml you can also use the service’s name as DNS-name to connect to.