Docker compose-up SpringBoot Mysql

Viewed 643

m new in docker i;m watching videos on youtube and i want make my first container, of course it's a lot of problem.

this is my error.

enter image description here

docker-compose.yml:

version: "3.2"
 services:

   autoshop:
    container_name: autoshop-spring
    image: autoshop
    restart: on-failure
    depends_on:
      - autoshopdb
    ports:
      - "8081:8080"

  autoshopdb:
    image: mysql
    container_name: autoshopdb
    environment:
      MYSQL_ROOT_PASSWORD: dominik372
      MYSQL_DATABASE: shopspring
      MYSQL_USER: root
      MYSQL_PASSWORD: dominik372
    ports:
      - "3307:3306"

application.proporties

server.port=8080
spring.datasource.url=jdbc:mysql://autoshopdb:3306/shopspring?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=dominik372
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true

and this is my Dockerfile

FROM openjdk:15-jdk-alpine
ADD target/autoshop-0.0.1-SNAPSHOT.jar autoshop-0.0.1-SNAPSHOT.jar
EXPOSE 8080
CMD java -jar autoshop-0.0.1-SNAPSHOT.jar

i need help !

2 Answers

It is important to note the distinction between HOST_PORT and CONTAINER_PORT. In your example, for autoshopdb, the HOST_PORT is 3307 and the container port is 3306 (mysql default). Networked service-to-service communication uses the CONTAINER_PORT. When HOST_PORT is defined, the service is accessible outside the swarm as well.

Within the web container, your connection string to autoshopdb would look like mysql://autoshopdb:3306, and from the host machine, the connection string would look like mysql://{DOCKER_IP}:3307

See more information here: https://docs.docker.com/compose/networking/

So, it seems that correct mysql url in your case should be:

spring.datasource.url=jdbc:mysql://{DOCKER_IP}:3307/shopspring?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false

but you could also try:

spring.datasource.url=jdbc:mysql://autoshopdb:3307/shopspring?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false

maybe it will work

Here is the example of "docker-compose" with mysql and spring boot images: https://github.com/eventuate-tram/eventuate-tram-sagas/blob/master/docker-compose-mysql.yml

According to this example your settings should look like this:

docker-compose.yml:

  autoshopdb:
    image: mysql
    container_name: autoshopdb
    environment:
      MYSQL_ROOT_PASSWORD: dominik372
      MYSQL_DATABASE: shopspring
      MYSQL_USER: root
      MYSQL_PASSWORD: dominik372
    ports:
      - 3306:3306

application.properties:

spring.datasource.url=jdbc:mysql://autoshopdb/shopspring?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=dominik372

Or according to this example: https://dev.to/scottshipp/docker-compose-a-spring-boot-app-backed-by-mysql-89m

You could try this:

docker-compose.yml:

  autoshopdb:
    image: mysql
    container_name: autoshopdb
    environment:
      MYSQL_ROOT_PASSWORD: dominik372
      MYSQL_DATABASE: shopspring
      MYSQL_USER: root
      MYSQL_PASSWORD: dominik372
    ports:
      - 3306:3306

application.properties:

spring.datasource.url=jdbc:mysql://autoshopdb:3306/shopspring?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=dominik372

Hope it will help

Related