1

I have a linux docker container built on microsoft/mssql-server-linux/ image. The container doesn't have anything at this moment, I am trying to connect to remote MSSQL db hosted on windows server somewhere. I am not sure exactly how can I do that.

The documentation for microsoft/mssql-server-linux/ doesn't provide much of details. Any help would be appreciated.

Updated: I have got container working now. But, the container gets exited with code 0 if I try to create and seed db through bash script.

Here is docker file & docker-compose files

version: '2'
services:
  db:
    build: .
    ports:
      - "1433:1433"
    environment:
        ACCEPT_EULA: Y
        SA_PASSWORD: Password
        PATH: /opt/mssql-tools/bin:/opt/mssql/bin:$PATH
    container_name: db

FROM microsoft/mssql-server-linux:latest
EXPOSE 1433
RUN echo $PATH
RUN mkdir -p /usr/src/app
COPY ./db/* /usr/src/app/
WORKDIR /usr/src/app
RUN ls
RUN chmod +x /usr/src/app/dbInit.sh
RUN chmod +x /usr/src/app/dbSeed.sh
CMD /bin/bash ./entrypoint.sh

Here is my entrypoint.sh:

/opt/mssql/bin/sqlservr & /usr/src/app/dbInit.sh

dbInit.sh contains SQLscripts to create db, some tables and seed them. something like this.

sqlcmd -S localhost -U SA -P password -d master -Q "CREATE DATABASE dbo"

The docker-compose up --build successfully creates db, tables and seeds them. But the container is exited with code 0. Seems like SQL Server it self is closed.

4
  • That image is the server, if you have already server elsewhere, you really shouldn't be using that. Commented Jun 2, 2017 at 16:12
  • @James Thank you. I think i should change my question. So, i need to access remote sql db from docker container. Any ideas or thoughts? I have a postgres db i am able to connect to it from docker container using postgres image. But not with MSSQL. Commented Jun 2, 2017 at 16:21
  • Hi James, I still don't understand what you're trying to achieve. Are you connecting a mssql engine to another mssql engine? Are you trying to use linked servers? Or are you just trying to connect to mssql running on Docker from another docker container that includes an application? Commented Jun 2, 2017 at 17:38
  • I am trying to connect remote db on windows server from a docker container. And use this container as a link to api docker container. Commented Jun 2, 2017 at 17:42

1 Answer 1

1

The documentation is pretty clear..see the Connect and Query for more details

1.First you have to install SQLCMD tools,as they are not installed automatically

  • Import the public repository GPG keys.

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -

  • Register the Microsoft Ubuntu repository

curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list

  • Update the sources list and run the installation command with the unixODBC developer package.

sudo apt-get update sudo apt-get install mssql-tools unixodbc-dev

You can check out for further enhancements here :Install tools on Ubuntu

now you can query like below

for local :

sqlcmd -S localhost -U SA -P ''

For remote:

sqlcmd -S 192.555.5.555 -U SA -P ''

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your response. I tried this, got the following error. Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login timeout expired . Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : TCP Provider: Error c ode 0x2749. Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Se rver. Server is not found or not accessible. Check if instance name is correct.....
Thank you for the help !!!. I have got that working now, but there seems to be an issue with container being closed. I have updated my question. Thank you once again.
I am not sure,can you please add an update section and add more details
There is an option to ouput sqlcmd errorlog to file ,try adding that and also try adding some kind of print after final line ,I am mobile,will try to check more once I am at laptop
@Aj1:can you try above mentioned steps and update question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.