'Docker can't copy files from outsite of Dockerfile's folder [duplicate]
I can't copy file from outsite of dockerfile's folder. I'm getting error from docker-compose build:
failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount196528245/target: lstat /var/lib/docker/tmp/buildkit-mount196528245/target: no such file or directory
ERROR: Service 'discovery' failed to build : Build failed
There is the folder structure
discovery
-docker
Dockerfile
-target
discovery.jar
This is Dockerfile
FROM openjdk:11
ARG JAR_FILE=../target/discovery.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
and this is my docker-compose file
services:
discovery:
build: ./discovery/docker
ports:
- 8761:8761
Thank in adance for help.
Solution 1:[1]
This is by design.
The Docker build context (a tarball sent to the Docker daemon along with the instructions in the Dockerfile) is computed from the directory you run docker build in.
You'll need to do
$ docker build -f docker/Dockerfile
within discovery, with
ARG JAR_FILE=./target/discovery.jar
(not ..) (or pass the corresponding --build-arg).
Solution 2:[2]
AS already pointed out in AKX answer, you cannot move outside the context (upwards). I just want to show that can build your app by providing the right parameters. Since you state that, you can't go out of the docker folder.
discovery:
build:
context: discovery
Dockerfile: docker/Dockerfile
args:
JAR_FILE: target/discovery.jar
The key is to use the parent directory discovery as context, but use the Dockerfile from the child directory docker. Note that this is relative to the build context when using compose.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AKX |
| Solution 2 | marc_s |
