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.
- IBM Cloud account and access to deploy apps in Code Engine
- Finished Tutorial: Deploying applications
- Basic understanding of how to write a Dockerfile
- You have reviewed Writing a Dockerfile for Code Engine (this page shows essential information)
1. Default port of Code Engine is 8080
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:
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:
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:
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_modulesto 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_moduleson 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.
Having a trouble deploying an app?
Please feel free to contact IBM client support for any questions or concerns.