Share localhost with docker container - docker

I have a localhost application running on port 5001.
I also have a docker container running that I want to be able to access the application described above but I receive a connection refused error.
I imagine this is because the container needs additional configuration to access the application via the port 5001.

Related

connect ECONNREFUSED 127.0.0.1:8046 when connecting to host app from Docker container - Windows 10

I have a standalone app running on my machine (not in a docker container) listening to port 8046 (localhost:8046) using an ngrok tunnel (http ngrok 8046). I am trying to connect to it sending a POST from my docker container which I'm also running on my local machine using http://host.docker.internal:8046. I have tried many different approaches but nothing seems to work:
I have tried to disable my firewall to make sure that wasn't the issue.
Tried http://localhost:8046 in the container to connect to the app
Tried serving the standalone app on 0.0.0.0 with the same port 8046 and setting http://0.0.0.0:8046 in the container to connect to it, unsuccessfully
I keep having connect ECONNREFUSED 127.0.0.1:8046
my etc/hosts is configured with the IP added by docker desktop for host.docker.internal and gateway.docker.internal
I'm running out of ideas and can't figure out what I could be missing. Any ideas? Thanks in advance
Inside container localhost, 127.0.0.1, 0.0.0.0 represent the container itself. To connect to the host machine please try using its domain name or IP address in the LAN, which the host machine belongs to.

How to connect to localhost inside the docker container?

I have mysql and an application running on the docker. I want the application to connect to mysql localhost inside the docker.
Every container in Docker is a different host with its own IP and hostname, that's why you can't connect to your DB from your app using 127.0.0.1, they are not running on the same host.
You can see the IP assigned to a container with docker inspect <container-id>, but more easily you can refer to a service running on a container by its host, which by default is the name of the container (db in your case). You can also customize the hostname using hostname as you did.
Set db (or hybris_dev depending on how you prefer to configure your container) as the hostname to establish the connection to your DB from your app and it should work.

Docker container can't connect to ip host

I have deployed a netflix hystrix dashboard with turbine on a docker container, I can access to http://ip:8081/hystrix but when I try to monitor the stream of turbine it freeze and doesn't return any information, I test using curl inside the container and execute curl http://localhost:8081/turbine.stream and curl http://containername:8081/turbine.stream, with those two command works perfectly but when I use the host ip as curl http://hostip:8081/turbine.stream the curl throws Failed to connect to hostip port 8081: No route to host, I can't found a solution, can someone help me with this issue?,
Thanks in advance.
In order to access the container through Host IP you need to ensure the following:
Port mapping is allowing through the Host/Public IP itself not only localhost.
You can check this by executing docker ps on the docker host and look for the PORTS column the default should be as the following 0.0.0.0:8081->8081/tcp which means it can accept connection from any interface either public, private or localhost.
The firewall is not blocking the connection on port 8081.
By default the firewall of the host should be managed by Docker daemon itself so the port 8081 will be allowed in the firewall but you might have a different case either Docker is not managing the firewall of the host or there is an extra layer that prevents the connection

Docker: How to connect to locally available servers from within docker

I run docker on windows. I have a docker container running a python application that needs a database connection.
Installing a DB on my machine and connecting to it via "docker.for.win.localhost" in my container works fine.
Now I want to connect to a database running on a server that is available over my local network. I can't seem to connect to it from inside my docker container. I don't quite understand how I can proxy the server to my container. The error indicates that it can't establish a connection to this server:
(psycopg2.OperationalError) could not connect to server: No route to host
Is the server running on host "XX.XXX.XX.XX" and accepting
TCP/IP connections on port 5555?
I'm sure this is supposed to work somehow, right?
you can add IP of host to the container
docker run --add-host="yourhost:IPOFTHEHOST"
and yourhost will be connected to host

Connecting to Docker container connection refused - but container is running

I am running 2 spring boot applications: A client and rest-api. The client communicates to the rest-api which communicates to a mongodb database. All 3 tiers are running inside docker containers.
I launch the containers normally specifying the exposed ports in the dockerfile and mapping them to a port on the host machine such as: -p 7070:7070, where 7070 is a port exposed in the Dockerfile.
When I run the applications through the java -jar [application_name.war] command, the application works fine and they all can communicate.
However, when I run the applications in a Docker container I get connection refused error, such as when the client tries to connect to the rest-api I get a connection refused error at http://localhost:7070.
But the command docker ps shows that the containers are all running and listening on the exposed and mapped ports.
I have no clue why the containers aren't recognizing that the other containers are running and listening on their ports.
Does this have anything to do with iptables?
Any help is appreciated.
Thanks
EDIT 1: The applications when ran inside containers work fine on my machine, and they don't throw any connection refused errors. The error only happens on that particular different machine.
I used container linking to solve this problem. Make sure you add --link <name>:<alias> at run-time to the container you want linked. <name> is the name of the container you want to link to and <alias> will be the host/domain of an entry in Spring's application.properties file.
Example:
spring.data.mongodb.host=mongodb if the alias supplied at run-time is 'mongodb':
--link myContainerName:mongodb

Resources