如何在 Dockerfile 中编写以在容器中安装 Java?

huangapple go评论73阅读模式
英文:

How to install java in container while writing it in Dockerfile?

问题

我在软件开发方面很幼稚。我想通过Dkron调度程序使用cron job运行一个jar文件。我在Docker中运行dkron(使用docker-compose up)。我正在传递"command": "java --version",以查看是否可以从Dkron运行Java。由于Docker没有安装Java,我将dockerfile.hub文件更改为以下内容:

FROM alpine
LABEL maintainer="Victor Castell <victor@victorcastell.com>"

RUN set -x \
	&& buildDeps='bash ca-certificates openssl tzdata' \ 
	&& apk add --update $buildDeps \
	&& apk add openjava8								#添加此行以安装Java
	&& rm -rf /var/cache/apk/* \
	&& mkdir -p /opt/local/dkron

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk				#添加此行以安装Java
ENV PATH $PATH:$JAVA_HOME/bin							#添加此行以安装Java

EXPOSE 8080 8946

ENV SHELL /bin/bash
WORKDIR /opt/local/dkron

COPY dkron .
COPY dkron-* ./
ENTRYPOINT ["/opt/local/dkron/dkron"]

CMD ["--help"]

当我再次运行docker-compose up时,没有任何错误,但是通过UI将"command": "java --version"传递后,dkron显示错误 - exec: "java": 在$PATH中找不到可执行文件。

我该如何解决这个问题?

提前谢谢。

英文:

I am naive in software development. I want to run a jar file from Dkron Scheduler using cron job. I am running dkron in docker(using docker-compose up). I am passing "command": "java --version" to see if I can run java from Dkron. As docker do not have java installed I changed the dockerfile.hub file to this:

FROM alpine
LABEL maintainer=&quot;Victor Castell &lt;victor@victorcastell.com&gt;&quot;

RUN set -x \
	&amp;&amp; buildDeps=&#39;bash ca-certificates openssl tzdata&#39; \ 
	&amp;&amp; apk add --update $buildDeps \
	&amp;&amp; apk add openjava8								#add this line to install java
	&amp;&amp; rm -rf /var/cache/apk/* \
	&amp;&amp; mkdir -p /opt/local/dkron

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk				#add this line to install java
ENV PATH $PATH:$JAVA_HOME/bin							#add this line to install java

EXPOSE 8080 8946

ENV SHELL /bin/bash
WORKDIR /opt/local/dkron

COPY dkron .
COPY dkron-* ./
ENTRYPOINT [&quot;/opt/local/dkron/dkron&quot;]

CMD [&quot;--help&quot;]

When I again do docker-compose up it do not give any error, still on passing "command": "java --version" by json through UI, dkron shows error - exec : "java" : executable file not found in $PATH.

What can I do to resolve it?

Thanks in advance.

答案1

得分: 1

我能够使用 dkron 作为基础镜像,在 Docker 容器中创建了一个 JVM,并在其之上构建了另一个镜像。

以下是我为运行 Java 应用程序创建的 Dockerfile:

FROM dkron/dkron
WORKDIR /root/hello-world
COPY hello.java /root/hello-world

RUN apk add openjdk8

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $JAVA_HOME/bin:$PATH

RUN javac hello.java

然后我构建了镜像,假设其 ID 为 xxx。

接着我运行了该镜像,并构建了 dkron 服务器,命令如下:

docker run -p 8080:8080 xxx agent --server --bootstrap-expect=1 --node-name=node1
英文:

I was able to create JVM in docker container using dkron as the base image, (publically available) and build another image on top of it.

Here is dockerfile I created for running java application

FROM dkron/dkron
WORKDIR /root/hello-world
COPY hello.java /root/hello-world

RUN apk add openjdk8

ENV JAVA_HOME /usr/lib/jvm/java-1.8-openjdk
ENV PATH $JAVA_HOME/bin:$PATH

RUN javac hello.java

Then I build image. Let id be xxx
Then I ran the image and build dkron server as
docker run -p 8080:8080 xxx agent --server --bootstrap-expect=1 --node-name=node1

答案2

得分: 0

尝试使用以下文件:

FROM alpine
LABEL maintainer="Victor Castell <victor@victorcastell.com>"

RUN set -x \
        && buildDeps='bash ca-certificates openssl tzdata' \
        && apk add --update $buildDeps \
        && apk add openjdk8 \
        && rm -rf /var/cache/apk/* \
        && mkdir -p /opt/local/dkron

ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"

EXPOSE 8080 8946

ENV SHELL /bin/bash
WORKDIR /opt/local/dkron

COPY dkron .
COPY dkron-* ./
ENTRYPOINT ["/opt/local/dkron/dkron"]

CMD ["--help"]

Alpine软件包中没有openjava8软件包。

编辑:更新环境变量。

英文:

Try this file

FROM alpine
LABEL maintainer=&quot;Victor Castell &lt;victor@victorcastell.com&gt;&quot;

RUN set -x \
        &amp;&amp; buildDeps=&#39;bash ca-certificates openssl tzdata&#39; \
        &amp;&amp; apk add --update $buildDeps \
        &amp;&amp; apk add openjdk8 \
        &amp;&amp; rm -rf /var/cache/apk/* \
        &amp;&amp; mkdir -p /opt/local/dkron

ENV JAVA_HOME=/usr/lib/jvm/java-1.8-openjdk
ENV PATH=&quot;$JAVA_HOME/bin:${PATH}&quot;

EXPOSE 8080 8946

ENV SHELL /bin/bash
WORKDIR /opt/local/dkron

COPY dkron .
COPY dkron-* ./
ENTRYPOINT [&quot;/opt/local/dkron/dkron&quot;]

CMD [&quot;--help&quot;]

alpine package doesn't have openjava8 packages.

Edit: Update ENV variables

huangapple
  • 本文由 发表于 2020年9月19日 13:10:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63965436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定