错误:服务“ api-gateway”构建失败:添加失败:stat / var / lib / docker / tmp / docker-builder931060141 / api-gateway:无此类文件或目录

时间:2019-04-16 10:19:15

标签: docker docker-compose dockerfile

我在构建基于Go的Docker项目时遇到了麻烦。我的总体目录结构如下:

api-gateway
│  ├─handler
│  └─resource
   --Dockerfile

我的Dockerfile包含:

FROM alpine:3.2
ADD api-gateway /api-gateway
ADD resource/pri_key.pem resource/pub_key.pem /resource/
#ADD resource/ca-certificates.crt /etc/ssl/certs/
VOLUME /resource/
ENTRYPOINT [ "/api-gateway" ]

即使我正在使用ADD在图像中包含文件,但仍然出现错误。 api-gateway是一个包含Dockerfile在内的目录。

D:\FileWithDocument\ExtraCodeProject\shop-micro-master>docker-compose up
Building api-gateway
Step 1/5 : FROM alpine:3.2
 ---> 98f5f2d17bd1
Step 2/5 : ADD api-gateway /api-gateway
ERROR: Service 'api-gateway' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder931060141/api-gateway: no such file or directory

我在Windows中使用Docker Desktop。 Docker Engine版本为:

Client: Docker Engine - Community
 Version:           18.09.2
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        6247962
 Built:             Sun Feb 10 04:12:31 2019
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.2
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.6
  Git commit:       6247962
  Built:            Sun Feb 10 04:13:06 2019
  OS/Arch:          linux/amd64
  Experimental:     true

当我下载github项目并运行docker build时,它仍然输出此错误。

  

错误:服务“ api-gateway”构建失败:添加失败:stat / var / lib / docker / tmp / docker-builder931060141 / api-gateway:没有此类文件或目录

1 个答案:

答案 0 :(得分:0)

When you run docker build, the directory you give it becomes the context directory; you can only refer to file paths within that directory tree, and any file paths in COPY or ADD statements are always relative to that directory. That means if you're running docker build from the directory named api-gateway that contains the Dockerfile, . is the same directory. Your Dockerfile might look more like:

FROM alpine:3.2

# This will create the directory in the image if it
# doesn't already exist.
WORKDIR /api-gateway

# Copy the entire current directory into the image.
# (Prefer COPY to ADD, unless you specifically want
# automatic archive extraction or HTTP fetches.)
COPY . .

# Copy in some additional files.
# (Remember that anyone who has the image can extract any
# file from it: this leaks a private key.)
COPY resource/pri_key.pem resource/pub_key.pem /resource/
COPY resource/ca-certificates.crt /etc/ssl/certs/

# Set the default command to launch.
# (Prefer CMD to ENTRYPOINT: it is easier to get a debugging
# container with a shell, and there is a useful pattern that
# uses an ENTRYPOINT wrapper to do first-time setup before
# launching the CMD.)
CMD ["/api-gateway/handler"]

If you see a "docker-builder12345678/...: no such file or directory" error, you should always interpret the path components after the long number as relative to the directory you passed to docker build.