A Dockerfile is a document that contains all the commands a user could call on the command line to create an image.
Running a Node.js + Express Server on Serverless Container
- serverless
- containers
- caas
- nodejs
- express-server
Express is a lightweight Node.js framework designed to ease building applications and APIs.
In this tutorial you will learn how to configure your Express.js application to run on Scaleway Serverless Container.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- A Serverless Containers namespace
- Installed Node.js, NPM, and Docker on your computer
Creating an Express.js application
To begin, we need to create a web server with Node.js and an Express.js framework.
-
Create a new folder for the project:
mkdir my_nodejs_project -
Install the
express
library:npm install expressTwo files are created during this step:
package.json
andpackage-lock.json
. -
Create a
server.js
file with the following code. This creates a simple Web Server using Express.js:const express = require('express');// Constantsconst PORT = process.env.PORT || 8080;const HOST = '0.0.0.0';// Appconst app = express();app.get('/', (req, res) => {res.send('Hello World');});app.listen(PORT, HOST);console.log(`Running on http://${HOST}:${PORT}`);
Configuring the Docker image
To Dockerize our simple web app, we will use the official Node.js image.
-
Create a Dockerfile using the following command:
touch DockerfileTip -
Open the Dockerfile in a text editor and specify the following actions:
- Define the node version you want to use:
FROM node:14- Create a directory to store your application code:
# Create app directoryWORKDIR /usr/src/app- Install the application’s dependencies using the npm binary:
# Install app dependencies# A wildcard is used to ensure both package.json AND package-lock.json are copiedCOPY package*.json ./RUN npm install- Copy your code inside the Docker image:
# Bundle app sourceCOPY . .- Your app binds to the port defined in the environment variable PORT. Define it and expose the port 8080:
ENV PORT 8080EXPOSE 8080- Use CMD to run the node server.js command that will start your application:
CMD [ "node", "server.js" ]The final Dockerfile should look like this example:
FROM node:14# Create app directoryWORKDIR /usr/src/app# Install app dependencies# A wildcard is used to ensure both package.json AND package-lock.json are copiedCOPY package*.json ./RUN npm install# Bundle app sourceCOPY . .ENV PORT 8080EXPOSE 8080CMD [ "node", "server.js" ]To lighten your image, some folders and files need to be excluded from the image generation:
-
Create a
.dockerignore
file in the same directory as your Dockerfile:touch .dockerignore -
Open the
.dockerignore
file in a text editor and add the following content:node_modulesnpm-debug.log
Deploying the application on Serverless Containers
- Build your image using the following command:
docker build . -t <your application name>
- Push the created image into the Container Registry linked to your Containers namespace
Tip
The registry name starts with
funcscw
+ the namespace name. - Deploy the image using the Scaleway console.
Conclusion
You have built, pushed, and deployed your first web application using Node.js and Express Server on Scaleway Serverless Containers. For more information about the Containers product, refer to our dedicated product documentation.