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=root
spring.datasource.password=root
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_DATABASE: schoolregistrationsystem
MYSQL_ROOT_PASSWORD: root
ports:
- "8083:8083"
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 12:48:29 00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 12:48:30 00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2022-07-18 12:48:30 00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.29-1.el8 started.
2022-07-18 12:48:30 00:00 [Note] [Entrypoint]: Initializing database files
2022-07-18 12:49:38 00:00 [Note] [Entrypoint]: Database files initialized
2022-07-18 12:49:38 00:00 [Note] [Entrypoint]: Starting temporary server
2022-07-18 12:49:39 00:00 [Note] [Entrypoint]: Temporary server started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-07-18T12:48:30.816755Z 0 [System] [MY013169] [Server] /usr/sbin/mysqld (mysqld 8.0.29) initializing of server in progress as process 42
2022-07-18T12:48:31.015889Z 1 [System] [MY013576] [InnoDB] InnoDB initialization has started.
2022-07-18T12:49:04.426785Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T12:49:32.656981Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2022-07-18T12:49:38.891328Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 146
2022-07-18T12:49:38.906077Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T12:49:39.036073Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T12:49:39.430759Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T12:49:39.430871Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T12:49:39.435708Z 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-18T12:49:39.486763Z 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-18T12:49:43.158453Z 13 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.29).
2022-07-18T12:49:44.206565Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29) MySQL Community Server - GPL.
2022-07-18T12:49:45.378807Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29) starting as process 1
2022-07-18T12:49:45.389248Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-07-18T12:49:45.513297Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-07-18T12:49:45.776972Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2022-07-18T12:49:45.777056Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2022-07-18T12:49:45.781265Z 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-18T12:49:45.806004Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2022-07-18T12:49:45.806180Z 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.
2022-07-18 12:49:43 00:00 [Note] [Entrypoint]: Creating database schoolregistrationsystem
2022-07-18 12:49:43 00:00 [Note] [Entrypoint]: Creating user user
2022-07-18 12:49:43 00:00 [Note] [Entrypoint]: Giving user user access to schema schoolregistrationsystem
2022-07-18 12:49:43 00:00 [Note] [Entrypoint]: Stopping temporary server
2022-07-18 12:49:45 00:00 [Note] [Entrypoint]: Temporary server stopped
2022-07-18 12:49:45 00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
CodePudding user response:
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
CodePudding user response:
Mysql by default runs on Port 3306
So you must use this port in the docker compose file and map it to 8083.
version: "3.8"
services:
api:
build: ./SchoolRegistrationSystem
ports:
- "8082:8082"
depends_on:
- database
database:
container_name: "mysql"
image: mysql:8
environment:
MYSQL_DATABASE: schoolregistrationsystem
MYSQL_ROOT_PASSWORD: root
ports:
- "8083:3306"
I would also recommend exposing it 3306 as not to confuse others.