Host port with DB to Docker container

2019-08-10 11:57发布

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条回答
做自己的国王
2楼-- · 2019-08-10 12:25

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 the ip 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.

查看更多
登录 后发表回答