0

I have created a docker image from a spring application but there's an error where it's not able to access mysql database and I am having issues solving this.

I created an image using Dockerfile

FROM openjdk:17-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=./sample-service.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

after the image was created, I created docker-compose.yml, here's my docker-compose.yml:


version: "3"
services:
  sample-service:
    image: v2stechit/sample-service
    ports:
        - "8080:8080"                      
    restart: always                            
    environment:
        SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/buddyto_mstr_local?useSSL=false
        SPRING_DATASOURCE_USERNAME: root
        SPRING_DATASOURCE_PASSWORD: root
    networks:
      - spring-mysql
    depends_on:
      - mysqldb
 
  mysqldb:
    image: mysql:8.0.29
    networks:
      - spring-mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=buddyto_mstr_local
      - MYSQL_USERNAME=root
      - MYSQL_PASSWORD=root
   ports:
      - 3306:3306
networks:
  spring-mysql:

But it's not opening in browser so I checked the logs and got this:

Here's the spring docker container log: https://pastebin.com/raw/pjiscq4T

Here's the mysql log:

2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-11-30 05:18:01+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-11-30T05:18:01.727948Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 1
2022-11-30T05:18:01.744335Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-11-30T05:18:01.892603Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-11-30T05:18:02.206174Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-11-30T05:18:02.206242Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-11-30T05:18:02.209090Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2022-11-30T05:18:02.251051Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-11-30T05:18:02.251175Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

How do I solve this?

I tried using the empty password environment variable for mysql.

tried changing database names, changing mysql versions to 5.6, 8 and 8.0.29

I am not sure what to do next.

I am expecting for spring application to link with mysql

DarkDead
  • 85
  • 8
  • Still same error for both of them: https://pastebin.com/raw/wqcmiUGE – DarkDead Nov 30 '22 at 05:55
  • Check this SPRING_DATASOURCE_URL: jdbc:mysql://mysqldb:3306/buddyto_mstr_local – Mohammad Javad Nov 30 '22 at 06:00
  • Yes, I am able to connect. I got the welcome to mysql screen. If I run `show databases;` it shows my 5 Databases and it does have buddyto_mstr_local – DarkDead Nov 30 '22 at 06:15
  • Yeah! like I said I can even see my database: https://imgur.com/a/QSw7EdU – DarkDead Nov 30 '22 at 06:25
  • I don't think there's firewall. I doubt there's sudo itself on it. ...and Yeah, I was right. sh: sudo no command found sh: ufw no command found – DarkDead Nov 30 '22 at 06:27
  • You don't seem to have actually included the logs in your question. Can you [edit] the question to make sure there's a clear explanation of what's actually going wrong? (I might also expect the environment variable change that @MohammadJavad suggests to help.) – David Maze Nov 30 '22 at 11:20

1 Answers1

0

localhost from within the docker will not resolve to the server's localhost. Instead you need to specify the name of the mysql service mysqldb.

So this will become: SPRING_DATASOURCE_URL: jdbc:mysql://msqldb:3306/buddyto_mstr_local?useSSL=false

You can also try by giving the IP of the server where the docker is running instead of mysqldb

Abhishek S
  • 546
  • 3
  • 16