[英]Connect to database from another docker container
I ran two docker containers one is tomcat and one is phpmyadmin. 我运行了两个docker容器,一个是tomcat,一个是phpmyadmin。 I ran the phpmyadmin container with this command:
我使用以下命令运行phpmyadmin容器:
docker run -d -p 49160:22 -p 49161:80 -p 49162:3306 --name db phpmyadmin:imported
And I can see the phpmyadmin on my browser on port 49161. I ran the tomcat container with this command 而且我可以在浏览器的端口49161上看到phpmyadmin。我使用以下命令运行了tomcat容器
docker run -it -v ~/docker/tomcat/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml --name tomcat --link db:server -p 8888:8080 tomcat:deployed
Every thing looks fine. 一切都很好。 I can ping the db from the tomcat container.
我可以从tomcat容器ping数据库。 but when I try to connect to it using hibernate and my J2EE application I get the following error.
但是当我尝试使用休眠和J2EE应用程序连接到它时,出现以下错误。
10-Jun-2015 19:20:12.293 WARNING [http-nio-8080-exec-15] org.hibernate.cfg.SettingsFactory.buildSettings Could not obtain connection metadata
java.sql.SQLException: Connections could not be acquired from the underlying database!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:690)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:140)
at org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:56)
This is my hibernate.cfg.xml: 这是我的hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://172.17.0.20:3306/auction</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<mapping class="ie.domain.entity.User"/>
<mapping class="ie.domain.entity.Auction"/>
<mapping class="ie.domain.entity.Offer"/>
</session-factory>
</hibernate-configuration>
I tried to use the name server instead of the IP and also I tried the port 49162 instead of 3306 and I got the same error. 我尝试使用名称服务器而不是IP,并且尝试使用端口49162而不是3306,但出现相同的错误。
Try connecting with: 尝试连接:
jdbc:mysql://server:3306/auction
The port mappings are on the host, so you don't use them when talking directly to the container. 端口映射位于主机上,因此在直接与容器通信时不要使用它们。 The docker link functionality will add the name "server" to
/etc/hosts
with container IP address, so you can just reference it by name. docker链接功能会将名称“ server”添加到具有容器IP地址的
/etc/hosts
,因此您可以按名称进行引用。
UPDATE: Judging from the comment on https://registry.hub.docker.com/u/wnameless/mysql-phpmyadmin/ , this isn't going to work as the server is only listening on 127.0.0.1, not 0.0.0.0. 更新:从对https://registry.hub.docker.com/u/wnameless/mysql-phpmyadmin/的评论来看,这将无法正常工作,因为服务器仅在监听127.0.0.1,而不是0.0.0.0 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.