简体   繁体   中英

Jetty Docker container running spring application unable to connect to mysql running outside docker container

I am new to docker world. I have jetty docker container that runs a spring application which is unable to connect to mysql running outside the docker container. Mysql is running on my localhost which is a MAC. I understand docker runs in a VM on mac. My jdbc connection string is jdbc.url=jdbc:mysql://127.0.0.1:3306/

Below is the exception that I see

nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 71 more
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:394)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:443)
    at com.multi.enterprise.commons.jdbc.config.JdbcConfig.jdbcTemplate(JdbcConfig.java:57)
    at com.multi.enterprise.commons.jdbc.config.JdbcConfig.namedParameterJdbcTemplate(JdbcConfig.java:52)
    at com.multi.enterprise.commons.jdbc.config.JdbcConfig$$EnhancerBySpringCGLIB$$492af47.CGLIB$namedParameterJdbcTemplate$3(<generated>)
    at com.multi.enterprise.commons.jdbc.config.JdbcConfig$$EnhancerBySpringCGLIB$$492af47$$FastClassBySpringCGLIB$$1a9e6a23.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
    at com.multi.enterprise.commons.jdbc.config.JdbcConfig$$EnhancerBySpringCGLIB$$492af47.namedParameterJdbcTemplate(<generated>)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
        ... 72 more
    Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)
        at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
        at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
        ... 86 more
    Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
        at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
        at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
        at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
        ... 89 more

Since a docker container has its own network namespace, it has its own 127.0.0.1. You will have to connect to your host machine by another routable ip address.

In my case, my mac is on my local home network with an ip address: 192.168.10.42. When I run the Docker For Mac application, it fires up a tiny virtual machine. Inside that virtual machine, docker runs along with all my containers. The virtual machine that is running sends all traffic out of the virtual machine by default. Since the 192.168.10.42 address is accessible to my mac, any traffic I send to that ip from inside the virtual machine will get to my mac.

From the point of view of one of the containers I am running, any traffic that it sends out is handled by the virtual machine. This means that any location that the virtual machine can get to I can also get to from my container.

If you do not want to depend on what your mac's ip address is on your local network, you can do something like this: https://gist.github.com/ralphschindler/535dc5916ccbd06f53c1b0ee5a868c93

The idea is to give an ip address to your mac that will always consistently be there no matter what network you move your mac to. I have done a similar thing, where I will add a new bridge interface to my mac through the System Preferences Network gui. I just leave the bridge with no physical interfaces attached to it, and then I manually assign an ip address to it.

Both of these approaches work because remember, any ip address on your mac is routable to from the vm that Docker For Mac runs.

A brand-new possibility as of Docker 17.06 is to use the docker.for.mac.localhost address instead of using any of the other previously mentioned workarounds. (The current stable version is still 17.03 as of writing.)

See: https://github.com/docker/docker.github.io/pull/3220 for information about the changes in 17.06

Use host.docker.internal (see here )

When using docker-desktop, this hostname is automatically populated by the proper IP.

When running this somewhere else, you need to use the proper host name or the ip.

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