gpt4 book ai didi

java - Dockerized Spring Boot 无法在 Raspberry Hyperiot OS + Gitlab CI + Spring Boot 上运行

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:51 25 4
gpt4 key购买 nike

我想使用 Gitlab CI 和 Docker 为我的 Spring Boot 项目设置自动部署。为此,我在树莓派上安装了 Hypriot OS 来运行我的 docker 容器。 Maven 和 Docker 构建通过 Gitlab CI 运行没有错误。但如果我在 Raspberry 上运行 docker,什么也不会发生。

gitlab-ci.yml

image: docker:latest

services:
- docker:dind

variables:
DOCKER_DRIVER: overlay
SPRING_PROFILES_ACTIVE: gitlab-ci
USER_GITLAB: ft
APP_NAME: ft-backend
REPO: backend

stages:
- build
- docker

maven-build:
image: maven:3-jdk-8
stage: build
script: "mvn package -B"
artifacts:
paths:
- target/*.jar

docker-build:
stage: docker
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker build -t registry.gitlab.com/ft/$REPO .
- docker push registry.gitlab.com/ft/$REPO

应用程序属性

spring.profiles.active=local
server.port=8080
spring.application.name=backend

Dockerfile

FROM jsurf/rpi-java:latest
VOLUME /tmp
ADD target/ft-backend-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","app.jar"]
CMD [""]

我在 Raspberry 上使用以下命令来启动 Docker。但如果我想在 192...:8080 上访问我的 Raspberry,我的项目不会出现。 docker ps 也不显示任何内容。

docker login registry.gitlab.com
docker pull registry.gitlab.com/ft/backend
docker run -d -p 8080:8080 registry.gitlab.com/ft/backend:latest

更新

项目结构

enter image description here

list .yml

applications:
- name: hello
disk_quota: 512M
instances: 1
memory: 256M
random-route: true
timeout: 120
path: ./target/ft-backend-0.0.1-SNAPSHOT.jar
env:
JBP_CONFIG_OPEN_JDK_JRE: '[memory_calculator: {stack_threads: 100, memory_sizes: {stack: 128k, native: 150m}}]'

list .MF

Manifest-Version: 1.0
Start-Class: hello.Application

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.tf</groupId>
<artifactId>ft-backend</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>hello.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>

最佳答案

要构建 Spring Boot maven 项目的 docker 镜像,我建议使用 dockerfile-maven-plugin .

1.配置将项目打包为可执行jar

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
...

2.配置构建 Docker 镜像

  ...
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-plugin.version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.repository}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>

3.创建 Dockerfile

4.构建 Docker 镜像

$ mvn clean package

参见Spring Boot with Docker guide了解更多信息。

关于java - Dockerized Spring Boot 无法在 Raspberry Hyperiot OS + Gitlab CI + Spring Boot 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53285828/

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