CircleCI-Public / aws-ecr-orb

CircleCI orb for interacting with Amazon's Elastic Container Registry (ECR)
https://circleci.com/orbs/registry/orb/circleci/aws-ecr
MIT License
81 stars 142 forks source link

Split path to Dockerfile and path to build context #220

Closed davorceman closed 1 year ago

davorceman commented 2 years ago

This is more question, if someone can direct me how to solve this issue without changing project structure

I have to set circleci for one project with next structure... one repo for 3 different applications. Dockerfile for each app is in subfolder for that app. But in the root of the repo is poetry.lock and pyproject.toml They are already building from local without any issues, just with command as docker build -t app_1-f aws_lambda/app_1/Dockerfile .

$ tree
.
├── aws_lambda
│   ├── app_1
│   │   ├── Dockerfile
│   │   ├── __init__.py
│   │   ├── src
│   │   │   ├── dynamodb.py
│   │   │   ├── event_parser.py
│   │   │   ├── feature_extractor.py
│   │   │   ├── __init__.py
│   │   │   ├── models.py
│   │   │   ├── app_1.py
│   │   │   ├── utils.py
│   │   │   └── app.py
│   ├── app_2
│   │   ├── Dockerfile
│   │   ├── __init__.py
│   │   ├── src
│   │   │   ├── dynamodb.py
│   │   │   ├── event_parser.py
│   │   │   ├── get_from_rds.py
│   │   │   ├── __init__.py
│   │   │   ├── models.py
│   │   │   ├── opensearch.py
│   │   │   ├── app_2.py
│   │   │   ├── sqs_queue.py
│   │   │   └── utils.py
│   └── app_3
│       ├── Dockerfile
│       ├── __init__.py
│       ├── src
│       │   ├── dynamodb.py
│       │   ├── event_parser.py
│       │   ├── feature_extractor.py
│       │   ├── get_from_rds.py
│       │   ├── __init__.py
│       │   ├── models.py
│       │   ├── app_3.py
│       │   ├── utils.py
│       │   └── app.py
│   ├── README.md
├── cloudformation.yaml
├── poetry.lock
├── pyproject.toml
├── README.md
├── serverless-local.yml
└── serverless.yml

My dockerfile, let's say, for app_1 is:

FROM public.ecr.aws/lambda/python:3.8 as app_1

COPY pyproject.toml ${LAMBDA_TASK_ROOT}
COPY poetry.lock ${LAMBDA_TASK_ROOT}
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
ENV PATH="/root/.poetry/bin:$PATH"

ENV EVENT_PARSER_TYPE=kinesis
ENV POETRY_VIRTUALENVS_CREATE=false
RUN poetry install --no-dev --no-root --no-interaction --no-ansi

ENV NLTK_DATA=${LAMBDA_TASK_ROOT}
RUN python3 -c "import nltk; nltk.download('stopwords');"

COPY aws_lambda/app_1/ ${LAMBDA_TASK_ROOT}/app_1

CMD [ "app_1.src.app_1.handler" ]

My config.yml

version: 2.1

orbs:
   aws-ecr: circleci/aws-ecr@8.1.2

workflows:
  build-push-and-deploy:
    jobs:
      - aws-ecr/build-and-push-image:
          repo: app_1
          tag: ${CIRCLE_SHA1}
          path: aws_lambda/app_1/
          filters:
            branches:
              only:
                - feature/ci-cd

I'm constantly getting errors like this

#12 [7/7] COPY aws_lambda/app_1/ /var/task/app_1
#12 sha256:23f685c659863aa3229843e5ca138ebf7287d810fcfe6c49123487
#12 ERROR: failed to calculate checksum of ref ow96yyppfecy3wsytyeb09::23fv39b1chf6u9c6wq6sb: "/aws_lambda/app_1": not found

#8 [3/7] COPY poetry.lock /var/task
#8 sha256:5f478f09522e332c2bc67f6d3d9a0102d3384e696fcf0532d9c3f00
#8 ERROR: failed to calculate checksum of ref ow96yyppfdsytyeb09::23fv39b1cd6u9c6wq6sb: "/poetry.lock": not found

#7 [2/7] COPY pyproject.toml /var/task
#7 sha256:4b70560129556294445220b38917f064229b42e3e9cabda471268
#7 ERROR: failed to calculate checksum of ref ow96yypp3wufdsytyeb09::23fv39b1chfa9c6wq6sb: "/pyproject.toml": not found

note: I needed to rename some directory names, so maybe there is some error in the code

Quidge commented 2 years ago

I am having this same issue. Pretty sure I found the problem and agree that it would be an enhancement because the orb doesn't support this out of the box.

To solve your issue with the current orb version/capabilities, I suggest doing abandoning using path and falling back to the docker args themselves:

In your workflow:

workflows:
  build-push-and-deploy:
    jobs:
      - aws-ecr/build-and-push-image:
          repo: app_1
          tag: ${CIRCLE_SHA1}
          extra-build-args: '-f aws_lambda/app_1/Dockerfile'

It's happening because the path is used as the same value for both the location of the dockerfile and the location of the directory used to build from. See lines 76 and 81.

If there were another option, say dockerfile_path that just pointed at the dockerfile path only, that would work I think.

EDIT:

I pushed a PR for that dockerfile_path option.