How to dockerize your Node.js Express application for AWS Fargate?
My first project with Node.js - an asynchronous event-driven JavaScript runtime, designed to build scalable network applications - was building an online trading platform in 2013. Since then, Node.js is one of my favorite technologies. I will show you how to dockerize your Node.js application based on Express - a fast, unopinionated, minimalist web framework - and run it on AWS Fargate in this blog bost. I like AWS Fargate because running containers in the cloud were never easier.
This blog post is an excerpt from our book Rapid Docker on AWS.
Read on to learn how to build a Docker image for a Node.js application.
Building the Docker image
The Dockerfile
is based on the official Node.js Docker Image: node:10.16.2-stretch
. Static files (folders img
and css
) are served by Express as well as the dynamic parts. The following details are required to understand the Dockerfile
:
envsubst
is used to generate the config file from environment variablesnpm ci --only=production
installs the dependencies declared inpackage.json
(package-lock.json
, to be more precise)- The Express application listens on port 8080
- The Express application’s entry point is
server.js
and can be started withnode server.js
A simple server.js
file follows. Yours likely is more complicated.
const express = require('express'); |
Customization Most likely, your folder structure is different. Therefore, adapt the Copy config files and Copy Node.js files section in the following Dockerfile to your needs.
FROM node:10.16.2-stretch |
The custom entrypoint is used to generate the config file from environment variables with envsubst
.
|
Next, you will learn how to test your containers and application locally.
Testing locally
Use Docker Compose to run your application locally. The following docker-compose.yml
file configures Docker Compose and starts two containers: Node.js and a MySQL database.
cloudonaut plus
Staying ahead of the game with Amazon Web Services (AWS) is a challenge. Our weekly videos and online events provide independent insights into the world of cloud. Subscribe to cloudonaut plus to get access to our exclusive videos and online events.
Subscribe now!version: '3' |
The following command starts the application:
docker-compose -f docker/docker-compose.yml up --build |
Magically, Docker Compose will spin up two containers: Node.js and MySQL. Point your browser to http://localhost:8080 to check that your web application is up and running. The log files of all containers will show up in your terminal, which simplifies debugging a lot.
After you have verified that your application is working correctly, cancel the running docker-compose
process by pressing CTRL + C
, and tear down the containers:
docker-compose -f docker/docker-compose.yml down |
Deploying on AWS
You are now ready to deploy your application on AWS.
(1) Build Docker image:
docker build -t nodejs-express:latest -f docker/Dockerfile . |
(2) Create ECR repository:
aws ecr create-repository --repository-name nodejs-express \ |
(3) Login to Docker registry (ECR):
$(aws ecr get-login --no-include-email) |
(4) Tag Docker image:
docker tag nodejs-express:latest \ |
(5) Push Docker image:
docker push \ |
There is only one step missing: you need to spin up the cloud infrastructure.
- Use our Free Templates for AWS CloudFormation.
- Use our cfn-modules.
- Use the blueprint from our book Rapid Docker on AWS.