简体   繁体   English

Docker容器只能使用内部IP /端口访问在另一个容器中运行的MSSQL

[英]Docker container is only able to access MSSQL running inside another container with the internal IP/port

I have deployed a very simple MSSQL docker container using the following docker run command : 我已经使用以下docker run命令部署了一个非常简单的MSSQL docker容器:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=NotYourBusiness" -p 2433:1433 --name sql1 -d mcr.microsoft.com/mssql/server:2017-latest

I have SSMS installed on my machine and i'm able to connect to this instance with the following URL : 我的机器上安装了SSMS,并且可以使用以下URL连接到该实例:

MyHostName,2433

I am able to run my spring boot app from my machine also with the following connection string : 我也可以使用以下连接字符串从我的机器上运行我的spring boot应用程序:

dataSource.setJdbcUrl("jdbc:sqlserver://localhost:2433;database=SomeDatabase;");

With only MSSQL in a docker container, my application works perfectly from my machine. 在docker容器中只有MSSQL的情况下,我的应用程序可以在我的机器上完美运行。

Now, i want to put my spring boot app into a container as well. 现在,我也想将Spring Boot应用程序放入一个容器中。 I have therefore built the following docker file : 因此,我建立了以下docker文件:

FROM openjdk:11-jre-slim
VOLUME /tmp
EXPOSE 8082
ADD target/tno-1.0.jar tno-1.0.jar
ENTRYPOINT ["java","-jar","tno-1.0.jar"]

This is the build command i use to create the image : 这是我用来创建映像的构建命令:

docker build -f Dockerfile -t tno .

And this is the command i use to build the container : 这是我用来构建容器的命令:

docker run -t --name tno --link sql1:mssql -p 8082:8082 tno

Using the same connection string that works on my machine, the spring boot app fails to start with a connection refused error. 使用与我的机器上相同的连接字符串,spring boot应用程序无法启动,并显示连接被拒绝错误。 I did look at many posts and they pointed out that the term "localhost" no longer really applies when running from a container since it refers to that container only. 我确实看过很多文章,他们指出,从容器运行时,“本地主机”一词不再真正适用,因为它仅指该容器。 The only way i was able to make it work is to replace localhost:2433 with the container IP address : 1433. 我能够使其工作的唯一方法是将localhost:2433替换为容器IP地址:1433。

This is perfectly acceptable but is there a way for a container to behave like my dev machine and be able to connect to another container as if the connection was coming from the outside world? 这是完全可以接受的,但是容器是否可以像我的开发机一样工作,并且可以连接到另一个容器,就像连接来自外部世界一样?

Thanks 谢谢

If you can access to the database from the application running in other containers, try to configure your jdbc url from localhost to mssql (name of db link). 如果可以从在其他容器中运行的应用程序访问数据库,请尝试将jdbc url配置为从localhost到mssql(数据库链接的名称)。

jdbc:sqlserver://mssql:2433;database=SomeDatabase ; jdbc:sqlserver://mssql:2433;database=SomeDatabase ;

Let me know or check this how to link container in docker? 让我知道或检查一下如何在Docker中链接容器?

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

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