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

时间:2018-11-13 16:49:02

标签: java docker spring-boot raspberry-pi gitlab-ci

我想为我的Spring Boot项目使用Gitlab CI和Docker设置自动部署。为此,我在树莓派上安装了Hypriot OS,以运行docker容器。通过Gitlab CI,Maven和Docker构建运行没有错误。但是,如果我在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

application.properties

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

manifest.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}}]'

MANIFEST.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>

1 个答案:

答案 0 :(得分:0)

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

1。。将项目打包为可执行的jar文件

if(search_key!=''){

  dbo.collection("assets").aggregate([
    { 
      "$match": {  $and: [ { status: 1 },
                           { $or: [ 
                            { maker_name : '/^.*'+ search_key +'*$/i' },
                            { serial_number : '/^.*'+ search_key +'*$/i'  } 
                            ]
                           }  
                         ]
                }
    },  
    {
      $lookup:
       {
         from: 'asset_type',
         localField: 'asset_type',
         foreignField: 'asset_type_id',
         as: 'asset_type_details'
       }
     }
    ]).sort({_id:-1}).toArray(function(err, result) {
    if (err) throw err;
    res.status(200).json({'return_data': result });
    db.close();
  });

}

2。。配置构建Docker映像

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

3。。创建一个Dockerfile

4。。构建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>

有关更多信息,请参见Spring Boot with Docker guide