Docker with "--network host" does not allow to access to port of application - spring-boot

I have Spring Boot app inside docker and I am running my app like this:
docker run --rm --network host --name myapp1 myapp
But when i am trying to access it from host machine it fails:
my_machine:~ root$ curl localhost:8081/someendpoint -v
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8081 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connection failed
* connect to 127.0.0.1 port 8081 failed: Connection refused
* Failed to connect to localhost port 8081: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 8081: Connection refused
It is not clear for me - why it is not working ?
It works fine from inside of docker.
Also myapp have no problems with connection to external docker images/internet.
Please help.

From https://docs.docker.com/network/host/
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
Since you said you were using macOS, --network host will not work properly. I believe the underlying reason is that, outside of Linux, a virtual machine is used to host the containers. The host whose network the container shares is the VM's, not the physical host's.

Related

curl: (7) Failed to connect to localhost port 9000

We are using AWS Lambda runtime interface emulator to run our app locally inside Docker using lite-server
I start my docker container with:
docker run -p 9000:8080 image-name
The curl http://localhost:9000 command is returning
curl: (7) Failed to connect to localhost port 9000 after 0 ms: Connection refused
I am a newbie to Docker, can anyone please suggest how to fix it?

XAMPP receives "Connection refused" when curling other localhosts

I've a Reactjs app (NodeJS) running on localhost:3000 as well as a local blockchain node (Hardhat) running on localhost:8545.
There's no issues with those two communicating with each other, but I also have a XAMPP server running locally on localhost:8080 with a php app that returns some values.
I can reach all these localhosts fine through the browser.
But I need the php to communicate with my blockchain node. But the connection keeps getting refused when Xampp tries to create the connection to the node.
I even tried a basic curl from Xampp's terminal, and Xampp can't reach the other localhosts at all (tried both http://localhost:port and http://127.0.0.1:port). For instance if I just wanted to curl my react app (html) from Xampp's terminal like this...
curl http://localhost:3000/ -v
I get this...
* Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 3000 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* connect to 127.0.0.1 port 3000 failed: Connection refused
* Failed to connect to localhost port 3000: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 3000: Connection refused
Yes, I can curl things like google.com fine from within Xampp's terminal with no issues. It's just my localhosts. Yes, I have enabled curling in php. Yes, I can curl my react app (html) just fine from a separate terminal window. I'm on MacOS 12.1.
Thanks in advance for any help!!
Edit: I did some research, and I believe the issue is that the Xampp virtual machine can't reach my localhost. I've tried resolving this at the host file level to redirect a domain name to 127.0.0.1, but that doesn't work either. I'm still stuck, but wanted to put an update in case this info is helpful.
Last edit: I gave up.

Connect via Socket from Host machine to container using host:port (port mapping) (windows to windows)

Connect to docker container (windows 10) from host via Socket connection
I have tried to build an image using windows 10 as base image, expose port that i want connect to from host machine (40030).
Dockerfile:
FROM mcr.microsoft.com/windows:1903
# Set working directory
WORKDIR /app
EXPOSE 40020
And this is the docker run command:
docker run -it --rm -p 40020:40020 sample:dev
I expect to connect from Host machine to container machine via socket connection. Maybe there are some issue with Socket connection at all, because simple HTTP server is working (simple nodejs server return the response from container to host)

can't connect to postgres on windows10

I am setting up project on my windows PC and I have a problem with postgres.
Project is set on docker.
when I run docker-compose up I receive error
: *** Failed to connect to database dev; trying to create database
/usr/local/bundle/gems/sequel-4.48.0/lib/sequel/adapters/postgres.rb:224:in `initialize': PG::ConnectionBad: could not connect to server: Connection refused (Sequel::DatabaseConnectionError)
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
I don't know what to do. I have already installed microsoft easy-fix for TCP-IP, shut down windows firewall, changed all connection in pg_hba.conf to trust. Nothing helps. I don't have any antivirus software installed.
nmap report:
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0027s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 995 closed ports
PORT STATE SERVICE
135/tcp open msrpc
445/tcp open microsoft-ds
2068/tcp open avocentkvm
2179/tcp open vmrdp
5432/tcp open postgresql
edit:
I run psql -h localhost and received
psql: CATASTROPHIC: role "Kamil" does not exist
Kamil is my PC name.
edit2:
is it possible that docker doesn't have access to postgres on localhost?
problem solved
I had to set environment var to DB_HOST=docker.for.win.localhost

Localhost vs 0.0.0.0 with Docker on Mac OS

I am reading the docs here and I find myself a bit confused, since running
docker run --name some-mysql -p 3306:3306 -d mysql
or
docker run --name some-mysql -p 127.0.0.1:3306:3306 -d mysql
then mysql --host localhost --port 3306 -u root gives me the following error :
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
But running mysql -u root -p --host 0.0.0.0 works.
Does someone have an explanation ?
With docker port forwarding, there are two network namespaces you need to keep track of. The first is inside your container. If you listen on localhost inside the container, nothing outside the container can connect to your application. That includes blocking port forwarding from the docker host and container-to-container networking. So unless your container is talking to itself, you always listen on 0.0.0.0 with the application you are running inside the container.
The second network namespace is on your docker host. When you forward a port with docker run -p 127.0.0.1:1234:5678 ... that configures a listener on the docker host interface 127.0.0.1 port 1234, and forwards it to the container namespace port 5678 (that container must be listening on 0.0.0.0). If you leave off the ip, docker will publish the port on all interfaces on the host.
So when you configure mysql to listen on 127.0.0.1, there's no way to reach it from outside of the container's networking namespace. If you need to prevent others outside of your docker host from reaching the port, configure that restriction when publishing the port on the docker run cli.
As described in the mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/connecting.html), when you connect to 127.0.0.1 with the client, it'll try to use the unix sockets to perform this operation. Normally this would work fine since it's on the same host. In Docker the socket file is not available.

Resources