gpt4 book ai didi

mysql - Docker-compose up 第一次运行时抛出错误,第二次运行后工作正常

转载 作者:行者123 更新时间:2023-11-29 15:15:40 25 4
gpt4 key购买 nike

当我第一次运行 docker-compose up 时,它会抛出错误并且无法工作,第二次运行后一切正常。看起来第一次运行期间缺少某些内容,但在第二次运行期间可用。我希望它在第一次运行后起作用。

这些是我的配置文件:

docker-compose.yml:

version: '3'
services:
api:
build: .
ports:
- "10012:10012"

database:
image: mysql
container_name: database
environment:
MYSQL_PASSWORD: ***
MYSQL_ROOT_PASSWORD: ***
MYSQL_USER: user
MYSQL_DATABASE: currencydb
ports:
- "3306:3306"
depends_on:
- api

Dockerfile:

FROM openjdk:8
ADD target/currency-rest-0.0.1-SNAPSHOT.jar .
EXPOSE 10011
CMD java -jar currency-rest-0.0.1-SNAPSHOT.jar

应用程序属性:

server.port=10012

spring.jpa.ddl-auto=create
spring.datasource.url=jdbc:mysql://database:3306/currencydb?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

spring.datasource.username=root
spring.datasource.password=***
spring.jpa.hibernate.ddl-auto=update
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true

首次运行后控制台输出:

PS C:\Users\wuher\desktop\currency-rest> docker-compose up
docker-compose : Building api
At line:1 char:1
+ docker-compose up
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Building api:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Step 1/4 : FROM openjdk:8
---> f8146facf376
Step 2/4 : ADD target/currency-rest-0.0.1-SNAPSHOT.jar .
---> fd52b0529742
Step 3/4 : EXPOSE 10011
---> Running in dba375d741f8
Removing intermediate container dba375d741f8
---> 2dd51a3c0782
Step 4/4 : CMD java -jar currency-rest-0.0.1-SNAPSHOT.jar
---> Running in 7d204a8fc8d5
Removing intermediate container 7d204a8fc8d5
---> 1010d46a1bb8
Successfully built 1010d46a1bb8
Successfully tagged currency-rest_api:latest
Image for service api was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating currency-rest_api_1 ...
[1A[2KCreating currency-rest_api_1 ... [32mdone[0m[1B
Creating database ...
[1A[2KCreating database ... [32mdone[0m[1B
Attaching to currency-rest_api_1, database
[33mdatabase |[0m 2020-01-12 23:44:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
[33mdatabase |[0m 2020-01-12 23:44:32+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
[33mdatabase |[0m 2020-01-12 23:44:32+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.18-1debian9 started.
[33mdatabase |[0m 2020-01-12 23:44:32+00:00 [Note] [Entrypoint]: Initializing database files
[33mdatabase |[0m 2020-01-12T23:44:32.472398Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future rele
ase.
[33mdatabase |[0m 2020-01-12T23:44:32.472481Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 45
[36mapi_1 |[0m
[36mapi_1 |[0m . ____ _ __ _ _
[36mapi_1 |[0m /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
[36mapi_1 |[0m ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
[36mapi_1 |[0m \\/ ___)| |_)| | | | | || (_| | ) ) ) )
[36mapi_1 |[0m ' |____| .__|_| |_|_| |_\__, | / / / /
[36mapi_1 |[0m =========|_|==============|___/=/_/_/_/
[36mapi_1 |[0m :: Spring Boot :: (v2.2.2.RELEASE)
[36mapi_1 |[0m
[36mapi_1 |[0m 2020-01-12 23:44:32.707 INFO 6 --- [ main] p.k.c.CurrencyRestApplication : Starting CurrencyRestApplication v0.0.1-SNAPSHOT on b5facd58e175 with PID 6 (/currency-rest-0.0.1-SNAPSHOT.jar started by root in /)
[36mapi_1 |[0m 2020-01-12 23:44:32.750 INFO 6 --- [ main] p.k.c.CurrencyRestApplication : No active profile set, falling back to default profiles: default
[36mapi_1 |[0m 2020-01-12 23:44:33.783 INFO 6 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[36mapi_1 |[0m 2020-01-12 23:44:33.904 INFO 6 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 104ms. Found 3 JPA repository interfaces.
[36mapi_1 |[0m 2020-01-12 23:44:34.426 INFO 6 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotat
ion.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
[36mapi_1 |[0m 2020-01-12 23:44:34.822 INFO 6 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 10012 (http)
[36mapi_1 |[0m 2020-01-12 23:44:34.841 INFO 6 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
[36mapi_1 |[0m 2020-01-12 23:44:34.841 INFO 6 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
[33mdatabase |[0m 2020-01-12T23:44:35.145873Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[36mapi_1 |[0m 2020-01-12 23:44:35.391 INFO 6 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were sc
anned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
[36mapi_1 |[0m 2020-01-12 23:44:35.697 INFO 6 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
[36mapi_1 |[0m 2020-01-12 23:44:35.697 INFO 6 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2840 ms
[36mapi_1 |[0m 2020-01-12 23:44:36.207 INFO 6 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
[36mapi_1 |[0m 2020-01-12 23:44:36.293 INFO 6 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.4.9.Final}
[36mapi_1 |[0m 2020-01-12 23:44:36.499 INFO 6 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
[36mapi_1 |[0m 2020-01-12 23:44:36.665 INFO 6 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
[36mapi_1 |[0m 2020-01-12 23:44:36.770 ERROR 6 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
[36mapi_1 |[0m
[36mapi_1 |[0m com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
[36mapi_1 |[0m
[36mapi_1 |[0m The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[36mapi_1 |[0m at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~]
/////
cut part of code
/////
[36mapi_1 |[0m Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
[36mapi_1 |[0m
[36mapi_1 |[0m The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[36mapi_1 |[0m at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_232]
[36mapi_1 |[0m at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_232]
[36mapi_1 |[0m at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_232]
[36mapi_1 |[0m at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m ... 57 common frames omitted
[36mapi_1 |[0m Caused by: java.net.ConnectException: Connection refused (Connection refused)
[36mapi_1 |[0m at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_232]
[36mapi_1 |[0m at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_232]
[36mapi_1 |[0m at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
[36mapi_1 |[0m ... 60 common frames omitted
[36mapi_1 |[0m
[36mapi_1 |[0m 2020-01-12 23:44:36.771 WARN 6 --- [ main] o.h.e.j.e.i.JdbcEnvironmentInitiator : HHH000342: Could not obtain connection to query metadata : Communications link failure
[36mapi_1 |[0m
[36mapi_1 |[0m The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
[36mapi_1 |[0m 2020-01-12 23:44:36.775 WARN 6 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceExce
ption: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
[36mapi_1 |[0m 2020-01-12 23:44:36.779 INFO 6 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
[36mapi_1 |[0m 2020-01-12 23:44:36.798 INFO 6 --- [ main] ConditionEvaluationReportLoggingListener :
[36mapi_1 |[0m
[36mapi_1 |[0m Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[36mapi_1 |[0m 2020-01-12 23:44:36.801 ERROR 6 --- [ main] o.s.boot.SpringApplication : Application run failed
///////
cut part of info
///////
[36mcurrency-rest_api_1 exited with code 1
[0m[33mdatabase |[0m 2020-01-12 23:44:38+00:00 [Note] [Entrypoint]: Database files initialized
[33mdatabase |[0m 2020-01-12 23:44:38+00:00 [Note] [Entrypoint]: Starting temporary server
[33mdatabase |[0m 2020-01-12T23:44:38.668558Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future rele
ase.
[33mdatabase |[0m 2020-01-12T23:44:38.668651Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 94
[33mdatabase |[0m 2020-01-12T23:44:39.078017Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
[33mdatabase |[0m 2020-01-12T23:44:39.082365Z 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.
[33mdatabase |[0m 2020-01-12T23:44:39.102708Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server - GPL.
[33mdatabase |[0m 2020-01-12 23:44:39+00:00 [Note] [Entrypoint]: Temporary server started.
[33mdatabase |[0m 2020-01-12T23:44:39.243716Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
[33mdatabase |[0m Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
[33mdatabase |[0m Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
[33mdatabase |[0m Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
[33mdatabase |[0m Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
[33mdatabase |[0m 2020-01-12 23:44:41+00:00 [Note] [Entrypoint]: Creating database currencydb
[33mdatabase |[0m 2020-01-12 23:44:41+00:00 [Note] [Entrypoint]: Creating user user
[33mdatabase |[0m 2020-01-12 23:44:41+00:00 [Note] [Entrypoint]: Giving user user access to schema currencydb
[33mdatabase |[0m
[33mdatabase |[0m 2020-01-12 23:44:41+00:00 [Note] [Entrypoint]: Stopping temporary server
[33mdatabase |[0m 2020-01-12T23:44:41.782901Z 14 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
[33mdatabase |[0m 2020-01-12T23:44:43.508856Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.18) MySQL Community Server - GPL.
[33mdatabase |[0m 2020-01-12 23:44:43+00:00 [Note] [Entrypoint]: Temporary server stopped
[33mdatabase |[0m
[33mdatabase |[0m 2020-01-12 23:44:43+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
[33mdatabase |[0m
[33mdatabase |[0m 2020-01-12T23:44:44.029991Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future rele
ase.
[33mdatabase |[0m 2020-01-12T23:44:44.030080Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 1
[33mdatabase |[0m 2020-01-12T23:44:44.448592Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
[33mdatabase |[0m 2020-01-12T23:44:44.452896Z 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.
[33mdatabase |[0m 2020-01-12T23:44:44.467507Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
[33mdatabase |[0m 2020-01-12T23:44:44.636049Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060

我做错了什么?

最佳答案

根据您的docker-compose,您希望您的应用程序数据库之前运行。

但是应用程序依赖于MySQL,如果它没有启动SpringBoot将抛出错误。

请像这样更改您的depends_on 配置。

version: '3'
services:
api:
build: .
ports:
- "10012:10012"
depends_on:
- database
database:
image: mysql
container_name: database
environment:
MYSQL_PASSWORD: ***
MYSQL_ROOT_PASSWORD: ***
MYSQL_USER: user
MYSQL_DATABASE: currencydb
ports:
- "3306:3306"

更详细请参阅here 。搜索depends_on

关于mysql - Docker-compose up 第一次运行时抛出错误,第二次运行后工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59709324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com