【问题标题】:Building a Spring Native application on my Docker Image在我的 Docker 映像上构建 Spring Native 应用程序
【发布时间】:2021-07-11 15:47:27
【问题描述】:

我正在一个 alpine (openjdk:13-alpine) docker 映像上构建我的 spring boot native 应用程序。

./mvnw spring-boot:build-image -DskipTests

执行此操作时出现错误:

[INFO] Building image 'docker.io/library/bff-distributor:0.0.1-SNAPSHOT'
[INFO] 
[INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
[INFO] Retrying request to {}->docker://localhost:2376
[INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
[INFO] Retrying request to {}->docker://localhost:2376
[INFO] I/O exception (java.io.IOException) caught when processing request to {}->docker://localhost:2376: com.sun.jna.LastErrorException: [2] No such file or directory
[INFO] Retrying request to {}->docker://localhost:2376
[INFO]  > Pulling builder image 'docker.io/paketobuildpacks/builder:tiny' 100%
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:54 min
[INFO] Finished at: 2021-04-16T15:26:34Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.4.4:build-image (default-cli) on project bff-distributor: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:2.4.4:build-image failed: Connection to the Docker daemon at 'localhost' failed with error "[2] No such file or directory"; ensure the Docker daemon is running and accessible: com.sun.jna.LastErrorException: [2] No such file or directory -> [Help 1]

我的 gitlab.ci 配置

build:
  image: openjdk:13-alpine
  stage: build
  script:
    - chmod 755 ./mvnw
    - ./mvnw spring-boot:build-image -DskipTests

有关信息:我需要它在 docker 映像上运行,因为它是我的 gitlab ci/cd 阶段的一部分。

【问题讨论】:

  • 你在本地安装了docker吗?
  • 看起来它正在尝试通过 TCP 套接字访问 Docker;这是一个非常危险的配置,通常不会启用。您是否设置了 DOCKER_HOST 环境变量?能否提供更完整的繁殖配方?
  • 我刚刚下载了一个带有 spring 初始化程序的 spring 本机应用程序,并尝试使用此脚本在我的图像中通过 gitlab ci/cd 构建它
  • spring-boot:build-image 目标需要访问 Docker 守护程序。您需要配置构建容器以访问 Docker,可能使用此处显示的“Docker-in-Docker”或“Docker 套接字绑定”解决方案:docs.gitlab.com/ee/ci/docker/using_docker_build.html

标签: spring spring-boot docker spring-native


【解决方案1】:

正如 Scott 提供的链接所说,你应该在 Docker 中使用 Docker 来做你想做的事。

所以,用 docker 镜像 (https://hub.docker.com/_/docker) 替换 openjdk 镜像并激活 dind 服务。 由于您需要 Java 来执行 Maven,您只需在“before_script”部分安装 jdk。

这是我用于同类项目的 gitlab-ci 脚本:

  variables:
    DOCKER_HOST: tcp://docker:2375
    DOCKER_TLS_CERTDIR: ""
  image: docker:20.10.8-dind-alpine3.13
  services:
    - docker:20.10.8-dind
  stage: build_push
  before_script:
    - apk add --update openjdk11
  script:
    - chmod 755 ./mvnw
    - ./mvnw spring-boot:build-image

【讨论】:

  • 在为我的 spring boot maven 项目创建 jenkins 管道脚本时,我遇到了类似的问题。我使用 'maven:3.8.1-adoptopenjdk-11' 作为我的 docker 镜像,但它可能没有 docker。我的 jenkins 服务器作为 docker 容器运行,另一个 dind docker 实例也在运行。您能否指导我在我的 Jenkinsfile 中指定的正确 docker 映像应该是什么?
猜你喜欢
  • 2017-06-04
  • 2020-12-16
  • 2020-06-09
  • 2022-09-28
  • 1970-01-01
  • 2022-01-13
  • 2019-07-31
  • 2019-07-13
  • 2020-01-27
相关资源
最近更新 更多