top

How to Deploy a Node.js Application to Amazon Web Services Using Docker

Table of Contents1. Introduction2. Prerequisites3. A quick primer on Docker and AWS4. What we’ll be deploying 5. Creating a Dockerfile6. Building a docker image7. Running a docker container8. Creating the Registry (ECR) and uploading the app image to it9. Creating a new task definition10. Creating a cluster11. Creating a service to run it12. Conclusion1. IntroductionWriting code that does stuff is something most developers are familiar with. Sometimes, we need to take the responsibility of a SysAdmin or DevOps engineer and deploy our codebase to production where it will help a business solve problems for customers.In this AWS docker node.js tutorial, I’ll show you how to dockerize a Node.js application and deploy it to Amazon Web Service (AWS) using Amazon ECR (Elastic Container Registry) and ECS (Elastic container service).2. Prerequisites To deploy Node.js applications to AWS using Docker, you’ll need the following:Node and Npm: Follow this link to install the latest versions.Basic knowledge of Node.js.Docker: The installation provides Docker Engine, Docker CLI client, and other cool stuff. Follow the instructions for your operating system. To check if the installation worked, fire this on the terminal:docker --versionThe command above should display the version number. If it doesn’t, it means the installation didn’t complete properly.4. AWS account: Sign up for a free tier. There is a waiting period to verify your phone number and bank card. After this, you will have access to the console.5. AWS CLI: Follow the instructions for your OS. You need Python installed.3. A quick primer on Docker and AWSDocker is an open source software that allows you to pack an application together with the required dependencies and environment in a ‘Container’ that you can ship and run anywhere. It is independent of platforms or hardware, and therefore the containerized application can run in any environment in an isolated fashion.Docker containers solve many issues, such as when an app works on a coworker's computer but doesn’t run on yours, or it works in the local development environment but doesn’t work when you deploy it to a server.                                                       Amazon Web Services (AWS) offers a reliable, scalable, and inexpensive cloud computing service for businesses. As I mentioned before, this tutorial will focus on using the ECR and ECS services.4. What we’ll be deployingLet’s quickly build a sample Node.js app on docker that we’ll use for the purpose of this tutorial. It going to be very simple app.Enter the following in your terminal:// create a new directory mkdir sample-nodejs-app // change to new directory cd sample-nodejs-app // Initialize npm npm init -y // install express npm install express // create an server.js file touch server.jsOpen server.js and paste the code below into it:Start the app with:node server.jsAccess it on http://localhost:3000. You should get Hello world from a Node.js app! displayed in your browser. The complete code is available on GitHub.Now let’s take our very important app to production
Rated 4.0/5 based on 21 customer reviews
Normal Mode Dark Mode

How to Deploy a Node.js Application to Amazon Web Services Using Docker

Emmanuel Yusufu
Blog
14th Jun, 2018
How to Deploy a Node.js Application to Amazon Web Services Using Docker

Table of Contents

1. Introduction

2. Prerequisites

3. A quick primer on Docker and AWS

4. What we’ll be deploying 

5. Creating a Dockerfile

6. Building a docker image

7. Running a docker container

8. Creating the Registry (ECR) and uploading the app image to it

9. Creating a new task definition

10. Creating a cluster

11. Creating a service to run it

12. Conclusion

1. Introduction

Writing code that does stuff is something most developers are familiar with. Sometimes, we need to take the responsibility of a SysAdmin or DevOps engineer and deploy our codebase to production where it will help a business solve problems for customers.

In this AWS docker node.js tutorial, I’ll show you how to dockerize a Node.js application and deploy it to Amazon Web Service (AWS) using Amazon ECR (Elastic Container Registry) and ECS (Elastic container service).


2. Prerequisites 

To deploy Node.js applications to AWS using Docker, you’ll need the following:

  1. Node and Npm: Follow this link to install the latest versions.

  2. Basic knowledge of Node.js.

  3. Docker: The installation provides Docker Engine, Docker CLI client, and other cool stuff. Follow the instructions for your operating system. To check if the installation worked, fire this on the terminal:

docker --version

The command above should display the version number. If it doesn’t, it means the installation didn’t complete properly.

4. AWS account: Sign up for a free tier. There is a waiting period to verify your phone number and bank card. After this, you will have access to the console.

5. AWS CLI: Follow the instructions for your OS. You need Python installed.


3. A quick primer on Docker and AWS

Docker is an open source software that allows you to pack an application together with the required dependencies and environment in a ‘Container’ that you can ship and run anywhere. It is independent of platforms or hardware, and therefore the containerized application can run in any environment in an isolated fashion.

Docker containers solve many issues, such as when an app works on a coworker's computer but doesn’t run on yours, or it works in the local development environment but doesn’t work when you deploy it to a server.   

                                                 

  Amazon Web Services (AWS) offers a reliable, scalable, and inexpensive cloud computing service for businesses. As I mentioned before, this tutorial will focus on using the ECR and ECS services.


4. What we’ll be deploying

Let’s quickly build a sample Node.js app on docker that we’ll use for the purpose of this tutorial. It going to be very simple app.

Enter the following in your terminal:

// create a new directory
mkdir sample-nodejs-app
// change to new directory
cd sample-nodejs-app
// Initialize npm
npm init -y
// install express
npm install express
// create an server.js file
touch server.js

Open server.js and paste the code below into it:

Start the app with:

node server.js

Access it on http://localhost:3000. You should get Hello world from a Node.js app! displayed in your browser. The complete code is available on GitHub.

Now let’s take our very important app to production

Emmanuel

Emmanuel Yusufu

Blog Author

Emmanuel Yusufu is a Full-stack Web Developer specializing in React and Nodejs. He is fascinated by the potentials JavaScript in modern software development and always eager to explore new frameworks.

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE OUR BLOG

Follow Us On

Share on