简体   繁体   中英

Connect to database from another docker container

I ran two docker containers one is tomcat and one is phpmyadmin. I ran the phpmyadmin container with this command:

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

 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. but when I try to connect to it using hibernate and my J2EE application I get the following error.

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:

<?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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM