无法通过主机名从同一网络中的Spring Boot容器访问mysql容器

时间:2018-09-26 08:26:46

标签: mysql docker spring-boot docker-networking docker-network

我正在尝试从另一个Spring Boot应用程序容器访问我的MySQL容器,这两个容器都在同一网络中。 我创建了一个网络,并将两个容器都链接到它:

docker network create --driver bridge dmw_network

我的网络的详细信息:

[
    {
        "Name": "dmw_network",
        "Id": "2e204b7b48efafc5db0daccb801a3e38a0f1b99a6a745dd9b58a74172e966da3",
        "Created": "2018-09-25T17:28:35.530261782+02:00",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": {},
            "Config": [
                {
                    "Subnet": "172.19.0.0/16",
                    "Gateway": "172.19.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "28b1368d9256f0b323b528507ea9133ba410c691fbd01029ee3913eacfa94660": {
                "Name": "dmwapp",
                "EndpointID": "f59f82bf0e62fb97fa7b1b4e0c3750adffaf570a93de633938de32a199ba03ab",
                "MacAddress": "02:42:ac:13:00:03",
                "IPv4Address": "172.19.0.3/16",
                "IPv6Address": ""
            },
            "c6ed1cbdd376714c90c5e67024cc576030b030e29a9a8d766bb4bb637025dd08": {
                "Name": "mysql5",
                "EndpointID": "0ea9594ebb4251d5f13c05f407e660c2ed1b5e4ccaed2d71cedc610984b9b076",
                "MacAddress": "02:42:ac:13:00:02",
                "IPv4Address": "172.19.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

这是我的application.yml文件:

spring:
  application:
    name : ${appName:tasky}
  datasource:
    url: jdbc:mysql://mysql5:3406/dmw_db?autoReconnect=true&useSSL=false
    username: user
    password: pass
    driver-class-name: com.mysql.jdbc.Driver
    tomcat:
      max-active: 50
      max-idle: 50

这就是我运行两个容器的方式:

docker run --name=mysql5 --network=dmw_network -p 3406:3306 -v $(pwd)/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=pass -d dmw_mysql5

docker run --name dmwapp --network=dmw_network -d -p 8888:8888 -p 9999:9999 -v $(pwd)/conf:/home/conf/config/prod dmwimg

但是我仍然无法通过主机名从同一网络中的其他spring boot容器访问数据库。

这是错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.Util.getInstance(Util.java:408)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2096)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2021)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
        at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:319)
        at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:212)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:736)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:668)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:483)
        at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:154)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:118)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:107)
        at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:131)
        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 net.bull.javamelody.JdbcWrapper$3.invoke(JdbcWrapper.java:781)
        at net.bull.javamelody.JdbcWrapper$DelegatingInvocationHandler.invoke(JdbcWrapper.java:294)
        at com.sun.proxy.$Proxy139.getConnection(Unknown Source)
        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.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216)
        ... 117 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:990)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:342)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2037)
        ... 148 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:301)
        ... 150 more

当我切换到托管Docker容器的服务器的IP地址时,它就可以工作了。例如:

url: jdbc:mysql://184.4.x.x:3406/dmw_db?autoReconnect=true&useSSL=false

我在做什么错了?

2 个答案:

答案 0 :(得分:1)

将端口形式3406更改为正确的形式。

  url: jdbc:mysql://mysql5:3306/dmw_db?autoReconnect=true&useSSL=false

如果使用docker名称进行连接,则应使用内部端口,而不是暴露的内部端口。

答案 1 :(得分:0)

dmwapp对别名mysql5一无所知。您可以在跑步过程中链接它们:

docker run --name dmwapp --network=dmw_network -d -p 8888:8888 -p 9999:9999 -v $(pwd)/conf:/home/conf/config/prod --link mysql5:mysql5 dmwimg