出现这个问题是打包的问题,将springboot/springcloud打包成镜像,据我所知就有好几种。首先讲一下,为什么会出现/bin/sh: {java,-jar,/app.jar}: not found这个问题,是因为ENTRYPOINT {“java”,"-jar","/app.jar"}的大括号应该换成中括号,ENTRYPOINT [“java”,"-jar","/app.jar"]
- 首先来看第一种,使用的maven的插件,配置如下
<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>
使用的打包命令如下,这种方式,是我在一个网上课程学的,跟着别人做,别人可以,我不行,不知道为什么?
mvn install dockerfile:build
- 接下来看第二种打包方式,使用的这个命令,并不是maven的打包,使用的是docker自带的,但是还是出现了/bin/sh: {java,-jar,/app.jar}: not found这个问题
docker build -t eureka-server .
//eureka-server是镜像名称可以自己定,最后的一个.不要省略了,他是用来找到Dockerfile文件的,不然会打包不成功的
- 接下来,看第三种,使用的还是maven,但是插件不同,如下
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
使用的打包命令是
mvn package docker:build
打包成功运行后,的效果
2019-02-02 03:19:36.866 INFO [-,,,] 1 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6ad5c04e: startup date [Sat Feb 02 03:19:36 GMT 2019]; root of context hierarchy
2019-02-02 03:19:37.091 INFO [-,,,] 1 --- [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2019-02-02 03:19:37.125 INFO [-,,,] 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$fd2281ce] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.8.RELEASE)
2019-02-02 03:19:37.290 INFO [product-service,,,] 1 --- [ main] w.d.p.ProductServiceApplication : No active profile set, falling back to
注意:使用第三种打包方式需要配置一个,下面需要引用
<properties>
<docker.image.prefix>springcloud</docker.image.prefix>
</properties>
关于 Dockerfile的配置,基本一样如下,不同的是,第三种配置,指定了Dockerfile的路径必须为src/main/docker下,所以,你需要创建一个文件夹,或者去掉那个指定,还有,他指定了Dockerfile路径后,对jar的copy或者add都是相对路径,并且并无资源路径,直接是文件名操作。
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY product_service.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]