Stack Template: NodeJS
If you are using Docker or Kubernetes you can check specific instructions for these at the bottom of this page.
Building and running NodeJS based apps is quite straightforward in Squash. There are basically three elements to accomplish this:
- Pick the base OS image from Docker Hub, this is usually a flavor of a major distribution (Ubuntu, CentOS, etc) or a basic image tailored for your specific programming language. You may also use an image from your own private registry.
- Install & build the necessary libraries/packages to run your app.
- Run your app and services used by your app (database, caching, etc).
This is all done in a Squash YAML file.
Example #1: basic app, no database
This is a basic .squash.yml
file for a Django application that doesn't require installing any additional services:
deployments: MyApp: dockerimage: ubuntu:16.04 build_steps: - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install curl git sudo - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs - cd ~/code - npm install launch_steps: - cd ~/code - npm start
- A Squash YAML needs to start with "deployments:"
- "MyApp": you can change this and use the name of your application. If you have multiple apps in one repo you can define multiple Apps using this same format.
- Important: each application runs on its own unique virtual machine (with flexible sizes and storage) in Squash. However, you can define as many apps as needed within the same Squash YAML file and even create a chain of dependencies between apps or services. See the 3rd example below for more details.
- Squash also supports apps that are made of several repos (multi-repository apps).
- dockerimage: this is where you define the base OS image.
- Squash automatically downloads your branch's code into
/home/test-instance/code
, thus we are using the base path~/code
to build the app. - build_steps: install any required packages here. In the example above we are using "apt" to download and install packages. This is because the base OS image is Ubuntu. You would use different package managers depending on the OS image of your choice.
- launch_steps: This is where you run any required services and your app. For this specific example the app is listening on port 80, which is the default port used by Squash for routing, so no additional routing information is needed.
Example #2: app + database + port_forwarding
Here we are installing the necessary packages to build the database service (MongoDB). Then the database service is launched inside of "launch_steps". Need to load test data into this database? please check: how to import data into a database.
And since the web app doesn't listen on port 80 we are using the port_forwarding field to tell Squash where to check for a success response.
deployments: MyApp: dockerimage: ubuntu:16.04 build_steps: - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install curl git sudo build-essential mongodb - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs - cd ~/code - npm install launch_steps: - service mongodb start - cd ~/code - npm start port_forwarding: 80:8000
Example #3: using a separate VM for the database
Certain projects can benefit from having a separate VM/host for the database service. This is easy to achieve by using the Non-HTTP based deployments and the deployment dependencies feature.
In the example below we first define the database service itself, note that it has its own separate app at the top, named "DatabaseService". Then we define the web app (InventoryApp), note the depends_on field. This will tell Squash to make sure the Database service is available before starting the web app.
This feature allows you to also share the same database VM with multiple feature branches as needed. You can either tell Squash to always spin up a new database VM for each feature branch or just use a shared model. For more information please check the deployment dependencies page.
deployments: DatabaseService: dockerimage: ubuntu:16.04 build_steps: - DEBIAN_FRONTEND=noninteractive apt-get update - DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common - DEBIAN_FRONTEND=noninteractive apt-get install -y locales gcc postgresql postgresql-contrib sudo supervisor launch_steps: - service postgresql start check_service_ports: 5432 port_forwarding: 5432:5432 InventoryApp: dockerimage: ubuntu:16.04 build_steps: - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install curl git sudo - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - - > DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -y install nodejs - cd ~/code - npm install launch_steps: - cd ~/code - npm start depends_on: - DATABASE_SERVICE: app: myrepo/master:DatabaseService
Miscellaneous
- Sample YAML file configurations
- How to reduce the build & startup time of an app
- Kubernetes
- Dockerfile and docker-compose
- Environment Variables
- Basic Authentication
- Loading test data
- One app and multiple repositories (Multi-repo applications)
- One Repository and Multiple Apps
- Deployment dependencies
- Pre-seeded databases
- Squash YAML file reference