127.0.0.1 (and, I think, the IPv6 address ::1) access localhost of the container itself, which is to say, the container.
Right, your container probably isnât running a telnet daemon.
If you mean youâre trying to access a service running on the physical host that the container happens to be running on, itâs essentially the same question as trying to access the Web-based admin console of your home router: itâs somewhere else on the network that definitely isnât localhost. Depending on your specific environment the host is often 172.17.0.1, or else itâs 192.168.99.1, or maybe itâs something else. Some Docker variants have a dedicated DNS name for it too. The most portable path is to find the host systemâs IP address (maybe the docker0 interface address) and pass that as a parameter into the container.
(That is: youâd do it the exact same way as you would otherwise in your Jenkins setup if the master and the slave were on different systems.)
Thanks for a quick reply. Let me provide more clarity on the question. When i exec into the container any connection I try to make to localhost, it returns a connection refused error. As for the telnet, it is installed, the connection refused error is thrown by it as well.
I run the below script as its a modified snippet of something that i am trying to do.
Typically containers run only a single process that is running a network service, and you probably know what port itâs on. If your container is running a Web server thatâs listening on port 80, Iâd expect a connect(2) call to localhost:80 to work, and absolutely everything else to return âconnection refusedâ.
When you say connect to localhost:80, you mean from the host machine or the container?
The behavior I am expecting is to find make a connection on localhost inside the container itself. I have exposed few ports e.g. 49000 but those are accessible by the host ip and not localhost on the container.
If I run it from the host, I expect to get whatever the host has running on port 80 (which could be a Docker container docker run -p 80:....). If I run it from the container, I expect to get whatever in the container is listening on port 80, which may or may not be exposed to the host (it might be on a different port on the host; docker run -p ...:80).
Iâm still not clear what you mean by âlocalhost inside the container itselfâ. Since a container usually only runs one service, and it usually isnât that valuable for a service to connect to itself, the usual meaning of 127.0.0.1 (âconnect back to the same place Iâm calling fromâ) isnât very useful inside a Docker container. âExposeâ has a specific meaning in Docker which also isnât especially useful (itâs a declaration in an image that it runs a service that listens on a port, but doesnât cause that port to actually be published).
If your container is running a service:
CMD ["/mydaemon", "-http", ":8080"]
and you publish it on a port:
sudo docker run --rm -p 49152:8080 --name test myimage
and you make an HTTP request from within the container, youâd use the port the daemon runs on within the container, not the port itâs published on from the host
host$ curl http://localhost:49152
host$ sudo docker exec test curl http://localhost:8080
This is my first post and my English is not that good, so pls do not BASH meâŠ
I think you miss the point of âlocalhostâ. As it always points to the actual LOCAL âmachineâ where it is called, it will try to curl the 8080 port of the container itself this way.
If you execute docker inspect test you can find a line like this âGatewayâ: â172.17.0.1â this points to the base Docker networkâs Gateway (represented by docker0 if you execute ifconfig).
So running docker exec test curl http://172.17.0.1:8080 will give you the page you are hosting on the machine where you run Docker.
And if you want to go deeper you can write a script like this inside the container:
Which will give you the same result if you run docker exec test ./path/of/script/script.sh from the host machine, except it will work with any IP, as route | awk â/^default/ { print $2 }â translates to the default Gatewayâs IP address.
Ugh, thank you - Iâve been ânot getting thisâ for a while. This made perfect sense. Now, I see below I can âfixâ this by using docker-host and docker compose. Is there another way to forward into the container itself (not the local machine) without using docker compose?