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