0

I have created a docker container for my existing local Mysql db. Although container creates tables in my local db correctly, the data in it is not passed at all and it returns an empty set constantly. Should I insert my data once again in container or is there a way to synchronize my local db and db in container? Here is the docker.complose.yml:

version: '3'

services:

  mysql_st:
    image: mysql:latest
    container_name: mysql_st
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_DATABASE: components
      MYSQL_PASSWORD: 123456
    ports:
      - "3308:3306"
  app-mysql:
    image: app-mysql:latest
    container_name: app-mysql
    ports:
        - "9090:9090"
    depends_on:
        - mysql_st
    volumes:
        - ./target/app-mysql.jar:/app-mysql.jar
    command: ["java", "-jar", "app-mysql.jar"]
    build:
      context: ./
      dockerfile: Dockerfile
    links:
        - "mysql_st"
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://mysql_st:3306/components?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

Dockerfile:

FROM openjdk:latest
ADD target/app-mysql.jar app-mysql.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","app-mysql.jar"]
VOLUME /var/lib/mysql

Application properties:

spring.datasource.username= root
spring.datasource.password= 123456
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://mysql_st:3306/components?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update
server.port = 9090

spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
  • How is your data flow working? just one database gets the data and want to sync. – abestrad Jul 02 '20 at 07:58
  • I had created already db tables with data in my local db. Later, when I created a docker container and linked it to my local db, I was hoping to synchronize the data as well. But it did not. Is there a way to do it or should I insert my data in docker container from scratch and go on using that as a reference from now on? – rightphalange Jul 02 '20 at 08:01
  • There is no link between your local DB and the one from docker. Those are 2 seperate DB instances. So ofcourse the data in your local DB will not be visible in your local DB. – M. Deinum Jul 02 '20 at 09:50
  • Sorry for my ignorance, but I was hoping to connect those two adding the part 'SPRING_DATASOURCE_URL: jdbc:mysql://mysql_st:3306/components?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC'. If not, what would be the best way to do it? – rightphalange Jul 02 '20 at 10:06

1 Answers1

-1

why a mysql docker container if you already have one mysql service on your local machine (host)?

I think the mysql service part should be removed from you composefile

and then you need to connect from the container (app-mysql) to the database running on the local machine

have a look at this: From inside of a Docker container, how do I connect to the localhost of the machine?

gaetan224
  • 586
  • 3
  • 13