dockerfile中的命令运行部署在jboss服务器上的spring boot应用程序

时间:2019-05-21 10:50:09

标签: spring-boot docker jboss

我是Docker的新手。我有一个在jboss-eap-7.2上运行的springboot应用程序。通常,当我想运行应用程序时,首先必须在cmd中运行命令 mvn clean package ,然后在构建成功后,运行 standalone.bat -c standalone_wingsure.xml < / em> ,效果很好。 我想在docker容器中运行此应用程序。所以我创建了一个 DockerFile ,其中包含以下代码。

FROM openjdk:8
ADD target/wingsure_crop_insurance-0.0.1.war wingsure_crop_insurance-0.0.1.war
EXPOSE 8080
ENTRYPOINT ["java", "-war", "wingsure_crop_insurance-0.0.1.war"]

此springboot应用程序具有mysql数据库连接。所以我为mysql镜像创建了docker容器。这是我的 appliaction.properties 文件。

spring.datasource.url=jdbc:mysql://mysql-standalone:3306/crop_insurance
spring.datasource.username=chronito
spring.datasource.password=mysql

我的pom.xml-

<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wingsure</groupId>
<artifactId>wingsure_crop_insurance</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
<name>wingsure_crop_insurance</name>
<description>Crop Insurance</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <!-- <version>3.1.0</version> -->
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency> -->
    <!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -->
    <!-- <dependency>
        <groupId>org.springframework.security.oauth</groupId>
        <artifactId>spring-security-oauth2</artifactId>
        <version>2.3.5.RELEASE</version>
    </dependency> -->


    <dependency>
         <groupId>botlibre-ai</groupId>
         <artifactId>botlibre-ai</artifactId>
         <scope>system</scope>
         <version>1.0</version>
         <systemPath>${basedir}\src\lib\botlibre-ai.jar</systemPath>
  </dependency>
  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
</dependency>
   <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
  </dependency>
  <dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>27.1-jre</version>
</dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <outputDirectory>${env.JBOSS_HOME}/standalone/deployments/</outputDirectory>

            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
        </plugin>

        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                    <mainClass>com.wingsure.WingsureCropInsuranceApplication</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

mvn全新安装 后,将在 / target文件夹 中生成war文件。 然后,我通过运行命令D:\chronito\checkout\wingsure_crop_insurance\wingsure_crop_insurance>docker build . -t wingsure_crop_insurance-0.0.1

为应用程序构建映像

构建映像后,我通过与mysql容器链接来在容器中运行该映像,它成功运行了该容器,没有任何错误。

D:\chronito\checkout\wingsure_crop_insurance\wingsure_crop_insurance>docker run -p 8080:8080 --name wingsure_crop_insurance-0.0.1 --link mysql-standalone:mysql -d wingsure_crop_insurance-0.0.1
7db57549b4c94ee0546275b962482a95b215f7aaf576be3d82340ec1934f117f

但是当我看到日志时,我得到这样的错误-

D:\chronito\checkout\wingsure_crop_insurance\wingsure_crop_insurance>docker logs wingsure_crop_insurance-0.0.1
Unrecognized option: -war
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

我不知道我在做什么错。任何建议都将非常有帮助。

在EbrahimPasbani解决方案之后,先前的错误消失了,但没有启动springboot应用程序。在运行docker命令

之后
D:\chronito\checkout\wingsure_crop_insurance\wingsure_crop_insurance>docker run -p 8080:8080 --name wingsure_crop_insurance-0.0.1 --link mysql-standalone:mysql -d wingsure_crop_insurance-0.0.1
7b3836cb4da2d6ab1c1a55b1fa2c3ac500c99372313388cdeb54f24249e073bd

它成功运行了容器并将日志显示为

=========================================================================

12:34:52,374 INFO  [org.jboss.modules] (main) JBoss Modules version 1.8.6.Final-redhat-00001

I> No access restrictor found, access to any MBean is allowed
Jolokia: Agent started with URL https://172.17.0.3:8778/jolokia/

12:34:53,501 INFO  [org.jboss.msc] (main) JBoss MSC version 1.4.5.Final-redhat-00001

12:34:53,520 INFO  [org.jboss.threads] (main) JBoss Threads version 2.3.2.Final-redhat-1

主要课程是-

@SpringBootApplication
@EnableConfigurationProperties({
FileStorageProperties.class
})
public class WingsureCropInsuranceApplication extends 
SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder 
application)
{
    return application.sources(WingsureCropInsuranceApplication.class);
}
public static void main(String[] args) {
    SpringApplication.run(WingsureCropInsuranceApplication.class, args);
}
}

1 个答案:

答案 0 :(得分:1)

java命令没有-war选项。
如果要使用jboss,则需要在docker中将战争部署到jboss。
因此,docker文件应来自jboss,如下所示:

#start from eap72-openshift
FROM registry.access.redhat.com/jboss-eap-7/eap72-openshift

# Copy war to deployments folder
COPY target/wingsure_crop_insurance-0.0.1.war $JBOSS_HOME/standalone/deployments/

# User root to modify war owners
USER root

# Modify owners war
RUN chown jboss:jboss $JBOSS_HOME/standalone/deployments/wingsure_crop_insurance-0.0.1.war

# Important, use jboss user to run image
USER jboss