在Docker容器中使用时通过SQLCMD的MSSQL数据库实例连接问题

时间:2018-08-15 02:02:06

标签: java sql-server docker docker-java

我正在尝试编写Java代码来自动执行我执行的docker命令。我实际上是在创建一个mssql docker容器,然后想要在该容器中还原数据库备份。我可以使用docker命令来实现此目的,但无法使用docker-java复制它。

到目前为止,我已经实现了这一目标:

public static void main(String[] args) throws InterruptedException {
    BasicConfigurator.configure();
    DockerClient dockerClient
            = DockerClientBuilder.getInstance("tcp://localhost:1234").build();

    Volume volume1 = new Volume("/var/opt/mssql/backup"); //target

    //Network Creation
    CreateNetworkResponse networkResponse
            = dockerClient.createNetworkCmd()
            .withName("java-docker-mssql")
            .withDriver("bridge").exec();


    //Pulling an image
    dockerClient.pullImageCmd("microsoft/mssql-server-linux")
            .withTag("latest")
            .exec(new PullImageResultCallback())
            .awaitCompletion(30, TimeUnit.SECONDS);


    //Container Creation
    CreateContainerResponse container
            = dockerClient.createContainerCmd("microsoft/mssql-server-linux:2017-latest")
            .withPortBindings(PortBinding.parse("1433:1433"))
            .withEnv("ACCEPT_EULA=Y", "SA_PASSWORD=P@ssw0rd")
            .withVolumes(volume1)
            .withBinds(new Bind("/Users/robhit_saxena/Downloads/test-bind", volume1)) //is source
            .withName("mssql-from-java")
            .withNetworkMode("java-docker-mssql")
            .exec();

    //Starting a container
    dockerClient.startContainerCmd(container.getId()).exec();
    String containerId = container.getId();


    //Executing commands in a running container

    ExecCreateCmdResponse execCreateCmdResponse = dockerClient.execCreateCmd(container.getId())
            .withAttachStdout(true)
            .withAttachStderr(true)
            .withCmd("bash", "-c", "mkdir -p /var/opt/mssql/backup")
            .exec();


    dockerClient.execStartCmd(execCreateCmdResponse.getId()).exec(
            new ExecStartResultCallback(System.out, System.err)).awaitCompletion();

    ExecCreateCmdResponse execCreateCmdResponse1 = dockerClient.execCreateCmd(container.getId())
            .withAttachStdout(true)
            .withAttachStderr(true)
            .withCmd("bash", "-c", "/opt/mssql-tools/bin/sqlcmd -S localhost    -U SA -P 'P@ssw0rd'")
            .exec();


    dockerClient.execStartCmd(execCreateCmdResponse1.getId()).exec(
            new ExecStartResultCallback(System.out, System.err)).awaitCompletion();



}

但是,它无法连接并出现以下错误:

14253 [dockerjava-jaxrs-async-2] DEBUG org.apache.http.wire  - http-outgoing-4 << "[0x2][0x0][0x0][0x0][0x0][0x0][0x2][0x5]Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.[\n]"

14253 [dockerjava-jaxrs-async-2] DEBUG org.apache.http.wire  - http-outgoing-4 << "Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2749.[\n]"

14253 [dockerjava-jaxrs-async-2] DEBUG org.apache.http.wire  - http-outgoing-4 << "Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..[\n]"

14254 [dockerjava-jaxrs-async-2] DEBUG com.github.dockerjava.jaxrs.JerseyDockerCmdExecFactory  - 14 * Client response received on thread dockerjava-jaxrs-async-2

注意:当我执行以下命令时,它会起作用:

docker exec -it mssql-from-java /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'P@ssw0rd' -Q 'RESTORE FILELISTONLY FROM DISK = "/var/opt/mssql/backup/BSP-39251DBDump.bak"' | tr -s ' ' | grep ldf | cut -d ' ' -f 1

我什至尝试在创建容器时不使用网络并排除“ withNetworkMode”映射,但没有任何效果。

任何帮助将不胜感激!

谢谢!

0 个答案:

没有答案