This topic describes how to build a layer based on a Dockerfile. In the example used in this topic, Node.js is used to install Puppeteer dependencies.
Background information
Function Compute provides various methods to build layers. For dependencies that do not contain dynamic-link libraries, such as pure Python libraries, you can install dependencies in the Function Compute console or build a layer on an on-premises machine. If a dependency contains a dynamic-link library, or the local environment is incompatible with the Function Compute runtime environment, you cannot build the layer in the console or by using an on-premises machine. You can build the layer only by using a Dockerfile.
When you build a layer, we recommend that you package the dependency libraries of each language into the specified directory of the layer ZIP file by following Create a custom layer. For example, package the Python library into the /python directory of the layer ZIP package. If the dependency library contains a dynamic-link library, we recommend that you place the dynamic-link library in the /lib directory of the layer ZIP package. If you use a built-in runtime, the /opt/lib directory is automatically added to the LD_LIBRARY_PATH path. If you use a custom runtime, you must manually add the directory.
Build a Puppeteer layer
Step 1: Prepare a Dockerfile
Sample code:
FROM aliyunfc/runtime-nodejs14:build-latest
ENV PATH /opt/bin:$PATH
ENV LD_LIBRARY_PATH /opt/lib
ENV NODE_PATH /opt/nodejs/node_modules
WORKDIR /tmp
# Install the Puppeteer library to the /opt/nodejs directory.
COPY ./package.json /opt/nodejs/
RUN cd /opt/nodejs \
&& npm --registry https://registry.npm.taobao.org i
# Download the .deb file that needs to be installed to the system dependency library to the /tmp/install/archives directory.
RUN mkdir -p /opt/lib /tmp/install
RUN apt-get update && apt-get install -y -d -o=dir::cache=/tmp/install \
libblas3 fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 \
libgtk-3-0 libnspr4 libnss3 libpangocairo-1.0-0 libxcb-dri3-0 \
libx11-xcb1 libxcb1 libxss1 libxtst6 lsb-release \
xdg-utils libatspi2.0-0 libatk1.0-0 libxkbcommon0 libepoxy0 \
libglapi-mesa libnspr4 libgbm-dev \
--reinstall --no-install-recommends
RUN for f in $(ls /tmp/install/archives/*.deb); do \
echo "Preparing to unpack ${f##*/}"; \
cd /tmp/install/archives; \
dpkg-deb -x ${f##*/} /tmp/install; \
done;
# Copy the installed .so file to the /opt/lib directory.
RUN cp -r /tmp/install/usr/bin /opt/; \
cp -r /tmp/install/usr/lib/x86_64-linux-gnu/* /opt/lib/
# zip file
# -y store symbolic links as the link instead of the referenced file
# .[^.]* including hidden files and exclude the parent directory
RUN cd /opt \
&& zip -ry layer.zip * .[^.]*
CMD ["bash"]
In the preceding code:
Function Compute provides base images of various runtimes. You can find the corresponding image addresses from the Base images of Function Compute. We recommend that you use a
build-latest
image to build the image.NoteWhen you build a layer on an on-premises machine, the runtime version of the base image must be the same as the runtime version of the function.
FROM aliyunfc/runtime-nodejs14:build-latest
Declare environment variables and specify /tmp as the working directory.
ENV PATH /opt/bin:$PATH
ENV LD_LIBRARY_PATH /opt/lib
ENV NODE_PATH /opt/nodejs/node_modules
WORKDIR /tmp
Install the Puppeteer library in the /opt/nodejs directory.
# Install the Puppeteer library to the /opt/nodejs directory.
COPY ./package.json /opt/nodejs/
RUN cd /opt/nodejs \
&& npm --registry https://registry.npm.taobao.org i
Download the .deb file that needs to be installed in the system dependency library to the /tmp/install/archives directory.
# Download the. deb file that needs to be installed to the system dependency library to the /tmp/install/archives directory.
RUN mkdir -p /opt/lib /tmp/install
RUN apt-get update && apt-get install -y -d -o=dir::cache=/tmp/install \
libblas3 fonts-liberation libappindicator3-1 libasound2 libatk-bridge2.0-0 \
libgtk-3-0 libnspr4 libnss3 libpangocairo-1.0-0 libxcb-dri3-0 \
libx11-xcb1 libxcb1 libxss1 libxtst6 lsb-release \
xdg-utils libatspi2.0-0 libatk1.0-0 libxkbcommon0 libepoxy0 \
libglapi-mesa libnspr4 libgbm-dev \
--reinstall --no-install-recommends
Download the .deb file to the /tmp/install directory.
RUN for f in $(ls /tmp/install/archives/*.deb); do \
echo "Preparing to unpack ${f##*/}"; \
cd /tmp/install/archives; \
dpkg-deb -x ${f##*/} /tmp/install; \
done;
Copy the installed .so file to the /opt/lib directory.
# Copy the installed .so file to the /opt/lib directory.
RUN cp -r /tmp/install/usr/bin /opt/; \
cp -r /tmp/install/usr/lib/x86_64-linux-gnu/* /opt/lib/
Package the files in the /opt/lib directory into a ZIP package. Add the -y parameter to retain the symbolic link.
# zip file
# -y store symbolic links as the link instead of the referenced file
# .[^.]* including hidden files and exclude the parent directory
RUN cd /opt \
&& zip -ry layer.zip * .[^.]*
Step 2: Build a layer ZIP package
Run the following command to use the Dockerfile file to package the image:
sudo docker build -t ${layer-image-name} -f Dockerfile .
Run the following command to copy the layer ZIP package from the image:
sudo docker run --rm -v $(pwd):/tmp ${layer-image-name} sh -c "cp /opt/layer.zip /tmp/"
Step 3: Create a custom layer
Create and configure a layer in the Function Compute console or by using Serverless Devs. For more information, see Create a custom layer.
Puppeteer is built in Function Compute as an official common layer that you can directly use. For more information, see the "Example 1: Sample program for capturing web page screenshots based on Node.js 16 and Puppeteer" section in Examples of using official common layers.
Base images of Function Compute
For more information, see Base images of Function Compute.