问题描述
我有一个高山泊坞站,有postgres,听地址'*'和听5432,我正在部署使用 docker run -d --name postgres me / postgres:v1
我的tomcat容器带有oracle jre8,我正在使用以下方式部署我的休息web服务:
#设置环境
ENV CATALINA_HOME / opt / tomcat
EXPOSE 8080
#启动时启动Tomcat
CMD $ {CATALINA_HOME} /bin/catalina.sh运行
RUN rm -rf $ { CATALINA_HOME} / webapps / docs \
$ {CATALINA_HOME} / webapps / examples \
$ {CATALINA_HOME} / webapps / ROOT
#部署war文件
添加myapp.war $ {CATALINA_HOME} /webapps/ROOT.war
#部署
后重新启动服务器CMD $ {CATALINA_HOME} /bin/catalina.sh运行
并使用
docker run -d -p 8080:8080 --name tomcat --link postgres:postgres me / tomcat:v1
两个都在我的笔记本电脑上执行,IP地址为192.168.xx,我检查了端口正在收听。 >
不幸的是,我的tomcat上的web服务无法使用
jdbc连接到postgres服务:postgresql://192.168.xx:5432 / dBName
替代我已经尝试过: / strong>我在其自己的端口上启动了postgres,
docker run -d -p 5432:5432 --name postgres me / postgres:v1
docker run -d -p 8080:8080 --name tomcat me / tomcat:v1
然后使用
jdbc:postgresql://192.168.xx:5432 / dBName
和
jdbc: postgresql:// localhost:5432 / dBName
但似乎都不起作用。
在这两种情况下,我可以看到我的web服务器在tomcat管理器中运行,我可以使用
p sql -h localhost -p 5432 -d dBName -U myUser
以及pgAdmin。
解决这个问题的任何帮助都是值得赞赏的。
解决方案更新 ,指向postgres(即您的postgresql容器名称)而不是IP
jdbc:postgresql:// postgres:5432 / dBName
非常感谢@larsks指出它。
使用--link,指向postgres(即,您的postgresql容器名称)而不是IP
jdbc:postgresql:// postgres:5432 / dBName
一个完整的解决方案,运行你的postgresql和tomcat容器
docker run -d --name postgres me / postgresql:v1
docker run -d -p 8080:8080 --name tomcat --link postgres:postgres me / tomcat:v1
(请注意,我没有为postgres插入port呃因为它内部已经有了5432,除非你想从主机外面打它,你不需要在这里指定一个端口)
而你的服务器war文件将上面的jdbc地址,postgres将自动解析为容器的IP地址,当他们链接。
非常感谢@larsks指出它。 >
I have a alpine docker with postgres, with listen address '*' and listening to 5432, which I'm deploying using
docker run -d --name postgres me/postgres:v1
and my tomcat container with oracle jre8, on which I'm deploying my rest web service using:
# Set environment
ENV CATALINA_HOME /opt/tomcat
EXPOSE 8080
# Launch Tomcat on startup
CMD ${CATALINA_HOME}/bin/catalina.sh run
RUN rm -rf ${CATALINA_HOME}/webapps/docs \
${CATALINA_HOME}/webapps/examples \
${CATALINA_HOME}/webapps/ROOT
# Deploying war file
ADD myapp.war ${CATALINA_HOME}/webapps/ROOT.war
# Restarting server after deploying
CMD ${CATALINA_HOME}/bin/catalina.sh run
And deploying it with
docker run -d -p 8080:8080 --name tomcat --link postgres:postgres me/tomcat:v1
Both are being executed on my laptop, with IP address 192.168.x.x, and I checked the port is listening.
Unfortunately my web service on tomcat cannot connect to the postgres service using
jdbc:postgresql://192.168.x.x:5432/dBName
Alternate I already tried: I launched postgres on it's own port using,
docker run -d -p 5432:5432 --name postgres me/postgres:v1
docker run -d -p 8080:8080 --name tomcat me/tomcat:v1
Then used
jdbc:postgresql://192.168.x.x:5432/dBName
and
jdbc:postgresql://localhost:5432/dBName
but neither seems to work.
In both cases I can see my web server running in tomcat manager, and I am able to access my dB using
psql -h localhost -p 5432 -d dBName -U myUser
as well as pgAdmin.
Any help in resolving this is appreciated.
Solution Update: While using --link, point to postgres (i.e., your postgresql container name) instead of IP
jdbc:postgresql://postgres:5432/dBName
Many thanks to @larsks for pointing it out.
While using --link, point to postgres (i.e., your postgresql container name) instead of IP
jdbc:postgresql://postgres:5432/dBName
So for a full solution, run your postgresql and tomcat container
docker run -d --name postgres me/postgresql:v1
docker run -d -p 8080:8080 --name tomcat --link postgres:postgres me/tomcat:v1
(Notice here I didn't put port for postgres container since it will already have 5432 exposed internally, unless you want to hit it from outside your host, you don't need to specify a port here)
And your server war file will the jdbc address above, postgres will automatically resolve to the container's IP address when they are linked.
Many thanks to @larsks for pointing it out.
这篇关于Docker的Tomcat容器无法访问Postgres容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!