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
According to the serverless docs, you can specify the Dockerfile to use with
file
.Set
path
to root of your repo andfile
to the specified Dockerfile.Can you try:
COPY src ${LAMBDA_TASK_ROOT}
toCOPY ../../src ${LAMBDA_TASK_ROOT}
btw these steps:
I think should locate in local instead of
curl
to download: for better performance after that usecp
instead of mv