COPY failed: stat /var/lib/docker/tmp/docker-xxx : no such file or directory
When you run
docker build . --file backend/Dockerfile ...
The path argument .
becomes the context directory. (Docker actually sends itself a copy of this directory tree, which is where the /var/lib/docker/tmp/...
path comes from.) The source arguments of COPY
and ADD
instructions are relative to the context directory, not relative to the Dockerfile.
If your source tree looks like
.
+-- backend
| \-- Dockerfile
\-- target
\-- demo-0.0.1-SNAPSHOT.jar
that matches the Dockerfile you show. But if instead you have
.
+-- backend
+-- Dockerfile
\-- target
\-- demo-0.0.1-SNAPSHOT.jar
you'll get the error you see.
If you don't need to refer to anything outside of the context directory, you can just change what directory you're passing to docker build
COPY target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
docker build backend ...
Or, if you do have other content you need to copy in, you need to change the COPY
paths to be relative to the topmost directory.
COPY backend/target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar
COPY common/config/demo.yml /opt/demo-0.0.1/etc/demo.yml
docker build . -f backend/Dockerfile ...
WORKDIR just tells you from where the other commands will be executed.An important point is WORKDIR works w.r.t docker directory,not to local/git directory.As per your example, WORDIR does not take context to /opt/demo-0.0.1/ , but just creates an empty directory as /opt/demo-0.0.1/ inside the docker. In order to make dockerfile work, you should give full path in COPY command as COPY /opt/demo-0.0.1/target/demo-0.0.1-SNAPSHOT.jar /opt/demo-0.0.1/lib/demo-0.0.1-SNAPSHOT.jar.Make sure Dockerfile is at the same level as /opt directory.