Containerizing a Node.js and Express App¶
Let's see how we can containerize a Node.js
and Express
application.
Step 1: Create Application Using Node.js and Express¶
The first step is to create an application using Node.js
and Express
framework.
Create server.js
and package.json
files as follows:
Here's what your folder structure should look like:
The above Express app has the following routes:
What does each route return?
GET /
returns a JSON object containing Host and Version.GET /health
returns the health status.GET /random
returns a random integer between 1 and 10
Step 2: Test the Application Locally¶
Go to the folder containing package.json
and run the following commands:
Note
You must have Node.js
installed on your local machine to run the above commands.
If there are no errors, your Express application will begin serving requests on the specified port, which in our case is 5000
.
Verify if the endpoints are working as expected:
You can also access the endpoints mentioned above directly from your browser.
Note
When you run npm install
, it generates a package-lock.json
file to record and lock down the specific versions of installed packages and their dependencies. This ensures consistency in package versions across different environments and collaborations.
Step 3: Create Dockerfile¶
Now, let's containerize our application. To do that, we first need to create a Dockerfile
with instructions on how to build the image.
Create the Dockerfile
as follows:
Brief explanation of each command in Dockerfile
-
FROM node:18
: Specifies the base image as Node.js version 18, which serves as the foundation for your image. -
WORKDIR /usr/src/app
: Sets the working directory inside the container to/usr/src/app
. -
COPY package*.json ./
: Copies thepackage.json
andpackage-lock.json
(if present) from the local directory into the container's working directory. -
RUN npm install
: Runs thenpm install
command inside the container to install the application's dependencies. -
COPY . .
: Copies the contents of the local directory (your Node.js application code) into the container's working directory. -
EXPOSE 5000
: Informs Docker that the container will listen on port 5000, though it doesn't actually publish the port to the host. -
CMD [ "node", "server.js" ]
: Specifies the command that will be executed when the container starts. In this case, it runs your Node.js application using theserver.js
script.
These steps are essential for building a Docker image for your Node.js
application, including setting up the environment, copying code and dependencies, and defining how to run the application within the container.
Here's what your folder structure should look like at this point:
Step 4: Build the Docker Image¶
With the application code and Dockerfile
prepared, we are now ready to build the Docker image for our application.
Build the Docker image as follows:
# Command template
docker build -t <image-name>:<image-tag> <path-to-Dockerfile>
# Actual command
docker build -t my-node-express-image:v1 .
The above command will build an image named my-node-express-image
with tag v1
.
Step 5: Verify the Image¶
List images and verify if the newly created image is present:
Look for the image name
and the tag
we specified while building the image.
Step 6: Run a Container From the Image¶
Now that we have the image ready, let's create a container from it and test the application.
What does the above command do?
- It runs a container named
my-node-express-container
in detached mode, using the imagemy-node-express-image:v1
. - Additionally, it exposes port
5000
of the container to port5001
on the host
Step 7: Test the Application¶
-
Test the application from within the container:
-
Start a shell session inside the container
-
Access the endpoints
-
-
Test the application from host computer (Outside the container):
Open any browser on your host computer and hit the following endpoints to verify if port mapping is working as expected.
Step 8: View Container Logs¶
# Command template
docker logs -f <container-id/container-name>
# Actual command
docker logs -f my-node-express-container