0

I have a simple Springboot app connecting to two different SQL Server database. When all of them are hosted locally, I have no issues. But I need to have each of them in a separated docker container, when I do this I get an SQLServerException at the start of my Springboot app telling me :

 com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host '172.21.0.3', port 1434 has failed. Error: "'172.21.0.3'. Verify the connection properties.

Where 172.21.0.3 is the IP of one of my database and 1434 it's port.

I use a docker network called network_gls (which doesn't seems to work) to connect my containers (gls_app, mssql_1 & mssql_2) together, when I execute :

docker inspect network_gls

(NOTE : The execution of this line is after the start of the Springboot app container & before it's error)

I get the following result :

[
{
    "Name": "network_gls",
    "Id": "88895acb2247b3b63b0cc29656fcb6d1a0d4a8192a8c7c1bb7b79362509e0742",
    "Created": "2020-09-28T15:21:39.995019917Z",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": {},
        "Config": [
            {
                "Subnet": "172.21.0.0/16",
                "Gateway": "172.21.0.1"
            }
        ]
    },
    "Internal": false,
    "Attachable": false,
    "Ingress": false,
    "ConfigFrom": {
        "Network": ""
    },
    "ConfigOnly": false,
    "Containers": {
        "0754d8766736806549e99500c143420c556e9370c14f897f6beb82c24a3c1124": {
            "Name": "mssql_1",
            "EndpointID": "6d886cf8f2aed256d8cbc7141d9ea5242f7ce61d95ae5412c16905d1b490f133",
            "MacAddress": "02:42:ac:15:00:02",
            "IPv4Address": "172.21.0.2/16",
            "IPv6Address": ""
        },
        "54d20a9a409053eaf53eb5c7e73e340ab29c12ceaf8ac20b109d1403cba0c3d3": {
            "Name": "mssql_2",
            "EndpointID": "e675f72fc6c737201a31dd485496e749d386165eaa90a6647e0bf13507683028",
            "MacAddress": "02:42:ac:15:00:03",
            "IPv4Address": "172.21.0.3/16",
            "IPv6Address": ""
        },
        "7e4ae1a46358fe9081c5277cb52ec49681b44631d6d9c1cdcaf6116326277d37": {
            "Name": "gls_app",
            "EndpointID": "d9051cd0134f5074b2b756b44b60cced85d2cac2fd04653e0f52ddb9ada339b9",
            "MacAddress": "02:42:ac:15:00:04",
            "IPv4Address": "172.21.0.4/16",
            "IPv6Address": ""
        }
    },
    "Options": {},
    "Labels": {}
}
]

And in my Springboot application, my connection string looks like this (example of the database in mssql_2) :

 jdbc:sqlserver://172.21.0.3:1433;DatabaseName=gls

The docker networking aspect is new to me, tell me if I'm missing important information in this question

Thanks in advance

3
  • How do you start 3 containers? docker-compose? Commented Sep 30, 2020 at 10:15
  • @DuyPhan I use docker-compose up for my database and docker run for the Springboot app, you think this could be an issue ? Commented Sep 30, 2020 at 12:00
  • Have you read through background material, like Networking in Compose in the Docker documentation? Can you provide a minimal reproducible example, including the docker-compose.yml and the complete docker run command? (If you don't docker run --net on the network Compose creates, it won't work; docker inspect a container to find its IP address usually isn't necessary or a best practice.) Commented Sep 30, 2020 at 13:21

2 Answers 2

1

In my case it does work, when I use the Container Name instead of the IP address. So instead of:

 jdbc:sqlserver://172.21.0.3:1433;DatabaseName=gls

try this:

 jdbc:sqlserver://mssql_2:1433;DatabaseName=gls

Also you can try to publish your ports, if you want to test if you have problems with your networking. https://docs.docker.com/config/containers/container-networking/

Sign up to request clarification or add additional context in comments.

6 Comments

This does not work for me, still having the same SQLServerException
Did you try to publish your ports to see, if it is a network problem? Do the database dockers running properly? I would suggest to publish the database ports and try to connect with a database client, to confirm the database server are running correctly. To test this for both databases at the same time you would need to use map to different ports for each database. e.g. -p 1433:1433and -p 1434:1433
Yep, tried with : docker-compose -f ./mssql_2.yml -p 1434:1434 up
I am able to connect to the database running in the container with SQL Server Management Studio
And my other database is on 1433:1433
|
1

Edit: Thanks to David for pointing out container_name is not required. It can be connected using service name.

You can create a docker compose and use it to start your DB. Given is an example of docker compose you use and application.properties.

You can use your docker service name while connecting to db from another container. To connect from localhost, in most cases port are exposed as port:port, it can be accessed as localhost.

docker-compose build web .
docker-compose up db
docker-compose up web   

or

docker-compose up

  • You can use localhost when not accessing it from container

Docker Compose File

version: "3.3"
services:
  web:
    build:
      context: ./
      dockerfile: Dockerfile
    image: web:latest
    ports:
      - 8080:8080
    environment:
      POSTGRES_JDBC_USER: UASENAME
      POSTGRES_JDBC_PASS: PASSWORD
      SPRING_DATASOURCE_URL: "jdbc:postgresql://db:5432/DATABASE"
      SPRING_PROFILES_ACTIVE: dev

    command: mvn spring-boot:run -Dspring.profiles.active=dev
    depends_on:
      - db
      - rabbitmq

  db:
    image: "postgres:9.6-alpine"
    ports:
      - 5432:5432
    expose:
      - 5432
    volumes:
      - postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: USERNAME
      POSTGRES_PASSWORD: PASSWORD
      POSTGRES_DB: DATABASE


volumes:
  postgres:
  app:

This is application properties (for local development):

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD

Hope this will answer.

3 Comments

You do not need to specify links:, container_name:, or command: in typical use. The database URL will not be localhost: in Docker that almost always means "this container", so it will cause the Spring container to connect to the Spring container and not the database container.
@David - links removed (was not required). command - not required. localhost - is to connect from local machine and not from container, but to connect with other containers container_name is required.
You can connect to other Compose services using the name of their service block: if the block is named db: you can connect to db as a host name, without container_name:. This is described more in Networking in Compose in the Docker documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.