docker-组合给出'failed to read dockerfile: error from sender'

docker-compose up gives 'failed to read dockerfile: error from sender'

试图简单地 docker 化我的 mongodb 和 spring 启动应用程序。经历了很多挣扎,我以为我几乎成功了 运行,但终端却给我带来了这个错误:

Building user
[+] Building 0.0s (1/2)
 => ERROR [internal] load build definition from Dockerfile                                                                                                                         0.0s
 => => transferring dockerfile: 121B                                                                                                                                               0.0s
------
 > [internal] load build definition from Dockerfile:
------
failed to solve with frontend dockerfile.v0: failed to read dockerfile: error from sender: walk \?\C:\Users\ZRC\Documents\GitHub\s6-kwetter-backend\user\Dockerfile: The system cannot
find the path specified.
ERROR: Service 'user' failed to build : Build failed

Dockerfile(位于子目录;用户模块):

FROM openjdk:11
EXPOSE 8081
ADD target/user-module-docker.jar user-docker.jar
CMD ["java", "-jar", "user-docker.jar"]

docker-compose.yml(在主目录下有多个modules/microservices):

version: '3.8'

services:
  user:
    build: ./user/Dockerfile
    restart: unless-stopped
    container_name: user-ms
    ports:
    - 8081:8080
  mongodb:
    image: mongo
    restart: always
    container_name: mongodb
    ports:
      - 27017:27017

就像说找不到指定的路径但它确实存在,所以我哪里出错了?

错误告诉您找不到 Dockerfile,因为路径不存在。那是因为它试图将路径输入为文件夹。

The system cannot find the path specified.

这是因为您在 compose 构建语法中犯了错误。有两种使用方法。

1。简单形式:

这是使用 ./users/ 作为上下文,期望 Dockerfile 位于此目录中。

user:
  build: ./user

2。复数形式:

user:
  build:
    context: ./
    dockerfile: ./users/Dockerfile

这让您可以将上下文和 Dockerfile 所在的位置分开。在此示例中,当前文件夹用作上下文,Dockerfile 取自 ./users/Dockerfile。当您的 Dockerfile 有不同的名称时,它也很有用。 IE。 Dockerfile.dev.

请注意,这只是一个示例,我不知道这在您的项目中是否有意义。您需要知道什么上下文是正确的。


上下文是什么意思?

The docker build command builds Docker images from a Dockerfile and a “context”. A build’s context is the set of files located in the specified PATH or 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.

例如:

docker build --file /path/to/Dockerfile /path/to/context