Sunday, July 22, 2018

Docker and Firebase Cloud Functions

Focus: Setting up a Docker Image/Container to host Firebase Cloud Functions

Synopsis: This allows a developer to run cloud functions in an isolated docker container, using NodeJS 6.14 without conflicting with other development needs. Using the latest NodeJS for other development forces the developer to constently switch node versions. While NVM makes things easier, it can be frustrating and confusing if the developer forgets to switch node verisons. The final working DockerFile is below.

Details: Before beginning, this post assess that a firebase cloud functions project has already begin initialized and working. A firebase token is required to run against firebase. To get the token run firebase login:ci. This should open a window to authorize account and permissions. Upon successfully authenticating and granting permissions, copy the token from the console and us it to replace all references to <token> below.

*** A key thing to know here is Docker does not expose localhost to the host. So to make this all work the --host 0.0.0.0 in the firebase serve command is critical. Without this option, firebase will serve the functions on localhost by default and the cloud functions will not be accessible.

A dockerfile for all this is hosted on DockerHub -- daemogar/firebase-cloud-functions. Alternatively, create your own DockerFile as below. Place the DockerFile in the functions folder and run the following commands.

The build command:
docker build -t daemogar/firebase-cloud-functions:latest .
The run command:
docker run -d --rm \
     -p 5000:5000 \
     -v <projectpath>:/usr/src/data \
     -e FIREBASE_TOKEN="<token>" \
     daemogar/firebase-cloud-functions:latest

Note: project path should be the location of the parent folder of the functions directory, not the functions directory itself.

DockerFile
FROM node:6.14

VOLUME ["/usr/src/data"]
EXPOSE 5000

WORKDIR /usr/src/data/functions
RUN npm install -g firebase-tools

CMD [ "/bin/bash", "-c", "firebase serve --host 0.0.0.0 --only functions" ]

Docker and Firebase Cloud Functions

Focus: Setting up a Docker Image/Container to host Firebase Cloud Functions Synopsis: This allows a developer to run cloud functions in...