Before a function can be deployed and invoked, we need to prepare a binary program and pack it as a function container.
Here are the steps to package your program into a function container:
- Create a Dockerfile containing the FROM instruction to derive it from a base image. You can even use the Alpine base image.
- Add the function watchdog binary to the image using the ADD instruction. The function watchdog's name is fwatchdog and can be found on the OpenFaaS release page.
- Add the function program to the image. We usually use the COPY instruction to do so.
- Define the environment variable named fprocess with the ENV instruction to point to our function program.
- Expose port 8080 for this container image using the EXPOSE instruction with, of course, port number 8080.
- Define an entry point of this container image. We use ENTRYPOINT to point to fwatchdog.
We will do something a bit unusual, but in the proper way, to prepare a function container. We use a Docker feature called multi-stage builds to both compile the program and pack the function container using a single Dockerfile.
What is multi-stage build? The multi-stage build feature allows a single Dockerfile to have several build stages chaining along the build process.
With this technique, we can build a very tiny Docker image by discarding large image layers from the previous build stages. This feature requires Docker 17.05 or greater.