Connect tomcat and mysql in docker-compose

2019-07-15 04:59发布

I'm working on project for studies. Now I am at the stage of moving project to docker containers. I have placet tomcat with my website and mysql with database do docker-compose.yml. Unfortunately classes from tomcat can't connect with database


Java code

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Waliduj {

String url="jdbc:mysql://localhost:3306/uczelnia?useTimezone=true&serverTimezone=UTC";
String username="root";
String password="121212";

public boolean Waliduje(String uname,String pass) {


    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(url,username,password);
        PreparedStatement st=con.prepareStatement("SELECT email,haslo FROM logowanie WHERE email=? AND haslo=?");
        st.setString(1,uname);
        st.setString(2,pass);
        ResultSet rs = st.executeQuery();
        if(rs.next()) {
            return true;
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException se) {
        // TODO Auto-generated catch block
        se.printStackTrace();
    }

    return false;
}
}

Dockerfile code

FROM tomcat:8.0.41-jre8
COPY ./webapp /usr/local/tomcat/webapps/webapp
CMD ["catalina.sh", "run"]

docker-compose.yml code

version: '3'
services: 
    web:
        build: .
        ports:
         - "8080:8080"
        links:
         - "db:uczelnia"
    db:
        image: mysql
        environment:
         MYSQL_ROOT_PASSWORD: 121212
         MYSQL_DATABASE: uczelnia

        ports:
         - "3306:3306" 
    PhpMyAdmin:
        image: nazarpc/phpmyadmin
        ports:
         - "8090:80"
        links:
         - db:mysql

Error code

web_1         | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
web_1         |
web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1         |         at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:172)
web_1         |         at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
web_1         |         at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:664)
web_1         |         at java.sql.DriverManager.getConnection(DriverManager.java:247)
web_1         |         at com.login.walidacja.Waliduj.Waliduje(Waliduj.java:20)
web_1         |         at com.login.Login.doPost(Login.java:27)
web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
web_1         |         at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1         |         at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
web_1         |         at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
web_1         |         at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
web_1         |         at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
web_1         |         at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
web_1         |         at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
web_1         |         at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
web_1         |         at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
web_1         |         at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
web_1         |         at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
web_1         |         at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1104)
web_1         |         at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2508)
web_1         |         at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2497)
web_1         |         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
web_1         |         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
web_1         |         at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
web_1         |         at java.lang.Thread.run(Thread.java:745)
web_1         | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
web_1         |
web_1         | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
web_1         |         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
web_1         |         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
web_1         |         at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:59)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:103)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:149)
web_1         |         at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:165)
web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:92)
web_1         |         at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:982)
web_1         |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:852)
web_1         |         ... 30 more
web_1         | Caused by: java.net.ConnectException: Connection refused (Connection refused)
web_1         |         at java.net.PlainSocketImpl.socketConnect(Native Method)
web_1         |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
web_1         |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
web_1         |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
web_1         |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
web_1         |         at java.net.Socket.connect(Socket.java:589)
web_1         |         at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
web_1         |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:66)
web_1         |         ... 33 more

Additional informations:

  • I'm working on windows 10 so docker is on windows too.
  • I apologize for spelling errors but I'm from poland so english is not my

1条回答
forever°为你锁心
2楼-- · 2019-07-15 05:08

Via the compose file, your database service will be referencable on the network via db. The links keyword isn't necessary unless you simply need a different name for your container. If you change your java code to connect to jdbc:mysql://db:3306/uczelnia?useTimezone=true&serverTimezone=UTC it should work as expected.

https://docs.docker.com/compose/networking/

查看更多
登录 后发表回答