DeAaron
DeAaron

Reputation: 21

ERROR: Service 'api-gateway' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder931060141/api-gateway: no such file or directory

I am having trouble building a Go-based Docker project. My overall directory structure looks like:

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

My Dockerfile contains:

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" ]

Even though I'm using ADD to include a file in the image, I'm still getting an error. api-gateway is a directory that includes the Dockerfile inside.

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

I use Docker Desktop in Windows. Docker Engine version is:

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

When i download the github project and run docker build, it still outputs this error.

ERROR: Service 'api-gateway' failed to build: ADD failed: stat /var/lib/docker/tmp/docker-builder931060141/api-gateway: no such file or directory

Upvotes: 1

Views: 1260

Answers (1)

David Maze
David Maze

Reputation: 159750

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.

Upvotes: 1

Related Questions