I have host with PostgreSQL and Docker container. PostgreSQL work on 5432 port. Docker container must connect to database. How to connect container with database through Dockerfile or run command? EXPOSE 5432
and docker run -p 5432:5432 ...
did not help.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
From the documentation page:
Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the
--add-host
flag. To find the host’s address, use theip addr show
command.
$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`
$ docker run --add-host=docker:${HOSTIP} --rm -it busybox telnet docker 5432
EXPOSE
or -p
flag work the other way around e.g. publish container ports to host which you don't want here.