Dockerized Spring Boot应用程序无法连接到数据库容器 [英] Dockerized Spring Boot app can't connect to database container

查看:62
本文介绍了Dockerized Spring Boot应用程序无法连接到数据库容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有gradle和Hibernate的基本Spring Boot应用程序,可以调用Oracle数据库,该数据库位于docker容器中.

I have a basic Spring Boot application with gradle and Hibernate which makes calls to an Oracle database, the database being in a docker container.

我使用插件"com.google.cloud.tools.jib"并使用以下命令创建了Spring Boot应用程序的Docker映像:

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

我已经创建了一个用户定义的网络(我们称之为 my_network ),并且已经将数据库容器附加到上述网络中.

I already created a user-defined network ( let's call it my_network ) and I've attached the database container to the above mentioned network.

我有一个docker-file,即使我将映像指定为服务并在运行时使用 my_network ,我在CMD中命中"docker-compose up"命令时,也会收到以下异常:

I have a docker-file in which even though I specify the image as an service and use my_network when I run I hit the "docker-compose up" command in CMD I receive the following exception:

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

docker-compose文件如下:

The docker-compose file is the following:

version: '3.7'

networks:
    my_network:
        external: 
            name: my_network

services:
   app1:
   image: "app1"
   ports:
     - "9090:9090"
   environment: 
     SPRING_DATASOURCE_URL: "jdbc:oracle:thin:@localhost:32769/ORCLPDB1.localdomain"
     SPRING_DATASOURCE_USERNAME: username
     SPRING_DATASOURCE_PASSWORD: password 
   networks:
     - my_network

尝试过的解决方案,但没有结果:

Tried solutions but with no result:

  1. 将指定的数据库作为服务,因此基本上让它在我第一次点击"docker-compose up"时创建,并且在服务app1中指定了标签depends_on,但是创建的数据库容器将处于运行状况:app1拥有后很长的启动状态被启动并崩溃了
  2. 让网络从docker-compose文件中创建,但行为与所询问的问题相同
  3. 在数据库URL中用 host.docker.internal 替换了localhost,但它表示这是未知主机
  1. Specified database as service so basically let it be created the first time I hit "docker-compose up" and in service app1 specified the tag depends_on but the database container created would be in a health:starting state long after the app1 had been started and crashed
  2. Let the network be created inside from the docker-compose file but same behavior as in the asked question
  3. Replaced localhost with host.docker.internal in the database url but it said is was an unknown host

推荐答案

在Docker组成的文件内部.创建Oracle数据库容器,然后创建您的API容器,如下所示

Inside your Docker-compose file. Create Oracle Database container then your API container as follows

version: '3.7'
services:
  oracle-database:
      image: Your oracleImage
      environment:
         //define your oracle db settings..eg default password,users etc
      volumes:
            - /data/oracle
      ports:
        - 127.0.0.1: 32769:32769/tcp

  api1:
    image: api1
    ports:
      - 127.0.0.1:9090:9090/tcp
    depends_on:
     - oracle-database
    environment:
       - DATABASE_HOST=oracle-database
       - DATABASE_USER= username
       - DATABASE_PASSWORD=password
       - DATABASE_NAME= NameOfYourDatabase
       - DATABASE_PORT= 32769

这篇关于Dockerized Spring Boot应用程序无法连接到数据库容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆