title: Can't Connect to External SQL Server From Docker Container
body: <p>I've developed a SpringBoot(Java) application that calls out to an external SQL Server on port 1433.  The SQL Server instance is located on-premises (not local SQL Server instances).  However, it's reachable from my desktop using either IntelliJ or SQL Clients.</p>
  
  <p>I am using the Microsoft SQL Server JDBC connector to communicate with the instances.</p>
  
  <p>If I run the app from IntelliJ all is well, the app can call the SQL Server, execute the command and returns a resultsset.</p>
  
  <p>However, now I'm trying to Dockerize the api app. The container does the usual SpringBoot initialization but when it tries to call the SQL Server I get the following error:</p>
  
  <pre><code>com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host myexternalsqlserver.domain, port 1433 has failed. Error: "myexternalsqlserver.domain. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
      at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:285)
      at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2434)
      at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:659)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2546)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2216)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2067)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1204)
      at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:825)
      at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql/java.sql.DriverManager.getConnection(Unknown Source)
      at com.symetra.SdsApi.SqlConnector.getResultSet(SqlConnector.java:26)
  </code></pre>
  
  <p>This is my Docker file</p>
  
  <pre><code>FROM openjdk:11.0.4-jre-slim-buster
  VOLUME /tmp
  
  COPY target/myapi-1.0-SNAPSHOT.jar app.jar
  EXPOSE 8080
  EXPOSE 1433
  ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=docker -jar /app.jar" ]
  </code></pre>
  
  <p>And these are the Docker commands I use to build and run the container:</p>
  
  <pre><code>docker build -t myapi . &amp;&amp; docker run -p 8080:8080 -p 1433:1433 --name myapi myapi "java","-jar","myapi-1.0-SNAPSHOT.jar"
  </code></pre>
  
  <p>I don't think the mapping of port 1433 is necessary, it's just an experiment.  I should be able to communicate outside of my container through to port 1433 on the host. Port 1433 isn't being blocked on the host because I have no problems running this outside the container.</p>
  
  <p>Finally, this is the connection string I'm using:</p>
  
  <pre><code>//Create Connection Url.
  String connectionUrl="jdbc:sqlserver://myexternalsqlserver.domain:1433;database=mydb;user=MyUser;password=MyPassword";
  </code></pre>
  
  <p>I'm not sure what I'm doing wrong here. I'm wondering if I need to set up Docker networking.</p>
  
  <p>Thanks for your help!</p>