Adokshaj Bhandarkar
Adokshaj Bhandarkar

Reputation: 1

How to run apt-get update && apt-get install ffmpeg libsm6 libxext6 -y command on a Docker container on AWS ECR?

My Python project throws this error:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

Solution is to add this to my Dockerfile:

RUN apt-get update && apt-get install ffmpeg libsm6 libxext6  -y

I want to deploy this container to AWS ECR + Lambda Function using this base image:

FROM public.ecr.aws/lambda/python:3.9.2024.11.22.15

According to my understanding, I have to use an official lambda/python base image for my container to work with Lambda Functions.

Problem is that the base image I want does not allow apt-get commands to run.

I want to have official lambda/python base image and run apt-get command in the same Docker image.

Tried yum but this does NOT work:

RUN yum update && yum install ffmpeg libsm6 libxext6  -y

Reason yum does not work: - Those packages not available when using yum

 > [2/5] RUN yum update && yum install ffmpeg libsm6 libxext6  -y:
0.623 Loaded plugins: ovl
16.27 No packages marked for update
16.48 Loaded plugins: ovl
16.50 No package ffmpeg available.
16.60 No package libsm6 available.
16.64 No package libxext6 available.
16.73 Error: Nothing to do

Current Dockerfile:

# FOR AWS ECR TESTING
### silent:localaws
# localaws tag

# OFFICIAL AWS LAMBA BASE IMAGE
FROM public.ecr.aws/lambda/python:3.9.2024.11.22.15

# WORKS BUT NOT ON AWS OFFICIAL BASE IMAGE
# RUN apt-get update && apt-get install ffmpeg libsm6 libxext6  -y

# DOES NOT WORK
RUN yum update && yum install ffmpeg libsm6 libxext6  -y

# Copy application code and dependencies
COPY requirements.txt ${LAMBDA_TASK_ROOT}
RUN pip install -r requirements.txt

# Copy the rest of the application
COPY . ${LAMBDA_TASK_ROOT}

# Set the Lambda handler
CMD ["main.lambda_handler"]

Thanks in advance.

Upvotes: 0

Views: 113

Answers (1)

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

You can use the official AWS Lambda Python base image while also installing system libraries like ffmpeg, libsm6, and libxext6, to do that you can create a custom Lambda image based on an Amazon Linux image, which allows you to use yum. The official Lambda base images are designed to run Lambda functions efficiently but do not allow package management through apt-get or yum.

Here's how you can achieve this with a multi-stage build:

  • Use Amazon Linux as a build stage to install the required libraries.
  • Copy the necessary files to the Lambda base image in the final stage.

Updated dockerfile,

    # Use Amazon Linux as the build stage
    FROM amazonlinux:2 AS build
    
    # Install required packages
    RUN yum update -y && \
        yum install -y ffmpeg libSM libXext && \
        yum clean all
    
    # Use the official AWS Lambda Python base image for the final image
    FROM public.ecr.aws/lambda/python:3.9.2024.11.22.15
    
    # Copy the installed libraries from the build stage
    COPY --from=build /usr/lib64/libSM.so.6 /usr/lib64/
    COPY --from=build /usr/lib64/libXext.so.6 /usr/lib64/
    COPY --from=build /usr/bin/ffmpeg /usr/bin/
    
    # Copy application code and dependencies
    COPY requirements.txt ${LAMBDA_TASK_ROOT}
    RUN pip install -r requirements.txt
    
    # Copy the rest of the application
    COPY . ${LAMBDA_TASK_ROOT}
    
    # Set the Lambda handler
    CMD ["main.lambda_handler"]

Read More: https://dev.to/shilleh/how-to-install-pip-packages-in-aws-lambda-using-docker-and-ecr-2ghh

Upvotes: 0

Related Questions