Three important tips to follow if you’re going to develop an app based on Node.js with IBM Cloud Code Engine.
IBM Cloud offers a new feature called IBM Cloud Code Engine — a fully managed, serverless platform that runs your containerized workloads, including web apps, microservices, event-driven functions or batch jobs. The Code Engine experience is designed so that you can focus on writing code and not on the infrastructure that is needed to host it.
In this blog, I will share what I learned through the development of an app based on Node.js. Some tips only apply to Node.js apps, and I assume you have reviewed Writing a Dockerfile for Code Engine already.
Note: Code Engine is a beta service as of March 2021. This service might not yet be stable and is not for production use.
Prerequisites
IBM Cloud account and access to deploy apps in Code Engine
If you want to use non-default port other than 8080, specify the following line in the Dockerfile and run an `ibmcloud ce` command to change the port:
In Dockerfile:
EXPOSE 8081
Scroll to view full table
ibmcloud ce command:
$ ibmcloud ce application update --name APP_NAME -p 8081
Scroll to view full table
2. Make sure you have npm install in Dockerfile
This is a Node.js-specific practice. It is always safe to specify this in Dockerfile:
RUN npm install
Scroll to view full table
During development, npm install is often performed and the required dependencies are imported in the node_modules, which are stored in the Git repository root directly. In the Dockerfile, the COPY . . will then use those dependencies for the build steps in the Dockerfile. In Code Engine, the build starts with a fresh Git clone, and, therefore, the node_modules is missing. As a result, you might see an error like the one below upon deploying the app:
Mar 8 16:21:10 Code Engine APP-XXX-deployment-YYY 2021-03-08T05:21:07.096108821Z stderr F Error: Cannot find module 'ZZZZ'
Scroll to view full table
3. Do not carry node_modules
This is also a Node.js-specific practice and the goal is the same — to keep everything clean and tidy:
In the Git repository where your code exists, create .dockerignore and add node_modules to avoid the accidental pick-up of local files. Whatever is in this file won’t be included upon build.
In the Git repository, create .gitignore to avoid node_modules on git gets updated upon push.
The key takeaway here is that you should always have Tips 2 and 3 in your code so that you can prevent your Docker image from getting created based on the node_modules on the host side. Make sure npm install is executed inside the Docker image.