使用 Docker 启动 Web 应用程序,无法连接到 Postgresql DB?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37482716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Docker to launch web app, can't connect to Postgresql DB?
提问by Rome_Leader
I received the following error when trying to write session data using Tomcat's PersistentManager to a Postgres DB running on my local machine:
尝试使用 Tomcat 的 PersistentManager 将会话数据写入本地计算机上运行的 Postgres DB 时,收到以下错误:
SEVERE: A SQL exception occurred org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
The application itself runs in a docker container. For completeness sake, my current context.xml file is:
应用程序本身在 docker 容器中运行。为了完整起见,我当前的 context.xml 文件是:
<?xml version='1.0' encoding='utf-8'?>
<Context>
<Manager className="org.apache.catalina.session.PersistentManager"
distributable="true" processExpiresFrequency="6" maxIdleBackup="0" debug="99" >
<Store className="org.apache.catalina.session.JDBCStore"
driverName="org.postgresql.Driver"
connectionURL="jdbc:postgresql://localhost/admin?stringtype=unspecified"
connectionName="admin" connectionPassword="admin"
sessionAppCol="app_name" sessionDataCol="session_data" sessionIdCol="session_id"
sessionLastAccessedCol="last_access" sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions_tb" sessionValidCol="valid_session" />
</Manager>
</Context>
Pursuant to the suggestions here: Postgresql : Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections
根据此处的建议:Postgresql:连接被拒绝。检查主机名和端口是否正确以及 postmaster 是否正在接受 TCP/IP 连接
I confirmed via a netstat -aln | grep LISTEN
that Postgresql is running and listening on the correct ports:
我通过 anetstat -aln | grep LISTEN
确认 Postgresql 正在运行并侦听正确的端口:
tcp4 0 0 127.0.0.1.5432 *.* LISTEN
tcp6 0 0 ::1.5432 *.* LISTEN
and that my postgresql.conf (located in usr/local/var/postgres
) has listen_addresses = localhost and port = 5432, which mirrors the host and port of my running server in Pgadmin3.
和我的postgresql.conf(位于usr/local/var/postgres
)具有的listen_addresses = localhost和端口= 5432,其中反射镜在Pgadmin3我正在运行的服务器的主机和端口。
I suspect that the problem is that Docker runs in a VM, and thus the local information I have obtained may not be the whole story. Reading up on the available information online, it seems that I may require some sort of bridged networking.
However, I admit I am a novice in this area, and I'm unsure of how to set it up.
我怀疑问题是Docker运行在VM中,所以我得到的本地信息可能不是全部。阅读在线可用信息,似乎我可能需要某种桥接网络。
但是,我承认我是这方面的新手,我不确定如何设置。
回答by LittleQ
Why I can NOT connect to localhost:5432?
为什么我无法连接到 localhost:5432?
Cat your container's /etc/hosts
猫你的容器 /etc/hosts
$ sudo docker exec -it [container] cat /etc/hosts
For docker networks is bridge
by default, the localhost
inside points to container itself(Docker default bridge network).
Then you don't have 5432
listening in your container:
对于 docker 网络bridge
,默认情况下,localhost
inside 指向容器本身(Docker 默认桥接网络)。那么你就没有5432
在你的容器中收听:
$ sudo docker exec [container] nc -v -z localhost 5432
Solution 1. If you wanna hardcode the "localhost:5432" inside your config xml, the easiest way is creating your container with the option "--net=host":
解决方案 1. 如果您想在 config xml 中硬编码“localhost:5432”,最简单的方法是使用选项“--net=host”创建容器:
$ sudo docker run --net=host -it ...
解决方案 2. 更改
localhost
localhost
容器内的 docker 主机 ip- Get your docker host ip:
$ sudo docker inspect -f '{{ .NetworkSettings.Gateway }}' 192.168.5.1
- Enter your container:
$ sudo docker exec -it [container] /bin/bash
- Edit the file
/etc/hosts
to point the localhost to docker host ip:$ sudo vim /etc/hosts 192.168.5.1 localhost
- 获取你的docker主机ip:
$ sudo docker inspect -f '{{ .NetworkSettings.Gateway }}' 192.168.5.1
- 输入您的容器:
$ sudo docker exec -it [container] /bin/bash
- 编辑文件
/etc/hosts
以将 localhost 指向docker host ip:$ sudo vim /etc/hosts 192.168.5.1 localhost
Solution 3. Modify your db config file to use an alias instead of localhost
:
解决方案 3. 修改您的 db 配置文件以使用别名而不是localhost
:
connectionURL="jdbc:postgresql://DB_ALIAS/admin?stringtype=unspecified"
然后添加DB_ALIAS
DB_ALIAS
到容器的 hosts :
$ sudo docker run --add-host DB_ALIAS:192.168.5.1 -it [image] ...
回答by Didier R. G.
I was getting the same error but this simple solution works perfect for me.sudo docker run -d --net="host" -it <IMAGE>
Now I can run my app https://x.x.x.x:pppp/../..
and everything works fine. I hope this helps
我遇到了同样的错误,但这个简单的解决方案对我来说很完美。sudo docker run -d --net="host" -it <IMAGE>
现在我可以运行我的应用程序https://x.x.x.x:pppp/../..
并且一切正常。我希望这有帮助
回答by vinga
If you are using docker-compose together with postgres image, than you can reuse service name as IP inside jdbc connection (here: app-db)
如果您将 docker-compose 与 postgres 图像一起使用,那么您可以在 jdbc 连接中重用服务名称作为 IP(此处:app-db)
web:
build: ./web
ports:
- "8080:8080"
links:
- app-db
environment:
- MYAPP_JDBC_URL=jdbc:postgresql://app-db:5432/somedb
- MYAPP_JDBC_USER=someuser
- MYAPP_JDBC_PASS=pass
app-db:
image: postgres:9.6
environment:
- POSTGRES_USER=someuser
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=somedb
expose:
- 5432
volumes_from:
- app-db-data
app-db-data:
image: cogniteev/echo
command: echo 'Data Container for PostgreSQL'
volumes:
- /opt/postgresdata/:/var/lib/postgresql/data