dockerfile 中的命令 运行 spring 启动部署在 jboss 服务器上的应用程序
Commands in dockerfile to run spring boot application deployed on jboss server
我是 docker 的新人。我有一个在 jboss-eap-7.2 上 运行ning 的 springboot 应用程序。通常当我想要 运行 应用程序时,首先我必须 运行 在 cmd 中命令 mvn clean package 然后在构建成功后,我 运行 standalone.bat -c standalone_wingsure.xml,效果很好。
我想 运行 这个应用程序在 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 clean install 之后 war 文件生成在 /target folder。
然后我通过 运行ning 命令 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
主要class是-
@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);
}
}
java
命令没有 -war
选项。
如果您想使用 jboss,那么您需要将 war 部署到 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
我是 docker 的新人。我有一个在 jboss-eap-7.2 上 运行ning 的 springboot 应用程序。通常当我想要 运行 应用程序时,首先我必须 运行 在 cmd 中命令 mvn clean package 然后在构建成功后,我 运行 standalone.bat -c standalone_wingsure.xml,效果很好。 我想 运行 这个应用程序在 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 clean install 之后 war 文件生成在 /target folder。
然后我通过 运行ning 命令 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
主要class是-
@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);
}
}
java
命令没有 -war
选项。
如果您想使用 jboss,那么您需要将 war 部署到 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