Function Compute allows you to build layers with multiple methods. For dependencies that do not contain dynamic-link libraries, such as pure Python libraries, you can directly install dependencies in the Function Compute console or build layers on your on-premises machine. If a dependency contains a dynamic-link library or your on-premises environment is incompatible with the Function Compute runtime environment, you cannot build a layer in the Function Compute console or on an on-premises machine. You can build a layer only based on a Dockerfile. This topic describes how to build a layer based on a Dockerfile. In the example used in this topic, Puppeteer dependencies are installed in a Node.js runtime.
Precautions
When you build a layer, we recommend that you package dependency libraries of each language into the specified directory of the layer ZIP file. For more information, see Create a custom layer. For example, package the Python library into the /python
directory of the layer ZIP package. If your dependent libraries contain dynamic-link libraries, we recommend that you place the dynamic-link libraries in the /lib
directory of the ZIP file. After the libraries are uploaded to a runtime of Function Compute, they are automatically decompressed to the /opt/lib
directory. 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 need to manually add the directory.
Build a Puppeteer layer
Step 1: Prepare a Dockerfile
The following sample code shows an example:
# Specify the base image. We recommend that you use the build-latest image.
# When 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.
# For Chinese mainland users, we recommend that you use a base image in the registry.cn-beijing.aliyuncs.com repository.
FROM aliyunfc/runtime-nodejs14:build-latest
# Declare environment variables and specify the working directory as /tmp.
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.npmmirror.com i
# Download the .deb file that needs to be installed in 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/
# Package files in the /opt/lib directory into a ZIP file. Note that the -y parameter must be added to retain the symbolic link.
# .[^.]* indicates to include hidden files and exclude the parent directory.
RUN cd /opt \
&& zip -ry layer.zip * .[^.]*
CMD ["bash"]
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 file 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
After the layer ZIP package is built, you can create a layer in the Function Compute console or by using Serverless Devs. Set Layer Upload Method to Upload Layer in ZIP Package. For more information, see Create a custom layer.
Base images of Function Compute
The following items list the preset base images of different programming languages in Function Compute. You can directly pull a base image from the corresponding image repository path. For example, you can use the following pull script: docker pull aliyunfc/runtime-python3.10:latest
.
For more information, see fc-docker on GitHub.
What to do next
After a layer is created, you can bind it to a function in the Function Compute console or by using Serverless Devs so that the function can access resources provided in the layer. For more information, see Configure a custom layer.