在Dockerfile中的目录结构

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

directory structure in Dockerfile

问题

I have a directory and its structure is something like this:

  1. .
  2. ├── docker
  3. ├── docker-compose.yml
  4. └── Dockerfile
  5. ├── java-app
  6. ├── pom.xml
  7. └── src

我想创建一个 Docker 镜像,但不改变结构,但是我遇到了这个错误:

  1. => [internal] load build context 0.0s
  2. => => transferring context: 2B 0.0s
  3. => CACHED [2/5] WORKDIR /app 0.0s
  4. => ERROR [3/5] COPY java-app/pom.xml /app/ 0.0s
  5. => ERROR [4/5] COPY java-app/src /app/src 0.0s
  6. ------
  7. > [3/5] COPY java-app/pom.xml /app/:
  8. ------
  9. ------
  10. > [4/5] COPY java-app/src /app/src:
  11. ------
  12. Dockerfile:8
  13. --------------------
  14. 6 |
  15. 7 | # Copy the necessary files to the working directory
  16. 8 | >>> COPY java-app/pom.xml /app/
  17. 9 | COPY java-app/src /app/src
  18. 10 |
  19. --------------------
  20. ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref b1c85d8c-d082-47bf-843c-99a804945668a::o15zbeasfb4ieh2fetvzcczhp4a: "/java-app/pom.xml": not found

这是我的 Dockerfile

  1. # Use a base image with Java 11 installed
  2. FROM eclipse-temurin:11-jdk
  3. # Set the working directory inside the container
  4. WORKDIR /app
  5. # Copy the necessary files to the working directory
  6. COPY ../java-app/pom.xml /app/
  7. COPY ../java-app/src /app/src
  8. # Build the application
  9. RUN ./mvn package -DskipTests
  10. # Expose the port on which the application will run
  11. EXPOSE 8080
  12. # Start the application
  13. CMD ["java", "-jar", "target/your-app.jar"]

我在这里做错了什么?我会感激您的帮助。

英文:

I have a directory and it's structure is something like this:

  1. .
  2. ├── docker
  3. ├── docker-compose.yml
  4. └── Dockerfile
  5. ├── java-app
  6. ├── pom.xml
  7. └── src

I want to create a docker image without changing the structure but I am getting this error:

  1. => [internal] load build context 0.0s
  2. => => transferring context: 2B 0.0s
  3. => CACHED [2/5] WORKDIR /app 0.0s
  4. => ERROR [3/5] COPY java-app/pom.xml /app/ 0.0s
  5. => ERROR [4/5] COPY java-app/src /app/src 0.0s
  6. ------
  7. > [3/5] COPY java-app/pom.xml /app/:
  8. ------
  9. ------
  10. > [4/5] COPY java-app/src /app/src:
  11. ------
  12. Dockerfile:8
  13. --------------------
  14. 6 |
  15. 7 | # Copy the necessary files to the working directory
  16. 8 | >>> COPY java-app/pom.xml /app/
  17. 9 | COPY java-app/src /app/src
  18. 10 |
  19. --------------------
  20. ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref b1c85d8c-d082-47bf-843c-99a804945668a::o15zbeasfb4ieh2fetvzcczhp4a: "/java-app/pom.xml": not found

This is my Dockerfile

  1. # Use a base image with Java 11 installed
  2. FROM eclipse-temurin:11-jdk
  3. # Set the working directory inside the container
  4. WORKDIR /app
  5. # Copy the necessary files to the working directory
  6. COPY ../java-app/pom.xml /app/
  7. COPY ../java-app/src /app/src
  8. # Build the application
  9. RUN ./mvn package -DskipTests
  10. # Expose the port on which the application will run
  11. EXPOSE 8080
  12. # Start the application
  13. CMD ["java", "-jar", "target/your-app.jar"]

What am I doing wrong here? I would appreciate some help

答案1

得分: 1

Docker会将上下文文件夹设置为运行命令的文件夹。您可以从Dockerfile外部运行以下命令:

  1. docker build -f docker/Dockerfile .

然后,在Dockerfile中,您可以编辑COPY命令的路径,如下:

  1. COPY java-app/pom.xml /app/
  2. COPY java-app/src /app/src
英文:

Docker will set the context folder with the folder which you run the command. You can run from outside of Dockerfile

  1. docker build -f docker/Dockerfile .

And in the Dockerfile you can edit path 2 line with COPY

  1. COPY java-app/pom.xml /app/
  2. COPY java-app/src /app/src

答案2

得分: 1

文档中:

> 默认情况下,docker build命令将在构建上下文的根目录查找Dockerfile。 -f、--file选项允许您指定要使用的替代文件的路径。

因此,您可以从Java应用程序目录中使用以下命令:

  1. docker build -f ../docker/Dockerfile .
英文:

From the documentation:

> By default the docker build command will look for a Dockerfile at the root of the build context. The -f, --file, option lets you specify the path to an alternative file to use instead.

so you could, from the java app directory, use:

  1. docker build -f ../docker/Dockerfile .

答案3

得分: 0

Docker将文件集发送到服务器(称为“上下文”)

服务器只能看到这个拷贝。你不能越出它。

移动Docker文件。

英文:

Docker sends the set of files around the docker file to the server (called “context”)

The server sees this copy only. You cannot go outside of it.

Move the docker file.

huangapple
  • 本文由 发表于 2023年6月26日 15:36:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554489.html
匿名

发表评论

匿名网友

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

确定