I am trying to build a container image for a Node.js Lambda function. My base image is like this:
FROM public.ecr.aws/lambda/nodejs:20
COPY index.js ${LAMBDA_TASK_ROOT}
CMD [ "index.handler" ]
However, my Node.js function also uses pdf2htmlEX
package. One way to install it is with apt-get
. Running apt-get
in the above dockerfile will return an error "command not found". Understandable, because apt-get
is not available in the Node.js image from AWS.
Maybe that’s not be the way to do it. Ultimately, how do I get a Node.js Lambda function to execute a Linux package (pdf2htmlEX
in this case)?
Update 1: After testing out various options and combinations, I created an alternative base image in order to install pdf2htmlEX, node.js and npm :
ARG FUNCTION_DIR="/function"
FROM ubuntu:18.04
ARG FUNCTION_DIR
ENV NODE_VERSION=16.13.0
RUN apt-get update
COPY ./pdf2htmlEX.deb /tmp
# Install pdf2htmlEX and node.js and related packages
RUN apt-get install -y /tmp/pdf2htmlEX.deb curl cmake autoconf libtool
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
ENV NVM_DIR=/root/.nvm
RUN . "$NVM_DIR/nvm.sh" && nvm install ${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm use v${NODE_VERSION}
RUN . "$NVM_DIR/nvm.sh" && nvm alias default v${NODE_VERSION}
ENV PATH="/root/.nvm/versions/node/v${NODE_VERSION}/bin/:${PATH}"
RUN mkdir -p ${FUNCTION_DIR}
COPY index.js package.json ${FUNCTION_DIR}
WORKDIR ${FUNCTION_DIR}
RUN npm install aws-lambda-ric
CMD [ "index.handler" ]
The build failed when installing aws-lambda-ric
, the runtime interface client required when using an alternative base image. The error log is too long to post here, so maybe it’s not the right config.
Update 2: Another attempt using node-20:buster
:
ARG FUNCTION_DIR="/function"
FROM node:20-buster as build-image
# Include global arg in this stage of the build
ARG FUNCTION_DIR
COPY ./pdf2htmlEX.deb /tmp
# Install build dependencies
RUN apt-get update &&
apt-get install -y
/tmp/pdf2htmlEX.deb
Got another type of error:
The following packages have unmet dependencies:
pdf2htmlex : Depends: libjpeg-turbo8 but it is not installable
Unable to correct problems, you have held broken packages.
2
Answers
Docker image(
public.ecr.aws/lambda/nodejs:20
) that you used is build for Amazon Linux 2023 (AL2). For installing any package you can youyum
Howeverdnf
which is default software package management tool in AL2023 is DNF. DNF is the successor to YUM, the package management tool in AL2.DNF is similar to YUM in its usage. Many DNF commands and command options are the same as YUM commands. In a Command Line Interface (CLI) command, in most cases dnf replaces yum.
For example, for the following AL2 yum commands:
I understand that you want to install the Linux package
pdf2htmlEx
and the Node packageaws-lambda-ric
using a Dockerfile to create your customized image.Reviewing the documentation for
pdf2htmlEx
(https://github.com/pdf2htmlEX/pdf2htmlEX), the package is available for Ubuntu and Alpine. Therefore, the base image must be Ubuntu; I recommend version 20.04. Then, you install the dependencies and finally thepdf2htmlEx
package.In the Dockerfile, Node should be added (1) and the "aws-lambda-ric" package should be installed (2). For point 1, you can review the official Node Dockerfile; I used version
node:20.14.0-bullseye
(https://github.com/nodejs/docker-node/blob/daea62837e99456d7556b585edbc2b32fb57369e/20/bullseye/Dockerfile). For point 2, the package can be installed as indicated in the AWS guide (https://docs.aws.amazon.com/lambda/latest/dg/nodejs-image.html#nodejs-image-clients).Overall, the Dockerfile would look like this:
screen command pdf2htmlEX -v