Spring-Boot API container can’t connect to MySQL container on same user-defined Docker network [duplicate]

This question already has an answer here:

I am running 2 containers. The first is a Java Spring-Boot REST API/service. The second is a MySQL database. I am trying to perform a simple GET request from the running API container to the running MySQL container that should return all of the records from a table in the MySQL db container.

When both containers are running I try to make a simple GET request to http://localhost:15505/users/all or http://172.18.0.3:15505/users/all (via browser or Postman), I am getting an error:

{
    "timestamp": "2019-02-16T16:51:39.127+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection",
    "path": "/users/all"
}

When I inspect the container logs for the API via docker logs <container-id> I see a more detailed error message indicating issues connecting to the MySQL DB container:

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

What I have done to troubleshoot

I am able to run the API from IntelliJ locally, run the MySQL container, perform the GET request, and receive the expected response (i.e. all records from the users table).

I am able to connect to the running MySQL container via MySQL Workbench. I can perform any DB actions from this client (i.e. INSERT/SELECT/DELETE etc).

I created a Docker user-defined network local-network. When I created my containers using docker run, I added both to this network. When I inspect the network I see both containers on that network.

I have used docker exec <container-id> bash for each container. I was able to successfully perform a ping in both directions – by container ID and by container name.

From Spring-Boot APIapplication.properties

server.port=15505

spring.datasource.url=jdbc:mysql://localhost:3306/snc_users?useSSL=false
spring.datasource.username=root
spring.datasource.password=pass123

spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

Containers collection from docker network inspect local-network

"Containers": {
            "61a9c53af62c26bedbf0ed8ffaeed2b97e61f7cff1e9644b71ffa2f45877c2e6": {
                "Name": "snc-users-api-local",
                "EndpointID": "3dc0a3abd3571cedf7d2250d35868b604531a75abb2091bfde1a9a444889d7eb",
                "MacAddress": "02:42:ac:12:00:03",
                "IPv4Address": "172.18.0.3/16",
                "IPv6Address": ""
            },
            "efda04231e97a46c9d16c3825d6b370b475f315387c92be2ca86f87e80533786": {
                "Name": "snc-dev-db",
                "EndpointID": "151bdc6c7a8a5a85aba15bfcb5c522cfc390b9b54ac57295f6ac989e08363ad8",
                "MacAddress": "02:42:ac:12:00:02",
                "IPv4Address": "172.18.0.2/16",
                "IPv6Address": ""
            }
        },

Docker Commands Used To Create & Start Containers

  • MySQL Container
    docker run -p 3306:3306 -d --name snc-dev-db -e MYSQL_ROOT_PASSWORD=pass123 --network local-network mysql:5.6.41

  • Spring-Boot API Container
    docker run -p 15505:15505 -d --name snc-users-api-local --network local-network 5386a58f10c2
    NOTE: 5386a58f10c2 is the ID for my API image

I expect to be able to be able to hit the route http://localhost:15505/users/all or http://172.18.0.3:15505/users/all and receive a 200 response with all of the records in my users table.

Source: StackOverflow