Deploy Node.js Server to AWS EC2 with Docker
Learn to set up Docker with Node.js, EC2 instance & its configuration

Once your server is ready for production, you must need one of the available solutions to make it open to the public. Most famous and well know service is Amazon Web Service (AWS) Elastic Compute Cloud (EC2). Ec2 is a main part of AWS, many top-tier services build onto it.
In this article, we first create a Node.js application using Docker, configure EC2, and then deploy it.
Before moving ahead let’s first set up AWS and Docker. You need to create an account with AWS in order to use EC2, the same goes with Docker.
If you don’t know the role of Docker here then let me tell you in short: Docker will bundle Node.js application into small easily deployable units, so we can use it in any Docker environment.
Here I assume that you already have the Node.js app with you, so let’s skip to the next step.
Dockerizing the Node.js Application
Create a file in the same directory called Dockerfile
FROM node:13-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
This is just a basic Docker file, it will be used in a simple Node.js app. Now let’s build a Docker image:
> docker build . -t ec2-app
> docker run -p 3000:3000 ec2-app
Finally, let’s push a Docker image to Docker Hub to use elsewhere:
> docker login # Use your Docker Hub credentials here
> docker tag app-tag <YOUR_DOCKER_USERNAME>/AppName
> docker push <YOUR_DOCKER_USERNAME>/AppName
Now your Docker image is ready to use.
Setting up EC2
After dockerization, let’s move to EC2. First, need to create an EC2 instance to run the Node.js app with it. Hope you logged in with AWS.
Just click on ‘Services’ dropdown menu at the top of the page, and search for ‘EC2’ [Look Image for ref: page may look different]

Now click on the launch instance button and the page you see will be something like this:

Here we need to select Amazon Machine Image (AMI), It is an ‘out of box’ server and comes with many config options. As of now we will choose ‘quick start AMI’ or choose one that has Ubuntu support.

Click on the select button and it will take us to the next step. In the next step, you’ll see a list of instances. Here comes the main part of calling and costing. Choose wisely it may cost you a bit more but as of now, we will choose a free one. The t2.micro
is eligible to use it freely, so let’s choose it now:

You just need to select a checkbox and press Review and Launch
After that on the next page, you’ll see an option to generate key pair. Click on ‘Create a new key pair’. Under ‘Key pair name’, enter some name that you want to give to your key pair. Don’t forgot to download it, it will be used in the future. Now just click on Launch Instance
and it will be up and running:

For the next step click on the highlighted link, it will navigate you to the instance details page.
Security groups basically handle your instance’s security. It will handle who can access, who has proper privileges etc. Click on the highlighted link below:

Here we will add out IP and PORT, so that our req will be fulfilled by the instance. 0.0.0.0/0
meaning anywhere

Set our port i.e 3000
and make it available from anywhere. Finally, all configuration was done.
Connecting to Your EC2 Instance
By clicking on left side on `instance` you’ll be redirected to the instance page and there you need to select the instance that you just created. Open a terminal in your local machine and run following commands on a key that you downloaded (it has .pem
extension) :
chmod 400 <NAME_OF_KEYPAIR_FILE> ssh -i <NAME_OF_KEYPAIR_FILE>ec2-user@<PUBLIC_DNS>
So now you’re connected to your instance. Now launch your app via Docker:
> docker run -p 3000:3000 <YOUR_DOCKER_USERNAME>/AppName
Now you can check your app running on the instance’s IP address i.e :
<PUBLIC_DNS>:PORT
Hurray. It’s live. EC2 is really an awesome tool to use. Try to explore AWS as much as you can. You’ll be amazed by some services of AWS.
Hope you learn something important. If you like this article please share it with the needy ones.
Let’s connect on Twitter, LinkedIn, and GitHub! For contribution: BUY ME A COFFEE
If you have further queries feel free to reach out!
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.