1

我想为我的 Spring Boot 项目使用 Gitlab CI 和 Docker 设置自动部署。为此,我在我的树莓派上安装了 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 上访问我的树莓派,我的项目就不会出现。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

更新

项目结构

在此处输入图像描述

清单.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-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>
4

1 回答 1

0

要构建 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 指南

于 2018-11-14T12:22:58.520 回答