CircleCI Docker ECR orb ERROR: failed to solve: failed to compute cache key: "/init.sh" not found

I am utilizing CircleCI and ECR orb to build my docker image and push it to Elastic Container Registry ( ECR ) but i keep running into the following error: ERROR: failed to solve: failed to compute cache key: "/init.sh" not found.

My directory structure is as follows :

Terraform
  .circleci
  infra
  frontend
    --- Dockerfile
    --- init.sh

So basically i have a Terraform directory. Inside that directory i have an infra directory and a frontend directory. The frontend directory consists of my Dockerfile and my init.sh script.

The contents of my dockerfile is as follows :

FROM node:14
ENV JQ_VERSION=1.6
RUN wget --no-check-certificate https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64
RUN cp /tmp/jq-linux64 /usr/bin/jq
RUN chmod +x /usr/bin/jq
WORKDIR /app
COPY . .
RUN jq 'to_entries | map_values({ (.key) : ("$" + .key) }) | reduce .[] as $item ({}; . + $item)' ./src/config.json > ./src/config.tmp.json && mv ./src/config.tmp.json ./src/config.json
RUN npm install && npm run build

FROM nginx:1.17
ENV JSFOLDER=/usr/share/nginx/html/js/*.js
COPY ./init.sh /usr/bin/init.sh
RUN chmod +x /usr/bin/init.sh
WORKDIR /usr/share/nginx/html
COPY --from=0 /app/dist .
ENTRYPOINT [ "init.sh" ]

and my circleci config has the following content :

version: '2.1'
orbs:
  aws-ecr: circleci/aws-ecr@8.2.1
  aws-cli: circleci/aws-cli@3.1
jobs:
  build:
    machine:
      image: ubuntu-2004:2022.10.1
    steps:
      - checkout
      - aws-ecr/build-and-push-image:
          repo: frontend
          push-image: true
          region: ${AWS_REGION}
          registry-id: REGISTRY_ID
          dockerfile: frontend/Dockerfile
          path: .
          tag: ${CIRCLE_SHA1}
workflows:
  test:
    jobs:
      - build

But CircleCI gives me the following error :

> [stage-1 2/5] COPY ./init.sh /usr/bin/init.sh:
------
ERROR: failed to solve: failed to compute cache key: "/init.sh" not found: not found

Exited with code exit status 1 

Any help on this will be appreciated. Thank you.

The error being thrown is from within docker rather than circleci so it has something to do with docker’s ability to access ./init.sh.

Are you sure that you are in the correct directory to allow the copy to operate? When you call aws-ecr you are passing Dockerfile as being in a sub-directory within the working directory, while your copy command is expecting init.sh to be in the working directory. Your directory tree indicates that both the Dockerfile and init.sh are in the same directory.

You’re right. I updated the Dockerfile with the following content:

FROM node:14

ENV JQ_VERSION=1.6

RUN wget --no-check-certificate https://github.com/stedolan/jq/releases/download/jq-${JQ_VERSION}/jq-linux64 -O /tmp/jq-linux64

RUN cp /tmp/jq-linux64 /usr/bin/jq

RUN chmod +x /usr/bin/jq

WORKDIR /app/frontend

COPY . /app

RUN jq 'to_entries | map_values({ (.key) : ("$" + .key) }) | reduce .[] as $item ({}; . + $item)' ./src/config.json > ./src/config.tmp.json && mv ./src/config.tmp.json ./src/config.json

RUN npm install && npm run build

FROM nginx:1.17

ENV JSFOLDER=/usr/share/nginx/html/js/*.js

COPY ./frontend/init.sh /usr/bin/init.sh

RUN chmod +x /usr/bin/init.sh

WORKDIR /usr/share/nginx/html

COPY . .

ENTRYPOINT [ "/usr/bin/init.sh" ]

and it worked.