Issue
I want to put my spring rest api with database in mysql in Docker. application.properties file:
server.port=8082
spring.datasource.url=jdbc:mysql://database:8083/schoolregistrationsystem
spring.datasource.username=user
spring.datasource.password=pwd
spring.jpa.hibernate.ddl-auto=none
spring.liquibase.change-log=classpath:/db/changelog/dbchangelog.xml
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
Next I created docker-compose.yml file in directory with project:
version: "3.8"
services:
api:
build: ./SchoolRegistrationSystem
ports:
- "8082:8082"
depends_on:
- database
database:
container_name: "mysql"
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: schoolregistrationsystem
MYSQL_USER: user
MYSQL_PASSWORD: pwd
MYSQL_TCP_PORT: 8083
healthcheck:
test: "/usr/bin/mysql --user=user --password=pwd --execute \"SHOW DATABASES;\""
interval: 5s
timeout: 2s
retries: 15
ports:
- "8083:3306"
And next when I am running docker-compose up in power shell I am getting failure. I have 2 containers: mysql - with status "running" and api with status "exited".
Logs from docker: EDIT: Full logs from docker
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-07-18 14:09:52+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 14:09:53+00:00 [Note] [Entrypoint]: Initializing database files
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Database files initialized
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Starting temporary server
2022-07-18 14:10:00+00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Creating database schoolregistrationsystem
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Creating user user
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Giving user user access to schema schoolregistrationsystem
2022-07-18 14:10:03+00:00 [Note] [Entrypoint]: Stopping temporary server
2022-07-18 14:10:04+00:00 [Note] [Entrypoint]: Temporary server stopped
2022-07-18 14:10:04+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
2022-07-18T14:09:53.062484Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.29) initializing of server in progress as process 42
2022-07-18T14:09:53.069610Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:09:53.547572Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:09:54.932915Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-07-18T14:10:00.436541Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 97
2022-07-18T14:10:00.448283Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:10:00.610010Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:10:00.862119Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T14:10:00.862179Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T14:10:00.866036Z 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-07-18T14:10:00.912594Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: /var/run/mysqld/mysqlx.sock
2022-07-18T14:10:00.912814Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leapseconds' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/tzdata.zi' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2022-07-18T14:10:03.489315Z 9 [Warning] [MY-013360] [Server] Plugin sha256_password reported: ''sha256_password' is deprecated and will be removed in a future release. Please use caching_sha2_password instead'
2022-07-18T14:10:03.622492Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.29).
2022-07-18T14:10:04.566088Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL.
2022-07-18T14:10:04.808842Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 1
2022-07-18T14:10:04.814632Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T14:10:04.919826Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T14:10:05.104905Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T14:10:05.104952Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T14:10:05.107347Z 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-07-18T14:10:05.122300Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-07-18T14:10:05.122383Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.29' socket: '/var/run/mysqld/mysqld.sock' port: 8083 MySQL Community Server - GPL.
And I will add logs from Api
om.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:828) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198) ~[mysql-connector-java-8.0.29.jar!/:8.0.29]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-4.0.3.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-4.0.3.jar!/:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:266) ~[liquibase-core-4.9.1.jar!/:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.21.jar!/:5.3.21]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.21.jar!/:5.3.21].....
Solution
Maybe you should add a health check for database
container before starting api
container. add the following lines to database
service in docker-compose.yml
and try again.
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=schoolregistrationsystem
- MYSQL_USER=user
- MYSQL_PASSWORD=pwd
- MYSQL_TCP_PORT=8083
healthcheck:
test: "/usr/bin/mysql --user=user --password=pwd --execute \"SHOW DATABASES;\""
interval: 5s
timeout: 2s
retries: 100
Answered By - h.a.
Answer Checked By - Mildred Charles (JavaFixing Admin)