简体   繁体   English

无法连接到travis中docker内的SQL Server数据库

[英]Can't connect to SQL Server database inside docker in travis

I have trouble connecting to a SQL Server database inside a docker container in travis.我无法连接到 travis 中 docker 容器内的 SQL Server 数据库。

Everything is working fine on my machine the error only occurs inside travis, can't figure out why.在我的机器上一切正常,错误只发生在 travis 内部,无法弄清楚原因。

Dockerfile:文件:

from microsoft/mssql-server-linux:2017-latest

ENV ACCEPT_EULA=Y

ENV MSSQL_SA_PASSWORD=Passw0rd

COPY hermes.bak /var/opt/mssql/backup/hermes.bak

CMD ["/opt/mssql/bin/sqlservr"]

.travis.yml: .travis.yml:

dist: trusty

services:
  - docker

before_install:
  - docker build -t hermes-db .
  - docker run -d -p 127.0.0.1:1430:1433 --name hermes-db hermes-db
  - docker exec hermes-db /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'Passw0rd' -Q "RESTORE DATABASE [lbc_refs] FROM DISK = N'/var/opt/mssql/backup/hermes.bak' WITH MOVE 'lbc_refs' TO '/var/opt/mssql/data/lbc_refs_data.mdf', MOVE 'lbc_refs_log' TO '/var/opt/mssql/data/lbc_refs_log.ldf'"

script:
  - docker ps | grep -q hermes-db

Ending up with:结束于:

Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AF9.
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..

When I run netstat -tulpn:当我运行 netstat -tulpn 时:

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:1430          0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      -               
tcp6       0      0 :::22                   :::*                    LISTEN      -               
udp        0      0 0.0.0.0:11211           0.0.0.0:*                           -               
udp        0      0 172.18.0.1:123          0.0.0.0:*                           -               
udp        0      0 172.17.0.5:123          0.0.0.0:*                           -               
udp        0      0 127.0.0.1:123           0.0.0.0:*                           -               
udp        0      0 0.0.0.0:123             0.0.0.0:*                           -               
udp6       0      0 :::123                  :::*                                -               

I encountered same problem on multi tenants application using SQL Server on Travis CI.我在 Travis CI 上使用 SQL Server 的多租户应用程序遇到了同样的问题。

Here is the final build: https://travis-ci.org/github/ron190/jsql-injection/builds/663658271这是最终版本: https ://travis-ci.org/github/ron190/jsql-injection/builds/663658271

After a lot of try and errors I managed to stabilize connection on port 1433 following these rules:经过大量尝试和错误后,我按照以下规则设法稳定了端口 1433 上的连接:

  • Use image 2019-GDR1-ubuntu-16.04 instead of 2017-latest使用图像2019-GDR1-ubuntu-16.04而不是2017-latest
  • EXPOSE 1434 1433 in Dockerfile, 1434 for sqlcmd , 1433 for Hibernate on C3P0 EXPOSE 1434 1433 in Dockerfile, 1434 for sqlcmd , 1433 for Hibernate on C3P0
  • Add sleep 10s before testing first connection with sqlcmd在测试与sqlcmd的第一次连接之前添加sleep 10s
  • Use password that complies with sql server password policy使用符合sql server密码策略的密码

Some details about my debugging on travis-ci.community .关于我在travis-ci.community上调试的一些细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM