简体   繁体   中英

Docker compose connection between java and mysql container not possible

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

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

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 "cd /app && java -jar bookAPI.jar  --spring.datasource.url="jdbc:mysql://db:3306/bookAPI""  
    stdin_open: true
    tty: true
    links:
      - db
    ports:
      - 8080:8080

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM