Use a Dockerfile to build a layer when your dependency includes a dynamic-link library (.so file), or when your local environment is incompatible with the Function Compute runtime environment. For pure language packages without native dependencies (such as pure Python packages), install them directly in the Function Compute console or on your local machine instead.
This topic walks through building a Puppeteer layer for a Node.js runtime as an example.
Package directory requirements
When packaging dependencies into a layer ZIP file, place them in the directory that matches your runtime. For example, package the Python library into the /python directory of the layer ZIP package.
For dependencies that include dynamic-link libraries, place the .so files in the /lib directory of the ZIP file. After the layer is uploaded to Function Compute, these files are automatically decompressed to /opt/lib.
Built-in runtimes:
/opt/libis automatically added toLD_LIBRARY_PATH.Custom runtimes: Add
/opt/libtoLD_LIBRARY_PATHmanually.
Build a Puppeteer layer
Step 1: Prepare a Dockerfile
Create a Dockerfile with the following content:
# Use the build-latest image for the same runtime version as your function.
# If you are in the Chinese mainland, pull from registry.cn-beijing.aliyuncs.com instead.
FROM aliyunfc/runtime-nodejs14:build-latest
# Set environment variables and working directory.
ENV PATH /opt/bin:$PATH
ENV LD_LIBRARY_PATH /opt/lib
ENV NODE_PATH /opt/nodejs/node_modules
WORKDIR /tmp
# Install Puppeteer to /opt/nodejs.
COPY ./package.json /opt/nodejs/
RUN cd /opt/nodejs \
&& npm --registry https://registry.npmmirror.com i
# Download system dependency .deb files to /tmp/install/archives.
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
# Extract .so files from each .deb package.
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 extracted .so files to /opt/lib.
RUN cp -r /tmp/install/usr/bin /opt/; \
cp -r /tmp/install/usr/lib/x86_64-linux-gnu/* /opt/lib/
# Package /opt into layer.zip.
# The -y flag preserves symbolic links. .[^.]* includes hidden files without the parent directory.
RUN cd /opt \
&& zip -ry layer.zip * .[^.]*
CMD ["bash"]The base image runtime version must match the runtime version of your function. Use the build-latest tag for layer builds.Step 2: Build the layer ZIP file
Build the Docker image from the Dockerfile:
sudo docker build -t <layer-image-name> -f Dockerfile .Copy the layer ZIP file out of the image:
sudo docker run --rm -v $(pwd):/tmp <layer-image-name> sh -c "cp /opt/layer.zip /tmp/"This command mounts your current directory as
/tmpinside the container, then copieslayer.zipto it. After the command completes,layer.zipappears in your current directory. The--rmflag removes the container automatically.
Step 3: Create a custom layer
In the Function Compute console or using Serverless Devs, create a layer and set Layer Upload Method to Upload Layer in ZIP Package. For detailed steps, see Create a custom layer.
Base images
Function Compute provides preset base images for each supported runtime. Pull an image using the following format:
docker pull aliyunfc/<runtime-name>:<tag>
# Example:
docker pull aliyunfc/runtime-python3.10:latestAvailable base images:
Image name | Image repository path |
python3.10 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.10 |
python3.9 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.9 |
python3.6 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6 |
nodejs16 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs16 |
nodejs14 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs14 |
nodejs12 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-nodejs12 |
java11 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-java11 |
java8 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-java8 |
dotnetcore2.1 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-dotnetcore2.1 |
go1 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-go1 |
custom | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom |
custom.debian10 | registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom.debian10 |
For a full list of available images and tags, see fc-docker on GitHub.
What's next
After creating a layer, bind it to a function in the Function Compute console or using Serverless Devs. For details, see Configure a custom layer.