[英]docker-compose java cannot access jarfile
I'm trying to run a Spring Boot app as one of the services in a Docker Compose setup, but whenever I run docker-compose up -d
the container running the Spring Boot app fails and exits immediately. 我试图将Spring Boot应用程序作为Docker Compose设置中的服务之一运行,但是每当我运行docker-compose up -d
,运行Spring Boot应用程序的容器就会失败并立即退出。 For purposes of this post I'm using generic names like "mycontainer" and "myfile". 出于本文的目的,我使用的通用名称是“ mycontainer”和“ myfile”。
Running docker-compose logs core
shows that the error message is: 运行docker-compose logs core
显示错误消息为:
mycontainer | Error: unable to access jarfile myjar.jar
My docker-compose file is: 我的docker-compose文件是:
version: '3.3'
services:
webapp:
image: webapp_image
container_name: webapp-container
build:
context: ./webapp
dockerfile: Dockerfile
volumes:
- './webapp:/usr/src/app'
ports:
- 3000:3000
- 35729:35729
environment:
- NODE_ENV=development
- env_file=./webapp/.env
core:
image: core_image
container_name: core-container
build:
context: ./core
dockerfile: Dockerfile
volumes:
- './core:/usr/src/core'
- './algorithms:/usr/src/algorithms'
ports:
- 8080:8080
environment:
- JARFILE=myjar.jar
- JAVA_OPTIONS=-Djava.security.egd=file:/dev/./urandom
The Dockerfile for the core
service, which is the one running the Spring Boot app, is this: core
服务的Dockerfile是运行Spring Boot应用程序的Dockerfile,它是这样的:
FROM openjdk:8-jdk-alpine
RUN mkdir -p /usr/src/core
RUN mkdir -p /usr/src/algorithms
ADD ./myproject/target/myjar.jar /usr/src/core
ADD ./algorithms /usr/src/algorithms
WORKDIR /usr/src/core
CMD java $JAVA_OPTIONS -jar $JARFILE
If I run an individual container from the core_image
image, and set the same environment vars that I defined in the docker-compose file, I can launch the Spring Boot app successfully, like so: 如果我从core_image
映像运行单个容器,并设置与core_image
-compose文件中定义的环境变量相同的环境变量,则可以成功启动Spring Boot应用程序,如下所示:
$ docker run -it -p 8080:8080 --name mycontainer core_image sh
$ /usr/src/core # ls -l
total 15852
-rwxr-xr-x 1 root root 16228579 Apr 21 22:44 myjar.jar
$ /usr/src/core # java $JAVA_OPTIONS -jar $JARFILE
But if I run a container from the same image as part of docker-compose up -d
the container fails with the error that Java couldn't access the jarfile. 但是,如果我在与docker-compose up -d
相同的映像中运行一个容器,则该容器将失败,并显示Java无法访问jarfile的错误。 I've tried specifying the absolute path to the jarfile in the CMD
of the Dockerfile, but that doesn't help when running from Docker Compose. 我尝试在Dockerfile的CMD
中指定jarfile的绝对路径,但是从Docker Compose运行时这无济于事。
Does anyone know what might be causing this? 有谁知道这可能是什么原因? I'm on a Windows 10 host. 我在Windows 10主机上。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
It seems you are overriding content of /usr/src/core [where you copy the jar file to] by mounting content from ./core in docker-compose. 看来您是通过在docker-compose中装载./core中的内容来覆盖/ usr / src / core的内容(将jar文件复制到其中)。 Remove below mount from services->core->volumes and give a try. 从服务->核心->卷中删除下面的挂载,然后尝试。 volumes: - './core:/usr/src/core'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.