简体   繁体   中英

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

MyHostName,2433

I am able to run my spring boot app from my machine also with the following connection string :

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

With only MSSQL in a docker container, my application works perfectly from my machine.

Now, i want to put my spring boot app into a container as well. I have therefore built the following docker file :

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

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:sqlserver://mssql:2433;database=SomeDatabase ;

Let me know or check this how to link container in docker?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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