在Dockerfile中的目录结构

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

directory structure in Dockerfile

问题

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

.
├── docker
│ ├── docker-compose.yml
│ └── Dockerfile
├── java-app
│ ├── pom.xml
│ └── src

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

 => [internal] load build context                                                                                                  0.0s
 => => transferring context: 2B                                                                                                    0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                      0.0s
 => ERROR [3/5] COPY java-app/pom.xml /app/                                                                     0.0s
 => ERROR [4/5] COPY java-app/src /app/src                                                                      0.0s
------
 > [3/5] COPY java-app/pom.xml /app/:
------
------
 > [4/5] COPY java-app/src /app/src:
------
Dockerfile:8
--------------------
   6 |     
   7 |     # Copy the necessary files to the working directory
   8 | >>> COPY java-app/pom.xml /app/
   9 |     COPY java-app/src /app/src
  10 |     
--------------------
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

# Use a base image with Java 11 installed
FROM eclipse-temurin:11-jdk

# Set the working directory inside the container
WORKDIR /app

# Copy the necessary files to the working directory
COPY ../java-app/pom.xml /app/
COPY ../java-app/src /app/src

# Build the application
RUN ./mvn package -DskipTests

# Expose the port on which the application will run
EXPOSE 8080

# Start the application
CMD ["java", "-jar", "target/your-app.jar"]

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

英文:

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

.
├── docker
│  ├── docker-compose.yml
│  └── Dockerfile
├── java-app
│  ├── pom.xml
│  └── src

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

 => [internal] load build context                                                                                                  0.0s
 => => transferring context: 2B                                                                                                    0.0s
 => CACHED [2/5] WORKDIR /app                                                                                                      0.0s
 => ERROR [3/5] COPY java-app/pom.xml /app/                                                                     0.0s
 => ERROR [4/5] COPY java-app/src /app/src                                                                      0.0s
------
 > [3/5] COPY java-app/pom.xml /app/:
------
------
 > [4/5] COPY java-app/src /app/src:
------
Dockerfile:8
--------------------
   6 |     
   7 |     # Copy the necessary files to the working directory
   8 | >>> COPY java-app/pom.xml /app/
   9 |     COPY java-app/src /app/src
  10 |     
--------------------
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

# Use a base image with Java 11 installed
FROM eclipse-temurin:11-jdk

# Set the working directory inside the container
WORKDIR /app

# Copy the necessary files to the working directory
COPY ../java-app/pom.xml /app/
COPY ../java-app/src /app/src

# Build the application
RUN ./mvn package -DskipTests

# Expose the port on which the application will run
EXPOSE 8080

# Start the application
CMD ["java", "-jar", "target/your-app.jar"]

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

答案1

得分: 1

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

docker build -f docker/Dockerfile .

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

COPY java-app/pom.xml /app/
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

docker build -f docker/Dockerfile .

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

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

答案2

得分: 1

文档中:

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

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

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:

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:

确定