How to redirect one port in a docker container to the container host?

To simplify the development of the project, I put several services in which it depends on the docker containers. This makes "localhost" in the project configuration mean something else when it is passed to one of the containers.

change

To be clear, I'm trying to forward one of the port containers to the host , so when the process running in the container tries to access localhost: 5432, it connects to the host port of the host <54>.

Endedit

I am currently using

HOST_IP=`ip route | grep default | awk '{ printf "%s",$3 }'`
cat /etc/hosts | sed "s/127.0.0.1/$HOST_IP/" > /tmp/etc_hosts
cp /tmp/etc_hosts /etc/hosts

redirect everything that targets "localhost" to the container host. It works in this situation, but I would prefer to find a way to do this only for the required port, since I expect it to not work in other situations.

Here is what I came up with, but it does not work; when the connection in the container is with localhost: 5432, it tries to connect to the 5432 container instead of the host:

# --- These are the things that should make redirecting port 5432 to the host machine
#     work, provided the container is run in privileged mode.
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.ipv4.conf.all.route_localnet=1
iptables -t nat -A PREROUTING -p tcp --dport 5432 -j DNAT --to 172.19.0.1:5432
iptables -A FORWARD -d 172.19.0.1 -p tcp --dport 5432 -j ACCEPT
iptables -t nat -A POSTROUTING -j MASQUERADE
+4
source share
2 answers

If I understand well, for development, you want localhost to be resolved in a specific container, including when called from another container.

Host redirection

hosts, , , , , , ... .

.

Docker

Docker Toolbox Virtual Box, , localhost . , , Virtualbox. Wordpress :

  • docker run -p 80:80 --name website -d wordpress
  • → ( ) → → 1 → → 8080 80

Wordpress http://localhost:8080. , MacOS ​​ - ( 1024).

, script:
VBoxManage modifyvm "default" --natpf1 "app,tcp,,8080,,80"

Windows/Docker Mac

Docker for Windows/Docker for Mac ( Linux), Docker Toolbox, , -p, , ( VM Linux):

docker run -p 5432:5432 --name myapp -d myimage myapp localhost:5432.

socat ( iptables)

socat , :

socat TCP-LISTEN:5432,fork,reuseaddr,user=node,group=node,mode=777 TCP:172.19.0.1:5432 &

( 172.19.0.1 - IP- )

-

hosts , , ​​:

docker run ubuntu cat /etc/hosts

hosts --add-host:

docker run --add-host domain:1.2.3.4 --add-host domain2:5.6.7.8 ubuntu cat /etc/hosts

localhost, localhost. , ( ), - --network=host, :

docker run --network=host ubuntu

, , .

, - , localhost.

docker run -d  --name mariadb -e MYSQL_ROOT_PASSWORD=password mariadb 
docker run -d --name="wordpress" -p 8080:80 -e WORDPRESS_DB_PASSWORD=password --link mariadb:mysql wordpress

Wordpress mysql hosts, IP- mariadb. , bash Wordpress .

docker exec -ti wordpress bash
#cat /etc/hosts
+3

,

docker: -p hostport: containerport

docker run  -p 5432:5432 --name mycontainer -d myimage
+1

Source: https://habr.com/ru/post/1666300/


All Articles