Aj1
Aj1

Reputation: 973

Connect to Remote MSSQL db from Linux Docker container

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.

Upvotes: 1

Views: 2327

Answers (1)

TheGameiswar
TheGameiswar

Reputation: 28940

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 ''

Upvotes: 1

Related Questions