CodePipeline Issue During Deploy Stage - Dockerfile Doesn't Exist or Isn't Within the Dir

0

Hi there,

CodeBuild specs:

  • Platform: Ubuntu
  • Runtime: Standard
  • Image: aws/codebuild/Standard:7.0

During the final Deploy Stage, I'm receiving the following error:

2024/05/01 14:03:50.450964 [ERROR] An error occurred during execution of command [app-deploy] - [Docker Specific Build Application]. Stop running the command. Error: failed to read Dockerfile: open /var/app/staging/server/backend.Dockerfile: no such file or directory

This is my docker-compose.yml

version: '3.8'

services:
  server:
    image: ************.dkr.ecr.us-east-2.amazonaws.com/howdy-api-production:${IMAGE_TAG:-latest}
    container_name: servername
    platform: linux/amd64
    restart: always
    environment:
      - RDS_USER=${RDS_USER}
      - RDS_PASSWORD=${RDS_PASSWORD}
      - RDS_SERVER=${RDS_SERVER}
      - RDS_PORT=${RDS_PORT}
      - RDS_DB=${RDS_DB}
      - REDIS_PORT=${REDIS_PORT}
      - SECRET_KEY=${SECRET_KEY}
      - SMTP_SENDER_ADDRESS=${SMTP_SENDER_ADDRESS}
      - SENDGRID_API_KEY=${SENDGRID_API_KEY}    
      - AWS_KEY_ID=${AWS_KEY_ID}
      - AWS_SECRET_KEY=${AWS_SECRET_KEY}
    build: 
        context: ./server
        dockerfile: backend.Dockerfile
    volumes:
      - ./server/app/:/app/
      - public_assets:/app/shared/public
    # env_file:
    #   - ./server/.env
    ports:
      - 8000:8000
    networks:
      - howdynetwork

And here's the backend.Dockerfile

FROM python:3.11.1-slim

WORKDIR /app

ENV PYTHONDONTWRITEBYTECODE 1

ENV PYTHONUNBUFFERED 1

COPY ./requirements.txt /app/requirements.txt

RUN pip3 install --no-cache-dir -r /app/requirements.txt

COPY ./app /app/

CMD ["uvicorn","main:app","--host=0.0.0.0","--port=8000","--reload", "--log-level=debug"]

Finally, here's my buildspec.yml

version: 0.2

phases:
    pre_build:
        commands:
            - echo "Logging in to Amazon ECR..."
            - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
            - COMMIT_SHA=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
            - IMAGE_TAG=${COMMIT_SHA:-latest}
    build:
       commands:
            - echo "Build started on `date`"
            - cd ./server
            - docker build -f backend.Dockerfile -t $IMAGE_REPOSITORY:$IMAGE_TAG .
            - docker tag $IMAGE_REPOSITORY:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPOSITORY:$IMAGE_TAG
            - cd ..
            - echo "Build completed on `date`"
    post_build:
        commands:
            - echo "Push the docker image to AWS ECR..."
            - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPOSITORY:$IMAGE_TAG
            - echo "Update IMAGE_TAG in docker-compose.yml..."
            - sed -i -e "s/\${IMAGE_TAG:-latest}/$IMAGE_TAG/g" docker-compose.yml
artifacts:
    files:
        - .ebextensions/**/*
        - docker-compose.yml
        - nginx.conf
# cache:
#     paths:
#         - node_modules

Here's my project structure:

Enter image description here

If I use AWSCLI such as eb deploy ... the deploy is without any problems.

Finally, as a note, I'm following this tutorial https://bentranz.medium.com/setup-continuous-deployment-pipeline-for-aws-elastic-beanstalk-5f8edb38d872

  • Can you tell me the structure of your repository? I suspect a problem with the folder structure and file locations.

  • I just updated the question with a screenshot that shows the folder structure

  • Can you try adding -w with server directory like this

    • docker build -w ./server -f ./server/backend.Dockerfile -t $IMAGE_REPOSITORY:$IMAGE_TAG ./server
  • I updated the buildspec.yml file to reflect the attempt. Unfortunately, the same error persists.

    Instead of [docker build -w ...] which I gathered is an unsupported flag, I switched directories during the Build Stage and tried to replicate the effort.

asked 21 days ago172 views
3 Answers
1

I believe the problem is that when deploying to EB, docker-compose.yml is executed and docker build is executed again.

The server directory is not included in the file sent to EB by artifacts, so if we try to build again on the EB instance side, it will fail because there is no file.

The solution is to change the image referenced in docker-compose.yml to the ECR image, as described in Part 1 of the article you referred to.

https://bentranz.medium.com/deploy-dockerized-application-to-aws-elastic-beanstalk-f8a3cf2944a7

profile picture
EXPERT
shibata
answered 21 days ago
  • I updated the question to reflect new docker-compose.yml file. However, the issue still prevails, even when the image fetch comes from ECR (both manually and programmatically set)

1

Hi

Try in this way, The issue is Codebuild unable to find the DockerFile

docker build -f $CODEBUILD_SRC_DIR/server/backend.Dockerfile -t $IMAGE_REPOSITORY:$IMAGE_TAG .

profile picture
EXPERT
GK
answered 21 days ago
0

So the issue was solved. The solution was simple.

The issue as pointed by shibata is that it didn't have access not only to backend.Dockerfile but to the entire directory /server/. I fixed it by including within buildspec.yml

artifacts:
    files:
        - docker-compose.yml
        - nginx.conf
        - "**/*"

This resolved the issue and Deploy stage was success!

answered 21 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions