Ayu*_*pta 2 docker docker-compose
我有以下 docker-compose.yml
version: '3.7'
services:
auth_api:
build:
dockerfile: my-server/dev.Dockerfile
context: .
ports:
- '9000:3000'
volumes:
- ./my-server:/app/
Run Code Online (Sandbox Code Playgroud)
而以下 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"]
Run Code Online (Sandbox Code Playgroud)
现在,当我跑
docker build my-server/ --file=my-server/dev.Dockerfile
Run Code Online (Sandbox Code Playgroud)
docker容器搭建成功。
但是,当我运行时docker-compose build,它失败并输出:
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
Run Code Online (Sandbox Code Playgroud)
知道如何修复它吗?
编辑
TBH 我是设置 docker-compose 等的新手,所以不太确定不同的配置选项是如何工作的。如果我可以对docker-compose.yml设置 COPY 的工作目录进行任何更改,那将是最好的
中的context:选项docker-compose.yml与 path 选项匹配docker build,但docker build如果它与构建上下文目录匹配,则似乎会去除文件前缀。如果您手动运行
docker build my-server/ --file=my-server/dev.Dockerfile
# ^^^ ^^^
# context dockerfile
Run Code Online (Sandbox Code Playgroud)
那么你需要改变docker-compose.yml匹配
build:
dockerfile: dev.Dockerfile
context: my-server
Run Code Online (Sandbox Code Playgroud)
中的所有路径Dockerfile都相对于上下文目录,因此鉴于您显示的文件,这将对应于类似的文件系统结构
docker-compose.yml
my-server/
+-- go.mod
+-- go.sum
+-- dev.Dockerfile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4445 次 |
| 最近记录: |