Answer a question

I have a basic spring boot application with gradle which makes calls to an Oracle database and the database properties are specified in an application.properties file.

I created a Docker image of the spring boot application with the plugin "com.google.cloud.tools.jib" and using the following command:

./gradlew jibDockerBuild --image=app1

I have a docker-compose file in which i specify the image as an service and i want the application to start when i run the command: "docker-compose up"

The docker-compose file is the following:

version: '3'
    services:
        app1:
            image: "app1"
            ports:
                - "8731:8731"

But when I hit the run the "docker-compose up" command in CMD I recieve the following exception:

 java.sql.SQLRecoverableException: IO Error: The Network Adapter could not establish the connection

More informations:

  1. My Oracle database is a docker container with the name : "ORA12201_1" and port 3769
  2. Inside the application.properties the database properties specified are correct since I am amble to start the application from IntelliJ

Answers

you can connect from IntelliJ without problem as the container exposes the port (3769) to the host (your PC), but now you are trying to connect from one Docker container to another.
The containers do not share the network (isolation) so you need to connect them.

One of the recommended approach is User-defined networks

Create first a network

docker network create --driver bridge my_network

Run the applications

docker run -p 5432:5432 --network my_network -d --name=postgres postgres
docker run -p 5050:80 --network my_network -d --name=pgadmin dpage/pgadmin4

You can verify they are effectively running on the same network with

docker network inspect my_network

Spring Boot config

You can now connect from one to another using host.docker.internal as hostname, for example in your Spring Boot application.properties

spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/postgres
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐