简体   繁体   English

docker 映像构建时出错,dockerfile

[英]Error while docker image build with below dockerfile

Dockerfile Dockerfile

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
USER spring:spring
ARG DEPENDENCY=target/dependency
COPY ./target/dependency/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

Error :错误

    Sending build context to Docker daemon  36.83MB
Step 1/8 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/8 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> e9748d9baac5
Step 3/8 : USER spring:spring
 ---> Using cache
 ---> 25b83c6d3e7a
Step 4/8 : ARG DEPENDENCY=target/dependency
 ---> Using cache
 ---> 24f648b8f9ef
Step 5/8 : COPY ./target/dependency/BOOT-INF/lib /app/lib
COPY failed: stat /var/lib/docker/tmp/docker-builder226394005/target/dependency/BOOT-INF/lib: no such file or directory

1). 1)。 ARG DEPENDENCY=target/dependency -> this is not working that is why I did not change the same for other COPY lines in Dockerfile ARG DEPENDENCY=target/dependency -> 这不起作用,这就是为什么我没有对 Dockerfile 中的其他 COPY 行进行相同的更改

2). 2)。 I have a directory created as target/dependency/BOOT-INF/lib which contains all dependencies still, it says in error: no such file or directory.我有一个创建为target/dependency/BOOT-INF/lib的目录,它仍然包含所有依赖项,它错误地说:没有这样的文件或目录。

Please help with some solution and with a proper explanation regarding Dockerfile.请提供一些解决方案并提供有关 Dockerfile 的正确解释。 Any advice on Dockerizing Springboot application with Postgres DB is most welcomed.任何关于使用 Postgres DB 对 Springboot 应用程序进行 Dockerizing 的建议都非常受欢迎。

UPDATED Dockerfile:更新 Dockerfile:

FROM openjdk:8-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
RUN adduser spring root
COPY . /home/app
WORKDIR /home/app
RUN chmod -R 775 /home/app
RUN mkdir -p /home/app/lib 
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /home/app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","hello.Application"]

UPDATED Error:更新错误:

   Sending build context to Docker daemon  36.83MB
Step 1/12 : FROM openjdk:8-jdk-alpine
 ---> a3562aa0b991
Step 2/12 : RUN addgroup -S spring && adduser -S spring -G spring
 ---> Using cache
 ---> e9748d9baac5
Step 3/12 : RUN adduser spring root
 ---> Using cache
 ---> 444285d4e8d1
Step 4/12 : COPY . /home/app
 ---> 0bf717362f3e
Step 5/12 : WORKDIR /home/app
 ---> Running in 9f967425a899
Removing intermediate container 9f967425a899
 ---> bcb4e22f9faa
Step 6/12 : RUN chmod -R 775 /home/app
 ---> Running in 3110a9dd9059
Removing intermediate container 3110a9dd9059
 ---> c9ad2d9f411d
Step 7/12 : RUN mkdir -p /home/app/lib
 ---> Running in 48a172151d8b
Removing intermediate container 48a172151d8b
 ---> 11dbe4b4e21f
Step 8/12 : ARG DEPENDENCY=target/dependency
 ---> Running in 31a21dfd103f
Removing intermediate container 31a21dfd103f
 ---> ac340462402c
Step 9/12 : COPY ${DEPENDENCY}/BOOT-INF/lib /home/app/lib
COPY failed: stat /var/lib/docker/tmp/docker-builder273428680/target/dependency/BOOT-INF/lib: no such file or directory

In fact the issue cause is not so complex:实际上问题原因并不复杂:

COPY ./target/dependency/BOOT-INF/lib /app/lib

COPY failed: stat /var/lib/docker/tmp/docker-builder226394005/target/dependency/BOOT-INF/lib: no such file or directory复制失败:stat /var/lib/docker/tmp/docker-builder226394005/target/dependency/BOOT-INF/lib:没有这样的文件或目录

What does it mean?这是什么意思?
If these directories effectively exist on your host, it means that the target directory is not a direct child directory of the build context.如果这些目录有效地存在于您的主机上,则意味着target目录不是构建上下文的直接子目录。
What is the build context ?什么是构建上下文

The docker build command builds Docker images from a Dockerfile and a “context”. docker 构建命令从 Dockerfile 和“上下文”构建 Docker 图像。 A build's context is the set of files located in the specified PATH or URL.构建的上下文是位于指定 PATH 或 URL 中的文件集。 The build process can refer to any of the files in the context.构建过程可以引用上下文中的任何文件。 For example, your build can use a COPY instruction to reference a file in the context.例如,您的构建可以使用 COPY 指令来引用上下文中的文件。

When you execute: docker build -t myTag.执行时: docker build -t myTag. , the context is the current directory. ,上下文是当前目录。
So just adjust the build context to be a parent directory of the target directory and it should be correct.所以只需将构建上下文调整为目标目录的父目录,它应该是正确的。
For example with the layout:例如布局:

- docker
      - Dockerfile
- src
- target

You should keep the context as the base directory and set the DockerFile path in that way:您应该将上下文保留为基本目录并以这种方式设置 DockerFile 路径:

docker build -t myTag -f docker/Dockerfile . 

As discussed in the comment, the copy command work base on build context, so that's is why the build as target is outside of build context.正如评论中所讨论的, copy命令基于构建上下文工作,所以这就是构建作为目标在构建上下文之外的原因。

If Dockerfile inside target folders then the copy commands should be如果 Dockerfile 在目标文件夹中,则复制命令应为

copy dependency/BOOT-INF/lin /home/app/

Is the /app folder in your container already exist?容器中的/app文件夹是否已经存在? If not, just create one before copy files to it.如果没有,只需在将文件复制到它之前创建一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Dockerfile正确构建spring boot docker镜像? - how to properly build spring boot docker image using Dockerfile? 通过Jenkins SCM(Jenkinsfile和代码一起回购)构建和运行Docker镜像,Jenkins本身使用dockerfile在docker中运行 - Build And Run Docker Image through Jenkins SCM (Jenkinsfile pushed along with code to repo), Jenkins itself running in docker, using dockerfile 为springboot项目运行docker映像时出错 - Error while running docker image for springboot project 构建 docker 镜像失败: - Failed to build docker image : 在 docker-compose.yml 中构建 Dockerfile ,它位于另一条路径中 - build Dockerfile inside docker-compose.yml which is in another path 构建 docker 镜像时出错,无法在项目上执行目标 org.springframework.boot:spring-boot-maven-plugin:2.5.2:build-image (default-cli) - Error while building docker image, Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.5.2:build-image (default-cli) on project Docker - 尝试构建镜像时“无法在此平台上使用镜像操作系统”linux“” - Docker - “image operating system ”linux“ cannot be used on this platform” while trying to build image docker 运行 springboot 应用程序时出现图像抛出错误 - docker image throwing error while running springboot application 用于 sprin-boot 应用程序的 Dockerfile 无法构建映像 - Dockerfile for sprin-boot application fails to build image 在 docker 服务器上部署简单的 Spring 启动但得到 dockerfile 未找到错误 - Deploy simple Spring boot on docker server but getting dockerfile not found error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM