简体   繁体   English

docker-compose 构建失败,没有这样的文件或目录

[英]docker-compose build fails with no such file or directory

I have the following docker-compose.yml我有以下 docker-compose.yml

version: '3.7'
services:
  auth_api:
    build:
      dockerfile: my-server/dev.Dockerfile
      context: .
    ports:
      - '9000:3000'
    volumes:
      - ./my-server:/app/

And the following my-server/dev.Dockerfile以及以下my-server/dev.Dockerfile

FROM golang:1.13.3

WORKDIR /app

RUN ls
# Copy go mod and sum files
COPY ./go.mod ./go.sum ./


# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed
RUN go mod download

# Copy the source from the current directory to the Working Directory inside the container
COPY . .

# Build the Go app
RUN go build -o main .

# Expose port 8080 to the outside world
EXPOSE 3000

# Command to run the executable
CMD ["./main"]

Now, when I run现在,当我跑步时

docker build my-server/ --file=my-server/dev.Dockerfile

The docker container is built successfully. docker 容器构建成功。

However, when I run docker-compose build , It fails with the output:但是,当我运行docker-compose build时,它会因 output 而失败:

Building auth_api
Step 1/9 : FROM golang:1.13.3
 ---> dc7582e06f8e
Step 2/9 : WORKDIR /app
 ---> Using cache
 ---> c4005150884a
Step 3/9 : RUN ls
 ---> Using cache
 ---> 59051268c382
Step 4/9 : COPY ./go.mod ./go.sum ./
ERROR: Service 'auth_api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder881802488/go.mod: no such file or directory

Any idea how to fix it?知道如何解决吗?

EDIT编辑

TBH I'm new to setting up docker-compose etc, so not really sure of how the different config options work. TBH 我是设置 docker-compose 等的新手,所以不太确定不同的配置选项是如何工作的。 If there is any change I could make to my docker-compose.yml which would set the working directory for COPY, that would be best如果我可以对我的docker-compose.yml进行任何更改,这将为 COPY 设置工作目录,那将是最好的

The context: option in the docker-compose.yml matches the path option to docker build , except that docker build seems to strip off a file prefix if it matches the build context directory. docker-compose.yml 中的context:选项与docker-compose.yml docker build的路径选项匹配,除了docker build似乎会删除文件前缀,如果它与构建上下文目录匹配。 If you're manually running如果您手动运行

docker build my-server/ --file=my-server/dev.Dockerfile
#               ^^^                          ^^^
#             context                     dockerfile

then you need to change the docker-compose.yml to match那么您需要更改docker-compose.yml以匹配

build:
  dockerfile: dev.Dockerfile
  context: my-server

All paths in the Dockerfile are relative to the context directory, so given the files you show, this would correspond to a filesystem structure like Dockerfile中的所有路径都相对于上下文目录,因此鉴于您显示的文件,这将对应于文件系统结构,例如

docker-compose.yml
my-server/
+-- go.mod
+-- go.sum
+-- dev.Dockerfile

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM