Prerequisites
To follow this tutorial, you will need the following:- Node.js (v20.15.1 at the time of writing this doc).
- npm as a package manager for installing and maintaining dependencies. This usually comes bundled with Node.js.
- A basic knowledge of Node.js and Express.js.
- [docker] to build standalone containers to serve your production app.
This guide assumes that you have a basic knowledge of the above-mentioned technologies and tools. If you are not
familiar with any of them, it is highly recommended to review their official documentation and tutorials to get up to
speed before proceeding with this guide.
Scaffolding the Node.js app
Create a new folder for your Node.js project. In the following examples, we’ll use the project namenode-project
.
package.json
file for your
application. For more information on how package.json
works, see the specifics of
npm’s package.json handling. Run
<your project name>
) and version of your application.
For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
app.js
, or whatever you want the name of the main file to be. If you want it to be index.js
, hit RETURN to
accept the suggested default file name.
Installing Express.js
At this point, you have a Node.js project ready. Let’s install Express and save it in the dependency list by runningAt the time of writing this guide, the latest
express
version is 4.19.2.Hello World! example
Create a file calledapp.js
as mentioned earlier for the entry point and write a simple Express app that returns
Hello World!
when navigating to the /
path of the URL.
app.js
node_modules
folder in your project), so you can start your project by running the command:
http://localhost:3500
in your browser of choice. You should see the Node.js app running and printing
Hello World!
in the document.
Create the Docker image
Dockerizing makes the app run anywhere, agnostic of the platform. As long as Docker is installed, whether it’s Windows, Mac, or Linux, it can run with the same behavior. Before creating the Dockerfile, let’s create a.dockerignore
file and add the contents that should not be copied over
to the Docker file system.
.dockerignore
Creating a
.dockerignore
file and adding folders like node_modules
is a must since dependencies will be installed
while building the image based on the platform preferences used. Copying those from the file system will overwrite the
installed dependencies and might error out during deployment.source code
and node_modules
to be present while running the application. You can opt for multi-stage
builds if your process fits that approach.
Dockerfile
node-app
image for platform linux/amd64
and tags it as latest
.
If you are locally testing your application, you can skip the platform
key to build the images
Run the Docker image
Let’s run the Docker container using the image created of the Node application with the command below.-it
: enables interactivity with TTY.--rm
: tells the Docker Daemon to clean up the container and remove the file system after the container exits.--name node-app
: Name of the containernode-app
.-e PORT=3500
: Sets the environment variable PORT in Docker to3500
.-d
: Runs the container in detached (background) mode. You can skip the flag to see the logs directly in your terminal window.-p 3500:3500
: Maps port 3500 on your host to port 3500 in the container.node-app
at the end is the name of the image.
http://localhost:3500
to see the Node.js application running inside the Docker
container.
Hurray! Now we have created and packaged a Node app for production use.