Docker运行错误:无法访问jar文件

6
Docker 镜像已经构建,但当我想要运行它时,出现了这个错误:
Error: Unable to access jarfile rest-service-1.0.jar

我的操作系统是Ubuntu 18.04.1 LTS,我使用docker build -t doc-service & docker run doc-service命令。

这是我的Dockerfile:

FROM ubuntu:16.04

MAINTAINER Frederico Apostolo <frederico.apostolo@blockfactory.com> (@fapostolo)

RUN apt-get update && apt-get -y upgrade

RUN apt-get install -y software-properties-common python-software-properties language-pack-en-base

RUN add-apt-repository ppa:webupd8team/java

RUN apt-get update && apt-get update --fix-missing && apt-get -y --allow-downgrades --allow-remove-essential --allow-change-held-packages upgrade \
    && echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections \
    && apt-get install -y --allow-downgrades --allow-remove-essential --allow-change-held-packages curl vim unzip wget oracle-java8-installer \
    && apt-get clean && rm -rf /var/cache/* /var/lib/apt/lists/*

ENV JAVA_HOME /usr/lib/jvm/java-8-oracle/

run java -version
run echo $JAVA_HOME

#use locate for debug
RUN apt-get update && apt-get install -y locate mlocate && updatedb

#LIBREOFFICE START
RUN apt-get update && apt-get update --fix-missing && apt-get install -y -q libreoffice \
    libreoffice-writer ure libreoffice-java-common libreoffice-core libreoffice-common \
    fonts-opensymbol hyphen-fr hyphen-de hyphen-en-us hyphen-it hyphen-ru fonts-dejavu \
    fonts-dejavu-core fonts-dejavu-extra fonts-noto fonts-dustin fonts-f500 fonts-fanwood \
    fonts-freefont-ttf fonts-liberation fonts-lmodern fonts-lyx fonts-sil-gentium \
    fonts-texgyre fonts-tlwg-purisa
#LIBREOFFICE END

#font configuration
COPY 00-odt-template-renderer-fontconfig.conf /etc/fonts/conf.d

RUN mkdir /document-service /document-service/fonts /document-service/module /document-service/logs

# local settings
RUN echo "127.0.0.1       http://www.arbs.local http://arbs.local www.arbs.local arbs.local" >> /etc/hosts
# && mkdir /logs/ && echo "dummy" >> /logs/errors.log

#EXPOSE 2115

COPY document-service-java_with_user_arg.sh /
RUN chmod +x /document-service-java_with_user_arg.sh

RUN apt-get update && apt-get -y --no-install-recommends install \
    ca-certificates \
    curl

RUN gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4
RUN curl -o /usr/local/bin/gosu -SL "https://github.com/tianon/gosu/releases/download/1.4/gosu-$(dpkg --print-architecture)" \
    && curl -o /usr/local/bin/gosu.asc -SL "https://github.com/tianon/gosu/releases/download/1.4/gosu-$(dpkg --print-architecture).asc" \
    && gpg --verify /usr/local/bin/gosu.asc \
    && rm /usr/local/bin/gosu.asc \
    && chmod +x /usr/local/bin/gosu

ENV LANG="en_US.UTF-8"

# In case someone loses the Dockerfile
# Needs to be in the end so it doesn't invalidate unaltered cache whenever the file is updated.
RUN rm -rf /etc/Dockerfile
ADD Dockerfile /etc/Dockerfile

ENTRYPOINT ["/document-service-java_with_user_arg.sh"]

this is document-service-java_with_user_arg.sh:

#!/bin/bash

USER_ID=${LOCAL_USER_ID:-9001}
USER_NAME=${LOCAL_USER_NAME:-jetty}

echo "Starting user: $USER_NAME with UID : $USER_ID"
useradd --shell /bin/bash --home-dir /document-service/dockerhome --non-unique --uid $USER_ID $USER_NAME

cd /document-service

/usr/local/bin/gosu $USER_NAME "$@" java -jar rest-service-1.0.jar

有人能帮我解决这个问题吗?


1
似乎缺少 rest-service-1.0.jar。请将其添加到您的容器、打包和类路径中。 - duffymo
为了调试,您可以运行 pwdll / dir 命令来检查命令运行的位置(/usr/local/bin/gosu $USER_NAME "$@" java -jar rest-service-1.0.jar)和给定文件夹的内容,这样您至少会知道文件是否丢失或无法访问,而 pwd 命令将告诉您正在尝试查找给定文件的位置。 - xxxvodnikxxx
rest-service-1.0.jar应放置在dockerfile的同一文件夹中。我该如何将其添加到我的容器、打包和类路径中? - Mei-Chih Chang
2个回答

0

它正在寻找有关工作目录的错误, 您必须为此副本格式选择工作目录 尝试使用 WORKDIR /yourpath/


0
根据评论,您必须在构建镜像时通过在Dockerfile中定义来添加JAR文件:
COPY rest-service-1.0.jar /document-service/rest-service-1.0.jar

你也可以只是使用以下内容:

COPY rest-service-1.0.jar /rest-service-1.0.jar

在你的入口脚本中删除cd /document-service,因为在ubuntu:16.04镜像中,默认工作目录是/。我认为在脚本中设置工作目录更安全,所以你应该选择第一个解决方案。

请注意,你也可以使用ADD而不是COPY(就像你已经在Dockerfile中做的那样),但这里只需要COPY(如果你想了解更多信息,请阅读此文章:What is the difference between the `COPY` and `ADD` commands in a Dockerfile?)。

最后,我建议你将COPY行添加到Dockerfile的末尾,这样如果构建新的JAR,镜像不会从头开始重建,而是从现有层重新构建,加快构建时间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接