Java 当 运行 来自 docker 时出现 SIGSEGV 错误

Java SIGSEGV error when running from docker

我没有 Java 方面的经验,我可以 运行 我的代码从开发中成功。但是只有当我构建项目并从 docker 中 运行 时,我才遇到这样的错误。

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00000000000200a6, pid=1, tid=0x00007ff7227f9b10
#
# JRE version: OpenJDK Runtime Environment (8.0_212-b04) (build 1.8.0_212-b04)
# Java VM: OpenJDK 64-Bit Server VM (25.212-b04 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 3.12.0
# Distribution: Custom build (Sat May  4 17:33:35 UTC 2019)
# Problematic frame:
# C  0x00000000000200a6
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /opt/my_project/hs_err_pid1.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   https://icedtea.classpath.org/bugzilla
#

我已按照错误消息使用 ulimit -c unlimited,将其添加到 docker 命令中:docker run --ulimit core=-1 --env-file -t my_project 但仍然无济于事,并得到另一个类似的错误

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00000000000200a6, pid=1, tid=0x00007eff79c95b10
#
# JRE version: OpenJDK Runtime Environment (8.0_212-b04) (build 1.8.0_212-b04)
# Java VM: OpenJDK 64-Bit Server VM (25.212-b04 mixed mode linux-amd64 compressed oops)
# Derivative: IcedTea 3.12.0
# Distribution: Custom build (Sat May  4 17:33:35 UTC 2019)
# Problematic frame:
# C  0x00000000000200a6
#
# Core dump written. Default location: /opt/my_project/core or core.1
#
# An error report file with more information is saved as:
# /opt/my_project/hs_err_pid1.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   https://icedtea.classpath.org/bugzilla
#

这是我的 Dockerfile

FROM openjdk:8-jre-alpine
ADD ./build/libs/ /opt/my_project
RUN apk update && apk add --no-cache libc6-compat && ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2
WORKDIR /opt/my_project
CMD ["java", "-jar", "my_project.jar"]

我不知道错误是否与 java 环境或 docker 或其他原因有关。感谢您的帮助!

编辑:

在运行宁这一行时出现错误:

Logging logging = LoggingOptions.newBuilder().setCredentials(googleCredentials).setProjectId(projectId).build().getService();

这可以通过使用基于 debian 的映像而不是 alpine 来解决。

感谢评论区的小伙伴们!

这里只是记录我们自己的经历,这证实了 rahmathd 的回答。

我们使用的是基于阿尔卑斯山的 Amazon Corretto:

FROM amazoncorretto/amazoncorretto:11-alpine-jre

RUN apk --update --no-cache add tzdata && apk add --no-cache libstdc++ && apk add --no-cache libc6-compat && ln -s /lib/libc.musl-x86_64.so.1 /lib/ld-linux-x86-64.so.2

ENV TZ Asia/Tokyo

以下两条消息之一伴随着每次 运行 应用程序几秒钟时发生的崩溃。

1)

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fbd3196c5d6, pid=1, tid=60
#
# JRE version: OpenJDK Runtime Environment Corretto-11.0.8.10.1 (11.0.8+10) (build 11.0.8+10-LTS)
# Java VM: OpenJDK 64-Bit Server VM Corretto-11.0.8.10.1 (11.0.8+10-LTS, mixed mode, tiered, compressed oops, g1 gc, linux-amd64)
# Problematic frame:
# C  [ld-musl-x86_64.so.1+0x235d6]  free+0x28
#
# Core dump will be written. Default location: //core
#
# An error report file with more information is saved as:
# //hs_err_pid1.log
[thread 301 also had an error]
[thread 37 also had an error]
#
# If you would like to submit a bug report, please visit:
#   https://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

2)

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

切换到 Ubuntu 和基于亚马逊 Linux 的 Corretto 也为我们解决了这个问题。