简体   繁体   中英

connect tomcat and mysql in docker container with jdbc driver

i'm learning docker and trying to put my java web application using tomcat and mysql to container. but i faced some problem and no solution to work properly to me.

Error message 错误信息

My architecture 我的建筑

tomcat folder - docker file

FROM tomcat:latest
RUN mkdir -p /usr/local/tomcat/webapps/test/
COPY ./mysql-connector-java-5.1.44-bin.jar /usr/local/tomcat/webapps/test
COPY ./db.jsp /usr/local/tomcat/webapps/test 
COPY ./web.xml /usr/local/tomcat/webapps/test
CMD ["catalina.sh", "run"]

tomcat folder - db.jsp

<%@ page contentType = "text/html; charset=utf-8" %>
<%@ page import = "java.sql.DriverManager" %>
<%@ page import = "java.sql.Connection" %>
<%@ page import = "java.sql.Statement" %>
<%@ page import = "java.sql.ResultSet" %>
<%@ page import = "java.sql.SQLException" %>

<html>
<head><title>회원 목록</title></head>
<body>

MEMBER 테이블의 내용
<table width="100%" border="1">
<tr>
    <td>이름</td><td>아이디</td><td>이메일</td>
</tr>
<%
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        String jdbcDriver = "jdbc:mysql://mysql1:8888";
        String dbUser = "..";
        String dbPass = "..";

        String query = "select * from test";

        conn = DriverManager.getConnection(jdbcDriver, dbUser, dbPass);

        stmt = conn.createStatement();

        rs = stmt.executeQuery(query);

         while (rs.next()) {
                String sqlRecipeProcess = rs.getString("id");
                String sqlRecipeProcess2 = rs.getString("name");
        }


    }catch(SQLException ex) {
        out.println(ex.getMessage());
        ex.printStackTrace();
    } finally {
        if (rs != null) try { rs.close(); } catch(SQLException ex) {}
        if (stmt != null) try { stmt.close(); } catch(SQLException ex) {}

        if (conn != null) try { conn.close(); } catch(SQLException ex) {}
    }
%>
</table>

</body>
</html>`

mysql folder - dockerfile

FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=".."
ENV MYSQL_DATABASE=".."
ENV MYSQL_USER=".." 
ENV MYSQL_PASSWORD=".."
EXPOSE 3306

tomcat folder - mysql-connector-java-5.1.44-bin.jar

tomcat folder - web.xml

<web-app></web-app>

run by: docker build -t my-mysql . docker run -dit --name mysql1 -p 3306:3306 my-mysql

and input datadase using mysql workbench(i granted a privileges and checked the data at docker mysql bash.)

run by :

docker build -t my-tomcat .
docker run -dit --link mysql1:my-mysql --name myTomcat1 -p 8888:8080 my-tomcat

get access to http://localhost:8888/test/db.jsp

i think that the jdbc.jar is something wrong or db.jsp is wrong.

You might try this (assumes web.xml is the Web Application Context file):

FROM tomcat:latest
RUN mkdir -p /usr/local/tomcat/webapps/test/WEB-INF/lib
COPY ./mysql-connector-java-5.1.44-bin.jar /usr/local/tomcat/webapps/test/WEB-INF/lib
COPY ./db.jsp /usr/local/tomcat/webapps/test 
COPY ./web.xml /usr/local/tomcat/webapps/test/WEB-INF
CMD ["catalina.sh", "run"]

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