简体   繁体   English

Docker 找不到合适的驱动程序 sqlserver

[英]Docker no suitable driver found sqlserver

So I have a Spring boot application that runs on port 8080 on my localhost.所以我有一个 Spring 启动应用程序,它在我的本地主机上的端口 8080 上运行。 It connects to an Azure sqlserver database and pulls or inserts item from that database.它连接到 Azure sqlserver 数据库并从该数据库中提取或插入项目。 Locally everything is working fine.本地一切正常。 Now I'm trying to dockerize the application.现在我正在尝试对应用程序进行 docker 化。 My Dockerfile looks like this:我的 Dockerfile 看起来像这样:

FROM maven:3.5-jdk-8 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
COPY mssql-jdbc-9.2.1.jre15.jar .
RUN mvn -f /usr/src/app/pom.xml clean package

FROM gcr.io/distroless/java
COPY --from=build /usr/src/app/target/ProfileService-0.0.1-SNAPSHOT.jar /usr/app/ProfileService-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/ProfileService-0.0.1-SNAPSHOT.jar"]

The Dockerfile is located in the root folder, the same folder as the pom.xml and the mssql-jdbc-9.2.1.jre15.jar file. Dockerfile 位于根文件夹,与 pom.xml 和 mssql-jdbc-9.2.1.jre15.Z68995FCBF432492D15484DAC04A9 文件相同的文件夹。 This jar is downloaded from the official Microsoft source.这个jar是从微软官方源下载的。 My pom includes multiple dependencies, one of them is the mssql-jdbc dependency.我的 pom 包含多个依赖项,其中之一是 mssql-jdbc 依赖项。

<dependency>

    <groupId>com.microsoft.sqlserver</groupId>

    <artifactId>mssql-jdbc</artifactId>

    <version>9.2.1.jre15</version>

</dependency>

To connect to the database, I use the following code:要连接到数据库,我使用以下代码:

private final String connectionUrl = "jdbc:sqlserver://x.database.windows.net:1433;DatabaseName=profile;user=x@xr;password=x;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;";

With the following method as an example to get data:以如下方法为例获取数据:

public GetProfileReturnModel getProfile(String profile_name) {
    GetProfileReturnModel returnModel = new GetProfileReturnModel();
    try (Connection connection = DriverManager.getConnection(connectionUrl)) {
        try {
            CallableStatement cstmnt = connection.prepareCall("{CALL getProfile(?)}");
            cstmnt.setString(1, profile_name);

            cstmnt.execute();
            ResultSet rs = cstmnt.getResultSet();
            if (rs.next()) {
                returnModel.setUser_id(rs.getInt("user_id"));
                returnModel.setBio(rs.getString("bio"));
                returnModel.setLocation(rs.getString("location"));
                returnModel.setProfile_name(rs.getString("profile_name"));
                returnModel.setPicture(rs.getString("picture"));
                returnModel.setWebsite(rs.getString("website"));
            }
            returnModel.setSuccess(true);
        } catch (SQLException e) {
            returnModel.setSuccess(false);
            returnModel.setErrorMessage(e.toString());
    }
    } catch (SQLException e) {
        returnModel.setErrorMessage(e.getMessage());
        returnModel.setSuccess(false);
    }
    return returnModel;
}

This works fine locally.这在本地运行良好。 But when I run my build image in Docker and I call the same request I get the following error:但是当我在 Docker 中运行我的构建映像并调用相同的请求时,我收到以下错误:

No suitable driver found for jdbc:sqlserver://x.database.windows.net:1433;DatabaseName=profile;user=x@xr;password=x;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;没有找到适合 jdbc 的驱动程序:sqlserver://x.database.windows.net:1433;DatabaseName=profile;user=x@xr;password=x;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows .net;登录超时=30;

There is also this weird behavior that when I run the packaged.jar file using the cmd it runs perfectly, but if I start it by double clicking the jar I get the same error as mentioned above还有这种奇怪的行为,当我使用 cmd 运行 packaged.jar 文件时,它运行得很好,但是如果我通过双击 jar 来启动它,我会得到与上面提到的相同的错误

My complete pom.xml:我完整的 pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kwetter.profileservice</groupId>
    <artifactId>ProfileService</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ProfileService</name>
    <description>Demo project for Spring Boot</description>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <distributionManagement>
        <repository>
            <id>ProfileServiceBuild</id>
            <url>https://pkgs.dev.azure.com/382763/Kwetter/_packaging/ProfileServiceBuild/maven/v1</url>
        </repository>
    </distributionManagement>
    <repositories>
        <repository>
            <id>ProfileServiceBuild</id>
            <url>https://pkgs.dev.azure.com/382763/Kwetter/_packaging/ProfileServiceBuild/maven/v1</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.3</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>2.3.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-amqp -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-amqp</artifactId>
            <version>2.3.6</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>9.2.1.jre15</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jboss.spec.javax.ws.rs</groupId>
            <artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.11.3</version>
        </dependency>
    </dependencies>
</project>

The spring-boot-maven-plugin should by default, when configuring a jar packaging , repackage and include all your project dependencies in a big jar when mvn package runs, so probably the SQL Server JDBC Driver related classes should be included as well. The spring-boot-maven-plugin should by default, when configuring a jar packaging , repackage and include all your project dependencies in a big jar when mvn package runs, so probably the SQL Server JDBC Driver related classes should be included as well.

The docker related problem may have to do with the fact that you are using an incompatible Java version for the provided SQL Server driver when running your application. docker 相关问题可能与您在运行应用程序时为提供的 SQL 服务器驱动程序使用不兼容的 Java 版本有关。

You are using the SQL Server JDBC Driver for Java 15:您正在为 Java 15 使用 SQL 服务器 JDBC 驱动程序:

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>9.2.1.jre15</version>
  <scope>compile</scope>
</dependency>

Probably you are using Java 15 in your local environment, and it is running fine.可能您在本地环境中使用的是 Java 15,并且运行良好。

But you are running the application with gcr.io/distroless/java and that image only supports Java 1.8 and Java 11 .但是您正在使用gcr.io/distroless/java运行应用程序,并且该图像仅支持Java 1.8 和 Java 11

Please, consider use a different version of the SQL Server driver, for example:请考虑使用不同版本的 SQL 服务器驱动程序,例如:

<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>9.2.1.jre8</version>
</dependency>

And adapt your Dockerfile if necessary:如有必要,调整您的 Dockerfile:

FROM maven:3.5-jdk-8 AS build
COPY src /usr/src/app/src
COPY pom.xml /usr/src/app
RUN mvn -f /usr/src/app/pom.xml clean package

FROM gcr.io/distroless/java:8
COPY --from=build /usr/src/app/target/ProfileService-0.0.1-SNAPSHOT.jar /usr/app/ProfileService-0.0.1-SNAPSHOT.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/ProfileService-0.0.1-SNAPSHOT.jar"]

I removed line COPY mssql-jdbc-9.2.1.xxx.jar.我删除了行COPY mssql-jdbc-9.2.1.xxx.jar. : if you are including the dependency in your pom it will not be necessary. :如果您在 pom 中包含依赖项,则没有必要。

Perhaps the double click problem can also be related with this if you are using a different version of the JVM as the one configured by default in your OS.如果您使用不同版本的 JVM 作为操作系统中默认配置的版本,则双击问题也可能与此有关。

This could be caused by Azure SQL requiring login through AD Authentication.这可能是由于 Azure SQL 需要通过 AD 身份验证登录造成的。 Your connection string would look like this:您的连接字符串如下所示:

jdbc:sqlserver://x.database.windows.net:1433;DatabaseName=profile;user=x@xr;password=x;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;authentication=ActiveDirectoryPassword;

It seems you are including the jdbc driver in your build image instead of your runtime image您似乎在构建映像中包含 jdbc 驱动程序而不是运行时映像

You should copy it to your runtime images the following way: (please check the appropriate destination folder, but I assumed /usr/app/ )您应该通过以下方式将其复制到您的运行时映像:(请检查适当的目标文件夹,但我假设/usr/app/

FROM gcr.io/distroless/java
COPY --from=build /usr/src/app/target/ProfileService-0.0.1-SNAPSHOT.jar /usr/app/ProfileService-0.0.1-SNAPSHOT.jar
COPY mssql-jdbc-9.2.1.jre15.jar /usr/app/
EXPOSE 8080
ENTRYPOINT ["java","-jar","/usr/app/ProfileService-0.0.1-SNAPSHOT.jar"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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