Node.js is a cross-platform, open-source server environment that can run on Windows, Linux, Unix, macOS, and more. Node.js is a back-end JavaScript runtime environment, runs on the V8 JavaScript engine, and executes JavaScript code outside a web browser.
Acorn offers a user-friendly cloud computing platform with a generous free sandbox accessible through GitHub registration. Designed to simplify the deployment of modern cloud-native apps, Acorn facilitates familiar development and deployment workflows using mainstream container tools. By eliminating the need to handle the provisioning or configuration of underlying cloud resources, Acorn provides the robust capabilities of Kubernetes and Terraform without the associated complexity. In this tutorial, we'll guide you through the process of defining and deploying a sample Node.js Application on the Acorn platform using an Acornfile, ultimately creating an easily deployable Acorn Image.
If you want to skip to the end, just click Run in Acorn to launch the app immediately in a free sandbox environment. All you need to join is a GitHub ID to create an account.
If you want to follow along, I’ll walk through the steps to deploy Battleship game build using nodejs on Acorn.
Note: Everything shown in this tutorial can be found in this repository.
- Acorn CLI: The CLI allows you to interact with the Acorn Runtime as well as Acorn to deploy and manage your applications. Refer to the Installation documentation to install Acorn CLI for your environment.
- A GitHub account is required to sign up and use the Acorn Platform.
Log in to the Acorn Platform using the Github Sign-In option with your Github user.
After the installation of Acorn CLI for your OS, you can login to the Acorn platform.
$ acorn login beta.acorn.io
In this tutorial we will create a simple Nodejs Battleship game app following this repo. It is a simple Battleship game that provides two modes you can play the game
- Single Player
- Multiplayer
In the Acorn platform, there are two ways you can try this sample application.
- Using Acorn platform dashboard.
- Using CLI
The first way is the easiest one where, in just a few clicks you can deploy the Nodejs Battleship game application on the platform and start using it. However, if you want to customize the application or want to understand how you can run your own Battleship applications using Acorn, use the second option.
In this option you use the published Acorn application image to deploy the Battleship application in just a few clicks. It allows you to deploy your applications faster without any additional configurations. Let us see below how you can deploy the vote app to the Acorn platform dashboard.
-
Login to the Acorn Platform using the Github Sign-In option with your Github user.
-
Select the “Create Acorn” option.
-
Choose the source for deploying your Acorns 3.1. Select “From Acorn Image” to deploy the sample Application.
3.2. Provide a name "battleship”, use the default Region and provide the URL for the Acorn image and click Create.
ghcr.io/infracloudio/nodejs-acorn:v20.#.#-#
Note: The App will be deployed in the Acorn Sandbox Environment. As the App is provisioned on AcornPlatform in the sandbox environment it will only be available for 2 hrs and after that it will be shutdown. Upgrade to a pro account to keep it running longer.
- Once the Acorn is running, you can access it by clicking the Endpoint or the redirect link. 4.1. Running Application on Acorn 4.2. Battleship game homepages 4.3. Running Battleship game as a single player
As mentioned previously, running the acorn application using CLI lets you understand the Acornfile. With the CLI option, you can customize the sample app to your requirement or use your Acorn knowledge to run your own Battleship Nodejs application.
To run the application using CLI you first need to clone the source code repository on your machine.
$ git clone https://github.com/infracloudio/nodejs-acorn.git
Once cloned here’s how the directory structure will look.
.
├── Acornfile
├── Dockerfile
├── LICENSE
├── nodejs.svg
|........
├── public
│ ├── app.js
| |.....
│ ├── singleplayer.html
│ └── style.css
├── README.md
├── server.js
We have the sample Battleship Nodejs Application ready. Now to run the application we need an Acornfile which describes the whole application without all of the boilerplate of Kubernetes YAML files. The Acorn CLI is used to build, deploy, and operate Acorn on the Acorn cloud platform. It also can work on any Kubernetes cluster running the open source Acorn Runtime.
Below is the Acornfile for deploying the Battleship nodejs app that we created earlier:
containers: {
server: {
build: {
context: "."
dockerfile: "./Dockerfile"
}
dirs: "/usr/src/app": "./"
ports: publish: "3000:3000/http"
}
}
There is single requirements for running Battleship Nodejs Application
- Application Itself
The above Acornfile has the following elements:
- Containers: We define a single container named server and define the following configurations:
- build: details required to build the Nodejs Application
- ports: port number the application is listening on and we are also specifying publish field which will give the url to access the application.
- dirs: here we are giving our code dirs
We have already logged in using Acorn CLI now you can directly deploy applications on your sandbox on the Acorn platform. Run the following command from the root of the directory.
$ acorn run -n battleship -i
Below is what the output looks like.
As it is a simple Battleship game build using nodejs it provides two modes.
- Singleplayer
- Multiplayer
On the homepage you will see the option to select the mode you want to play with if you select single player your opponent will be the computer and if you select multiplayer you can copy the URL and open it in the second tab which will act as second player.
If you are developing your application and don't want to start and stop the application everytime you make the changes you can use the incredibly helpful Acorn Dev Mode. Running in dev mode allows you to do continous development of the application, and have your running instance reflect the changes as soon as you save your changes. Run the following command.
$ acorn dev -n battleship .
Using the Dev mode you can easily modify the application as per your requirement and once the application is working as expected and is ready to be built and packaged, push it to a registry. You can push it to any OCI registry. We will use Github Container Registry in this example. Once published, you can use the acorn image to deploy it directly in the Acorn platform using the dashboard or CLI, as described previously.
Log in to the registry with the command below and follow the prompts.
$ acorn login ghcr.io
Build and push the image with the below command.
$ acorn build --push -t ghcr.io/infracloudio/nodejs-acorn:v20.1.0-1
Once the application is built and pushed you can use those images to run your application on Acorn Platform.
- The App is provisioned on Acorn Platform and is available for two hours. Upgrade to Pro account for anything you want to keep running longer.
- After deploying you can edit the Acorn Application or remove it if no longer needed. Click the Edit option to edit your Acorn's Image. Toggle the Advanced Options switch for additional edit options.
- Remove the Acorn by selecting the Remove option from your Acorn dashboard.
In this tutorial we show how we can use the Acornfile and get our Nodejs Battleship application up and running and it’s very easy to make changes to the Application file when you are developing it without the need of restarting your application. And If you are looking to run the application directly you can run it on Acorn Platform by providing the image name.