Connect to Remote MSSQL db from Linux Docker container

 ̄綄美尐妖づ 提交于 2019-12-12 03:27:27

问题


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.


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



来源:https://stackoverflow.com/questions/44333252/connect-to-remote-mssql-db-from-linux-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!