我有一个sbt项目,可以说DemoProject依赖于另一个sbt项目commonsProject DemoProject使用了一个jars commonsProject,因为我为commonsProject创建了两个自定义图像,第二个为DemoProject创建了自定义图像 这是commonsProject的DockerFile
FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8
MAINTAINER name <name@gmail.com>
ADD . /project-commons
WORKDIR /project-commons
# Define default command.
CMD ["sbt", "publishLocal"]
然后我用docker build project-commons:v1 .
这是DemoProject的Dockerfile
FROM project-commons:v1
MAINTAINER name <name@gmail.com>
WORKDIR /project-commons
ADD . /home/user/.ivy2/local/project-commons_2.12/0.1.0-SNAPSHOT/jars/project-commons_2.12.jar
# Define default command.
CMD ["sbt","publishLocal"]
FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8
MAINTAINER name <name@gmail.com>
WORKDIR /project-demo
ADD . /project-demo
# Define default command.
CMD ["sbt", "run"]
并构建它docker build project-demo:v1
并运行
docker run project-demo
但是在日志中,我得到了project-commons.jar的未解决依赖性的例外,它说找不到jar 我在这里想念什么?我是Docker的初学者,请指导我
答案 0 :(得分:0)
您正在尝试做multi-stage build。在第二阶段(从第二个FROM
开始),您必须指定要从上一个阶段添加哪些文件:
FROM project-commons:v1 as firstStage
MAINTAINER name <name@gmail.com>
WORKDIR /project-commons
ADD . /home/user/.ivy2/local/project-commons_2.12/0.1.0-SNAPSHOT/jars/project-commons_2.12.jar
# Define default command.
CMD ["sbt","publishLocal"]
FROM hseeberger/scala-sbt:11.0.2_2.12.8_1.2.8
MAINTAINER name <name@gmail.com>
# Copy from previous stage
COPY --from=firstStage /projectCommons /projectCommons
COPY --from=firstStage /home/user/.ivy2/ /home/user/.ivy2/
WORKDIR /project-demo
ADD . /project-demo
# Define default command.
CMD ["sbt", "run"]
但是为什么不正常构建呢? (由于某种原因,您正在复制映像中的当前构建目录两次,ADD
之一可能是多余的)
FROM project-commons:v1
MAINTAINER name <name@gmail.com>
ADD . /home/user/.ivy2/local/project-commons_2.12/0.1.0-SNAPSHOT/jars/project-commons_2.12.jar
WORKDIR /project-demo
ADD . /project-demo
# Define default command.
CMD ["sbt", "run"]