问题
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.
来源:https://stackoverflow.com/questions/35327522/host-port-with-db-to-docker-container