skip to Main Content

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


  1. Docker image(public.ecr.aws/lambda/nodejs:20) that you used is build for Amazon Linux 2023 (AL2). For installing any package you can you yum However dnf 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:

    $ sudo dnf install packagename
    
    Login or Signup to reply.
  2. I understand that you want to install the Linux package pdf2htmlEx and the Node package aws-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 the pdf2htmlEx 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).

    Note:
    Add the Entrypoint file used by the Dockerfile from node:20.14.0-bullseye

    Overall, the Dockerfile would look like this:

    FROM ubuntu:20.04
    
    ENV PDF2HTMLEX_VERSION="pdf2htmlEX-0.18.8.rc2-master-20200820-ubuntu-20.04-x86_64.deb"
    
    ## Install pdf2htmlEX
    RUN apt-get update;  
        DEBIAN_FRONTEND=noninteractive TZ="America/Lima" apt-get -y install tzdata libjpeg-turbo8 wget gpg curl xz-utils; 
        wget -O pdf2htmlEX.deb https://github.com/pdf2htmlEX/pdf2htmlEX/releases/download/continuous/${PDF2HTMLEX_VERSION}; 
        apt-get install -y ./pdf2htmlEX.deb; 
        rm -rf /var/lib/apt/lists/*; 
        pdf2htmlEX -v
    
    ## Install node 22.2.0
    ## Source Dockerfile: node:20.14.0-bullseye
    
    RUN groupadd --gid 1000 node; 
        useradd --uid 1000 --gid node --shell /bin/bash --create-home node
    
    ENV NODE_VERSION 20.14.0
    
    RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" 
      && case "${dpkgArch##*-}" in 
        amd64) ARCH='x64';; 
        ppc64el) ARCH='ppc64le';; 
        s390x) ARCH='s390x';; 
        arm64) ARCH='arm64';; 
        armhf) ARCH='armv7l';; 
        i386) ARCH='x86';; 
        *) echo "unsupported architecture"; exit 1 ;; 
      esac 
      # use pre-existing gpg directory, see https://github.com/nodejs/docker-node/pull/1895#issuecomment-1550389150
      && export GNUPGHOME="$(mktemp -d)" 
      # gpg keys listed at https://github.com/nodejs/node#release-keys
      && set -ex 
      && for key in 
        4ED778F539E3634C779C87C6D7062848A1AB005C 
        141F07595B7B3FFE74309A937405533BE57C7D57 
        74F12602B6F1C4E913FAA37AD3A89613643B6201 
        DD792F5973C6DE52C432CBDAC77ABFA00DDBF2B7 
        61FC681DFB92A079F1685E77973F295594EC4689 
        8FCCA13FEF1D0C2E91008E09770F7A9A5AE15600 
        C4F0DFFF4E8C1A8236409D08E73BC641CC11F4C8 
        890C08DB8579162FEE0DF9DB8BEAB4DFCF555EF4 
        C82FA3AE1CBEDC6BE46B9360C43CEC45C17AB93C 
        108F52B48DB57BB0CC439B2997B01419BD92F80A 
        A363A499291CBBC940DD62E41F10027AF002F8B0 
        CC68F5A3106FF448322E48ED27F5E38D5B0A215F 
      ; do 
          gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$key" || 
          gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" ; 
      done 
      && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-$ARCH.tar.xz" 
      && curl -fsSLO --compressed "https://nodejs.org/dist/v$NODE_VERSION/SHASUMS256.txt.asc" 
      && gpg --batch --decrypt --output SHASUMS256.txt SHASUMS256.txt.asc 
      && gpgconf --kill all 
      && rm -rf "$GNUPGHOME" 
      && grep " node-v$NODE_VERSION-linux-$ARCH.tar.xz$" SHASUMS256.txt | sha256sum -c - 
      && tar -xJf "node-v$NODE_VERSION-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner 
      && rm "node-v$NODE_VERSION-linux-$ARCH.tar.xz" SHASUMS256.txt.asc SHASUMS256.txt 
      && ln -s /usr/local/bin/node /usr/local/bin/nodejs 
      # smoke tests
      && node --version 
      && npm --version
    
    ENV YARN_VERSION 1.22.22
    
    RUN set -ex 
      # use pre-existing gpg directory, see https://github.com/nodejs/docker-node/pull/1895#issuecomment-1550389150
      && export GNUPGHOME="$(mktemp -d)" 
      && for key in 
        6A010C5166006599AA17F08146C2130DFD2497F5 
      ; do 
        gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys "$key" || 
        gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" ; 
      done 
      && curl -fsSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz" 
      && curl -fsSLO --compressed "https://yarnpkg.com/downloads/$YARN_VERSION/yarn-v$YARN_VERSION.tar.gz.asc" 
      && gpg --batch --verify yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz 
      && gpgconf --kill all 
      && rm -rf "$GNUPGHOME" 
      && mkdir -p /opt 
      && tar -xzf yarn-v$YARN_VERSION.tar.gz -C /opt/ 
      && ln -s /opt/yarn-v$YARN_VERSION/bin/yarn /usr/local/bin/yarn 
      && ln -s /opt/yarn-v$YARN_VERSION/bin/yarnpkg /usr/local/bin/yarnpkg 
      && rm yarn-v$YARN_VERSION.tar.gz.asc yarn-v$YARN_VERSION.tar.gz 
      # smoke test
      && yarn --version 
      && rm -rf /tmp/*
    
    COPY docker-entrypoint.sh /usr/local/bin/
    ENTRYPOINT ["docker-entrypoint.sh"]
    
    CMD [ "node" ]
    
    ## Install aws-lambda-ric
    RUN apt-get update; 
        apt-get install -y 
            g++ 
            make 
            cmake 
            unzip 
            libcurl4-openssl-dev 
            autoconf 
            automake 
            build-essential 
            libtool 
            m4 
            python3 
            unzip 
            libssl-dev;        
        rm -rf /var/lib/apt/lists/*; 
        cd /root/;  
        npm install aws-lambda-ric
    
    CMD ["index.handler"]
    

    screen command pdf2htmlEX -v

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