skip to Main Content

I am wanting to use Serverless Framework to deploy a lambda that is built using a docker container image, but the Dockerfile to build the image for the lambda is in a separate folder to the source code. When I run sls deploy I get an error saying the Dockerfile cannot find the src folder where the code is in order to copy it over. I understand that Dockerfiles access files/folders that are outside it’s cwd, and to do this you need to run docker compose and context set correctly, something like:

  instagram_image:
    image: instagram_image
    build:
      context: ../.
      dockerfile: ./build/instagram/Dockerfile
    volumes:
      - ./bin:/out

However I do not know how to do this incontext of serverless framework. Does anyone know how to? All examples are always with the Dockerfile in the same directory as the code itself.

I have a project structure like so

▾ build/
  ▾ instagram/
      Dockerfile
    docker-compose.yml
    Dockerfile
    lambda-env-tokens.yml
    serverless.yml
▾ src/my_selector/
   ... code in here

My Dockerfile:


# Install tar and xz
RUN yum install tar xz unzip -y

# Install ffmpeg
RUN curl https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz -o ffmpeg.tar.xz -s
RUN tar -xf ffmpeg.tar.xz
RUN mv ffmpeg-*-amd64-static/ffmpeg /usr/bin

RUN mkdir ./download_videos
RUN mkdir ./media

COPY src ${LAMBDA_TASK_ROOT}

# Copy function code
COPY pyproject.toml .

RUN python -m pip install --target "${LAMBDA_TASK_ROOT}" .

CMD [ "my_selector.handlers.instagram.handler" ]

serverless.yml

service: MySelector
provider:
  name: aws
  profile: personal-profile
  region: eu-west-1
  runtime: python3.9
  ecr:
    images:
      instagramdockerimage:
        path: ./instagram

functions:
  instagram:
    image:
      name: instagramdockerimage
    timeout: 900 # Max out timeout of 15 mins
    memorySize: 200
    events:
      - schedule: rate(2 hours)
    environment:
     ...

2

Answers


  1. According to the serverless docs, you can specify the Dockerfile to use with file.

    provider:
      name: aws
      ecr:
        images:
          baseimage:
            path: ../.
            file: build/instagram/Dockerfile
    

    Set path to root of your repo and file to the specified Dockerfile.

    Login or Signup to reply.
  2. Can you try: COPY src ${LAMBDA_TASK_ROOT} to COPY ../../src ${LAMBDA_TASK_ROOT}

    btw these steps:

    RUN curl https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz -o ffmpeg.tar.xz -s
    RUN tar -xf ffmpeg.tar.xz
    RUN mv ffmpeg-*-amd64-static/ffmpeg /usr/bin
    

    I think should locate in local instead of curl to download: for better performance after that use cp instead of mv

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search