1

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.

Mateusz Sobczak
  • 1,551
  • 8
  • 33
  • 67

2 Answers2

2

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).

AKX
  • 152,115
  • 15
  • 115
  • 172
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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
The Fool
  • 16,715
  • 5
  • 52
  • 86