MSSQL DB Instance connection issue through SQLCMD when using inside a docker container

206 Views Asked by At

I am trying to write java code to automate the docker commands that I execute. I am actually creating a mssql docker container and then want to restore a DB backup in that container. I am able to achieve this using docker commands but having trouble replicating it using docker-java.

So far I have achieved this:

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();



}

But, it fails to connect with the following error:

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

Note: When I execute the following command it works:

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

I have even tried not using the network and excluding the "withNetworkMode" mapping as well while creating the container but nothing works.

Any help would be greatly appreciated!

Thanks!

0

There are 0 best solutions below