无法使用Docker Compse将Java程序连接到MySQL

时间:2019-12-01 19:30:32

标签: docker docker-compose

我有一个需要数据库连接的应用程序。我正在尝试运行它,但无法正常运行。仅创建具有db的容器。在我看来,它可以正常工作,因为如果我在Eclips中运行应用程序,它将连接到数据库容器。

应用程序属性

server.port=9200
spring.datasource.url=jdbc:mysql://localhost:3306/currency-converter1?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1
spring.jpa.properties.hibernate.id.new_generator_mappings=false
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.messages.encoding=UTF-8

Dockerfile

FROM openjdk:14-ea-15-jdk-alpine3.10
ADD target/currencyConverter-0.0.1-SNAPSHOT.jar .
EXPOSE 9200
CMD java -jar currencyConverter-0.0.1-SNAPSHOT.jar

docker-compose.yml

version: '3'                         
services:                             
  api:                                
    build: ./currencyConverter    
    ports:
    - "9200:9200"                   
    depends_on:                       
    - localhost
  localhost:
    container_name: localhost      
    image: mysql                      
    environment:                      
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: currency-converter1
    ports:
    - "3306:3306"
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
api_1        |
api_1        | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

0 个答案:

没有答案