Dockerfile, getting "failed to compute cache key" in the build

Hello all,
I am new to Docker and Github.
I have forked the famous repo of robot-shop into my own Github account:

After I have done it I have changed some files locally. Every time I have done docker-compose pull and docker-compose up I was getting the image from the main instana Github. Therefore, I have entered to the Dockerfile in order to change a folder that it will copy directly from my local folder.

This is the Dockerfile, what I have changed it is bold is only the last line, only one folder!

FROM alpine AS build
ARG KEY

WORKDIR /instana

RUN apk add --update --no-cache curl

ENV ARTI_PATH='https://artifact-public.instana.io/artifactory/shared/com/instana/nginx_tracing/'

RUN if [ -n "$KEY" ]; then \
    sensor_version=$(curl --user "_:$KEY" ${ARTI_PATH} | grep -o '>[0-9]\+\.[0-9]\+\.[0-9]\+'| cut -f 2 -d '>'|sort -V|tail -1 ); \
    echo "Downloading sensor version ${sensor_version} for Nginx version 1.21.6" ; \
    curl \
    --output instana.zip \
    --user "_:$KEY" \
    ${ARTI_PATH}/${sensor_version}/linux-amd64-glibc-nginx-1.21.6.zip && \
    unzip instana.zip && \
    mv glibc-libinstana_sensor.so libinstana_sensor.so && \
    mv glibc-nginx-1.21.6-ngx_http_ot_module.so ngx_http_opentracing_module.so; \
    else echo "KEY not provided. Not adding tracing"; \
    touch dummy.so; \
    fi


FROM nginx:1.21.6

EXPOSE 8080

ENV CATALOGUE_HOST=catalogue \
    USER_HOST=user \
    CART_HOST=cart \
    SHIPPING_HOST=shipping \
    PAYMENT_HOST=payment \
    RATINGS_HOST=ratings \
    INSTANA_SERVICE_NAME=nginx-web

# Instana tracing
COPY --from=build /instana/*.so /tmp/

COPY entrypoint.sh /root/
ENTRYPOINT ["/root/entrypoint.sh"]

COPY default.conf.template /etc/nginx/conf.d/default.conf.template
COPY /home/thesonics/robot-shop/web/static /usr/share/nginx/html

I have run a build without a catch in order to build it again correctly.
Sadly, I got the following error:

> [stage-1 5/5] COPY /home/thesonics/robot-shop/web/static /usr/share/nginx/html:
------
failed to compute cache key: "/home/thesonics/robot-shop/web/static" not found: not found
ERROR: Service 'web' failed to build : Build failed

Is there an option just to change part of the Dockerfile?
If not, how can I create one, I am very new at this tech.

Thanks,

Ido

Just to ask the obvious: does the file /home/thesonics/robot-shop/web/static exist on your local machine?

Hi,

It is a folder and it is there:

thesonics@worker01:~/robot-shop$ cd web/static/
thesonics@worker01:~/robot-shop/web/static$ ls -l
total 56
-rw-rw-r-- 1 thesonics thesonics 1335 יונ 19 23:17 cart.html
drwxrwxr-x 2 thesonics thesonics 4096 יונ 19 23:17 css
-rw-rw-r-- 1 thesonics thesonics 77 יונ 19 23:17 empty.html
-rw-rw-r-- 1 thesonics thesonics 444 יונ 19 23:17 eum-tmpl.html
drwxrwxr-x 2 thesonics thesonics 4096 יונ 19 23:17 images
-rw-rw-r-- 1 thesonics thesonics 2588 יונ 20 14:25 index.html
drwxrwxr-x 2 thesonics thesonics 4096 יונ 19 23:17 js
-rw-rw-r-- 1 thesonics thesonics 2169 יונ 19 23:17 login.html
drwxrwxr-x 2 thesonics thesonics 4096 יונ 20 14:25 media
-rw-rw-r-- 1 thesonics thesonics 1328 יונ 19 23:17 payment.html
-rw-rw-r-- 1 thesonics thesonics 1371 יונ 19 23:17 product.html
-rw-rw-r-- 1 thesonics thesonics 559 יונ 19 23:17 search.html
-rw-rw-r-- 1 thesonics thesonics 843 יונ 19 23:17 shipping.html
-rw-rw-r-- 1 thesonics thesonics 1182 יונ 19 23:17 splash.html

Maybe I need to add another “/” in the end?

Dockerfile reference | Docker Docs states:

COPY obeys the following rules:

  • The <src> path must be inside the context of the build (…)
  • (…)

See also How to include files outside of Docker's build context? - Stack Overflow.

1 Like

I don’t fully understand it.
Do I need to do the following?
COPY /home/thesonics/robot-shop/web/static/index.hml /inusr/share/nginx/html

Can you please write me?

Thanks,
Ido

It is like @avbentem wrote: the files can not be outside the build context.

If your Dockerfile is located in /home/thesoncis/robot-shop/web, then modiy the COPY instruction to:

COPY static/index.hml /usr/share/nginx/html

The COPY and ADD instruction are not able use files outside your build context (which is usualy the folder where the Dockerfile is).

1 Like

Thank you!
It is more clear now, I will test it again.

I got an error on it as well

COPY static/index.hml /usr/share/nginx/html

Please share the complete web: block form your compose file.

Here it is:

 web:
    build:
      context: web
      args:
        # agent key to download tracing libs
        KEY: ${INSTANA_AGENT_KEY}
    image: ${REPO}/rs-web:${TAG}
    depends_on:
      - catalogue
      - user
      - shipping
      - payment
    ports:
      - "8080:8080"
    networks:
      - robot-shop
    healthcheck:
      test: [ "CMD", "curl", "-H", "X-INSTANA-SYNTHETIC: 1", "-f", "http://localhost:8080/" ]
      interval: 10s
      timeout: 10s
      retries: 3
    # Uncomment to enable Instana EUM
    # environment:
      # INSTANA_EUM_KEY: <your eum key>
      # INSTANA_EUM_REPORTING_URL: <your reporting url>
    logging:
      <<: *logging

networks:
  robot-shop:

so the context relative to the Dockerfile is web, then the Dockerfile in this folder should use the relative path as first arguement of COPY. This is what you did, this is what should have worked…

No idea then. I am afraid this is for somone else to answer.

Thanks for your help!

There is one more thing that commes to mind. If things don’t make sense, usualy the snap version of docker is installed. Please use following command to check if it’s the case:

me@node:~$ snap list docker
error: no matching snaps installed

If you get a different output than I did, please remove the snap package and install the docker engine (!=Docker Desktop) package from docker’s own repositories (see: Install Docker Engine | Docker Documentation)!

What error exactly? @meyay may be right with suspecting snap, but “an error” is never enough information to help you. We are just sometimes lucky to figure it out. “An error” can be related to the COPY instruction but it can also be related to something else after the COPY. It is always a good practice to share error messages again even if you are sure that the message is the same so we can all focus on something else.

An other suggestion. When you are not sure what is in your actual build context, you can always copy copy everything and see what it will copy to the image:

COPY . /debug

If it works, you can run a container from the image and check the files in /debug

It is however not completely harmless, since if you don’t know what the context is, it can be your root directory (not likely, based on the docker compose file)

I was having the same issue and fixed it by removing the matching pattern in my .dockerignore file.