Docker Compose中Java和MySQL容器之间无法建立连接。

huangapple go评论80阅读模式
英文:

Docker compose connection between java and mysql container not possible

问题

我有一个 Java Spring / Hibernate 应用程序,我无法在 MySQL 和 Java 容器之间创建连接。当我只启动 MySQL 容器并在 CMD 中手动运行 Java 应用程序时,它可以正常运行。但是一旦 Java 应用程序在容器中运行,我就会遇到以下问题:

bookAPI | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
bookAPI |
bookAPI | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
bookAPI |      at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
bookAPI |      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]

如果我正确理解错误信息,Java 容器可以向 MySQL 容器发送请求,但反过来不行。我已经将 Java 容器链接到了 MySQL 容器。是否有人知道如何正确地连接这些容器?

我的 docker-compose.yml 如下:

version: '3'
 
services:
  db:
    image: mysql:5.7
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: aaaaaa
      MYSQL_DATABASE: bookAPI
      MYSQL_USER: aaaaaa
      MYSQL_PASSWORD: aaaaaa
    ports:
      - 3306:3306
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80
  java:
    container_name: bookAPI
    image: openjdk:8
    depends_on:
    - db
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c "cd /app && java -jar bookAPI.jar"  
    stdin_open: true
    tty: true
    ports:
      - 8080:8080
    links:
      - db
英文:

I have an java spring / hibernate application and I cant create a connection beween the mysql and java containers. When I start just the mysql Container and run the java application manualy in CMD it works fine. But once the java application runs in a container I get the following problem:

bookAPI | com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
bookAPI |
bookAPI | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
bookAPI |      at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]
bookAPI |      at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.21.jar!/:8.0.21]

If I understand the error correctly the java container can send to the mysql container but not the other way around. I have linked the java container to the mysql container. Anyone knows how to connect these containers correctly?

My docker-compose.yml looks like this:

version: '3'
 
services:
  db:
    image: mysql:5.7
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: aaaaaa
      MYSQL_DATABASE: bookAPI
      MYSQL_USER: aaaaaa
      MYSQL_PASSWORD: aaaaaa
    ports:
      - 3306:3306
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    links:
      - db
    environment:
      PMA_HOST: db
      PMA_PORT: 3306
      PMA_ARBITRARY: 1
    restart: always
    ports:
      - 8081:80
  java:
    container_name: bookAPI
    image: openjdk:8
    depends_on:
    - db
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c "cd /app && java -jar bookAPI.jar"  
    stdin_open: true
    tty: true
    ports:
      - 8080:8080
    links:
      - db

答案1

得分: 3

你还可以直接通过 docker-compose 的环境变量来覆盖 applications.properties 文件,如下所示:

  java:
    container_name: bookAPI
    image: openjdk:8
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c "cd /app && java -jar bookAPI.jar"
    stdin_open: true
    tty: true
    environment:
      spring.datasource.url="jdbc:mysql://db:3306/bookAPI"
    links:
      - db
    ports:
      - 8080:8080

我建议您使用 depends_on db 而不是 links:

depends_on:
  - db
英文:

You can also directly override the applications.properties with docker-compose environment variables like this :

  java:
    container_name: bookAPI
    image: openjdk:8
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c "cd /app && java -jar bookAPI.jar"
    stdin_open: true
    tty: true
    environment:
      spring.datasource.url="jdbc:mysql://db:3306/bookAPI""
    links:
      - db
    ports:
      - 8080:8080

I would recomment you tu use depends_on db instead of links

depends_on:
  - db


</details>



# 答案2
**得分**: 1

好的,我已经解决了这个问题。要将Spring应用程序与另一个Docker容器连接起来,您需要将application.properties文件中的url值从localhost或IP更改为Docker MySQL容器的名称。当在本地运行或运行maven install时,这可能会有些棘手,所以最佳做法是在容器中启动应用程序时进行覆盖。

```yaml
  java:
    container_name: bookAPI
    image: openjdk:8
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c "cd /app && java -jar bookAPI.jar --spring.datasource.url='jdbc:mysql://db:3306/bookAPI'"
    stdin_open: true
    tty: true
    links:
      - db
    ports:
      - 8080:8080
英文:

Okay, I solved the problem. To connect the spring application with another docker container you need to change the application.properties url value from localhost or ip to the docker maysql container name. Thats tricky while running it localy or run maven install so the best pracice is to overwrite it while starting the application in container.

  java:
    container_name: bookAPI
    image: openjdk:8
    volumes:
    - ./:/app
    working_dir: /app
    command: bash -c &quot;cd /app &amp;&amp; java -jar bookAPI.jar  --spring.datasource.url=&quot;jdbc:mysql://db:3306/bookAPI&quot;&quot;  
    stdin_open: true
    tty: true
    links:
      - db
    ports:
      - 8080:8080

huangapple
  • 本文由 发表于 2020年8月24日 03:27:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63551162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定