我想在docker容器上运行一个jar,它工作正常,但如果我使用需要从配置文件中读取的jar,它就会失败。
接下来是我的docker-compose.yml:
jar:
image: frolvlad/alpine-oraclejdk8:slim
ports:
- "3000:3000"
volumes:
- /tmp
- "./farr-api-0.1.0.jar:/fagor/app.jar"
- "./config/application.yml:/fagor/config/application.yml"
command: sh -c 'touch /fagor/app.jar'
environment:
- JAVA_OPTS=""
entrypoint: [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /fagor/app.jar" ]
例外:
jar_1 | Exception in thread "main" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
jar_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:62)
jar_1 | at java.lang.Thread.run(Thread.java:745)
jar_1 | Caused by: java.lang.reflect.InvocationTargetException
jar_1 | at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
jar_1 | at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
jar_1 | at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
jar_1 | at java.lang.reflect.Method.invoke(Method.java:498)
jar_1 | at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:54)
jar_1 | ... 1 more
jar_1 | Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private long es.ikerlan.farr_api.config.AppConfig.id; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'plants[0].plant.id' in string value "${plants[0].plant.id}"
答案 0 :(得分:3)
这是一个简单的dockerfile,可以将您的应用程序复制到映像,安装卷并将配置复制到卷。
FROM frolvlad/alpine-oraclejdk8:slim
MAINTAINER Asier
VOLUME /config
COPY your_app.jar app.jar
COPY your_config.yml config/application.yml
RUN touch /app.jar
ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar
撰写文件可以像
一样简单version: '2'
services:
farrapi_container:
container_name: farrapicontainer
build:
context: ./
dockerfile: Dockerfile
ports:
- "3000:3000"
通过这个你应该能够通过主机更改配置。确保该应用程序正确引用配置文件。 无论如何,我仍然会尝试通过环境设置完成配置。在spring boot中使用ENV设置覆盖application.properties中的设置非常简单。
答案 1 :(得分:2)
这不是答案,只是想展示如何使用jar
测试docker run
。现在我们是:
docker run -it -p 3000:3000 \
-v ./farr-api-0.1.0.jar:/fagor/app.jar \
-v ./config/application.yml:/fagor/config/application.yml \
frolvlad/alpine-oraclejdk8:slim \
/bin/bash
# Now when you're inside the container
# Make sure you can run Java
java -version
# Make sure you had your files
ls -la /fagor/app.jar
ls -ls /fagor/config/application.yml
# Run it
export JAVA_OPTS=""
java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /fagor/app.jar