How to dockerize your Node.js Express application for AWS Fargate?
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.
Dockerfile is based on the official Node.js Docker Image:
node:10.16.2-stretch. Static files (folders
css) are served by Express as well as the dynamic parts. The following details are required to understand the
envsubstis used to generate the config file from environment variables
npm ci --only=productioninstalls the dependencies declared in
package-lock.json, to be more precise)
- The Express application listens on port 8080
- The Express application’s entry point is
server.jsand can be started with
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.
The custom entrypoint is used to generate the config file from environment variables with
Next, you will learn how to test your containers and application 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.
Special offer: cloudonaut t-shirt
Do you love our blog posts and podcast episodes? Unlock our weekly videos and online events by subscribing to cloudonaut plus.
Special offer: Join cloudonaut plus before November 30th, and we will send you a cloudonaut t-shirt for free.Subscribe now!
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
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 \
--query 'repository.repositoryUri' --output text
(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.