Dockerize我的spring启动应用程序,而不使用maven来构建映像

时间:2017-12-25 04:23:33

标签: spring maven docker spring-boot dockerfile

我想通过docker容器提供我的spring启动应用程序,在设置时,它需要maven插件(fabric8)来为容器构建docker镜像,我不明白,

  • 为什么需要maven来构建图像(即使是春季启动官方文档也建议这样做。https://spring.io/guides/gs/spring-boot-docker/
  • 有什么最佳做法可以将我的春季启动应用程序停靠? 请提前帮助我,

2 个答案:

答案 0 :(得分:3)

有两种构建泊坞窗图像的方法

<强> 1。使用Maven构建Docker镜像

添加dockerfile maven插件

try:
    import Tkinter as Tk
    import tkFileDialog as fileDialog
except ImportError:
    import tkinter as Tk
    import tkinter.filedialog as fileDialog

import datetime



def read_data():
    '''
    Read data from file and convert to list with datetime
    which can be used to calculate time and display.
    '''
    global data

    filename = fileDialog.askopenfilename()

    if filename:
        # read all lines
        with open(filename) as fileHandle:
            lines = fileHandle.readlines()

        # convert to `datetime` (not `timestamp`)
        data = []        
        for line in lines:
            #direction = line[:4].strip()
            #dt1 = line[5:24]
            #dt2 = line[27:46]

            direction, d1, t1, _, d2, t2 = line.split()
            dt1 = d1 + ' ' + t1
            dt2 = d2 + ' ' + t2 

            t1 = datetime.datetime.strptime(dt1, "%d.%m.%Y %H:%M:%S")
            t2 = datetime.datetime.strptime(dt2, "%d.%m.%Y %H:%M:%S")

            seconds = (t2-t1).seconds

            data.append([direction, t1, t2, seconds])

        print(data)


def processText(lines, selected_date):

    total = 0
    start = None

    print(selected_date)
    # if there is `selected_date` then convert to `datetime`
    if selected_date:
        try:
            selected_date = datetime.datetime.strptime(selected_date, "%d.%m.%Y")
        except AttributeError as ex:
            print("ERROR:", ex)
            selected_date = None

    # calculate time
    for direction, t1, t2, seconds in lines:

        if direction == "DOWN":

            # if `selected_date` then filter times
            if selected_date and t1 <= selected_date:
                continue

            if not start:
                start = t1.strftime("%d.%m.%Y %H:%M:%S")

            total += seconds

    # convert to minutes after summing all second
    total = total//60

    return total, start

def calculate():

    all_dates = entry.get().split(',')
    print(all_dates)
    all_dates = [date.strip() for date in all_dates]

    txt = ''

    for current_date in all_dates:
        down, start = processText(data, current_date)
        txt += "Total Downtime is {0} min from {1}\n".format(down, start)

    textVar.set(txt)

# --- main ---

data = None # to keep data from file

# -

root = Tk.Tk()

button = Tk.Button(root, text="Open", command=read_data)
button.grid(column=1, row=1)

textVar = Tk.StringVar(root)

label = Tk.Label(root, textvariable=textVar)
label.grid(column=1, row=2)

entry = Tk.Entry(root)
entry.grid(column=1, row=3)

button2 = Tk.Button(root, text="Calculate", command=calculate)
button2.grid(column=1, row=4)

root.mainloop()

使用命令行

构建docker镜像
<plugins>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.6</version>
            <configuration>
                <repository>${docker.image.prefix}/${project.artifactId}</repository>
    <buildArgs>
        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
    </buildArgs>
            </configuration>
        </plugin>
    </plugins>

<强> 2。使用Docker命令构建Docker镜像

  • cd到包含Dockerfile的项目目录
  • 运行maven命令

    ./ mvnw clean install

  • 运行docker build命令

    docker build。

    docker build -f / path / to / Dockerfile

答案 1 :(得分:0)

Docker镜像是Spring Boot无法提供的,因为Spring Boot本身是一个运行时框架,它告诉JVM如何运行代码。相反,Docker是关于如何运送和运行JVM本身。因此,Spring Boot是一个运行时框架,Docker镜像不是您在运行时执行的操作。您的工件(和Spring Boot)尚未运行时,您只能在构建时构建Docker镜像。

Maven是一个构建工具,因此当Maven执行构建时,它可以包括在整个构建过程中构建Docker镜像。这不仅可以使用Maven,还可以使用任何具有Docker插件的构建工具。 Maven只是一种流行的构建工具,因此它经常用于其他框架手册,但实际上Maven,Spring Boot和Docker之间没有依赖关系。 Maven只是拥有构建Spring启动应用程序并构建Docker镜像的所有工具。

要使您的应用停靠,您不需要Maven,但您需要以某种方式包含构建Docker镜像以构建应用程序的脚本。