article

Micro Services Part I: Node, Docker, and Docker Compose

AWS EC2, Ubuntu logos

Log into your AWS account and go to EC2. You'll know you're in your EC2 dashboard if you see something like this:

AWS EC2 dashboard view

EC2 Dashboard

We're going to launch a new EC2 instance, so click that blue button labeled “Launch Instance”. Once there, you will see a list of different machine images. Let's chose the Ubuntu Server 18.04 LTS. Why, cause I like the Ubuntu distribution and it's eligible for the free tier. 😉

AWS EC2 launch instance view

Launch Instance (chose AMI)

We're going to leave the default selected for this small project but the 64-bit (Arm) version of this AMI is supposed to maximize efficiency, therefore being optimized for performance and cost. This simple instance won't use many resources at all and I will tear it down after building it. 🤷🏿‍♂️

Once we have hit the button labeled “Select” we will be taken to the instance type screen. Here we'll need to chose what type of instance we will have. It's important that we choose a free tier. The “t2.micro” tier will be the default, so let's stick with it.

AWS EC2

Instance Type (t2.micro “free”)

We could stop here, but let's skip on to “Step 6: Configure Security Group”. I have elected to skip steps: 3, 4, 5 because one, they are optional and two, we are creating a very simple project and we want to get it up and running quickly. I'm not against configuring the instance or adding storage or adding tags; however, the defaults are enough to get us going.

What we need to do in Step 6 is to create a security group so that we will be able to SSH into our AMI and “spin up our container”. It will come with a default name and description but I'm going to change them both to node-test-demo.

Next I am going to change the “Source” of the default rule to “My IP”. I will then “Add” a new HTTP rule. Once again, for the source select “My IP”. It should look something like this when you finish.

AWS EC2 Configure security group

Security Group

Once we start to SSH into our AMI, we'll do that on Port 22. And when the container is running on our AMI and we have our Public DNS, we'll be able to visit it as it will be opened on Port 80. We could assign that port to whatever we want but by leaving it on 80 (the default) it would be like visiting http://localhost versus assigning it to another port, let's say, Port 8080. In that case we would then have to visit http://localhost:8080.

Cool!

Now that we have that set up click the button labeled “Review and Launch”.

Once you are at the launch screen click “Launch” and you will be prompted to either choose an existing key pair or to create a new one. Create a new one. Name it and download it. I named mine node-test.

node-test.pem file GIF

node-test.pem

Cool! You've just launched a new Ubuntu Server on AWS EC2!

Now after you've downloaded your node-test.pem file you should store it somewhere safe. I store my *.pem files in the .ssh directory in my root. You should put yours someplace similar.

Step 2: Connect to Instance

Now that we have successfully created an instance we need to grab our instance Instance ID so that we can connect to our Linux instance using SSH. If you cannot find your Instance ID, go to your EC2 dashboard, and on the right hand side click where it says “Instances” and it should show up.

AWS EC2 dashboard instance running

node-test instance

Next, we need to bring up our SSH client so that we can actually work on our AWS EC2 instance.

If we right click on the Public DNS we should see a pop-up that has instructions on connecting.

GIF Showing AWS EC2 connecting

Connection Instructions

That first command is the “change mode” command and it sets the permissions of your private key file so that only you can read it. I stored my node-test.pem in my .ssh directory so I will correct my path accordingly.

Enter the second command and update your path if necessary to make the actual connection.

GIF Showing Ubuntu terminal connecting

Connecting

Once you have entered those commands you are in!

Great!

**I also ran an additional command $ sudo su so that I could run commands as the root user.

Step 3: Stand up our container

The last thing that we need to do is to get our container onto this instance.

First run the command $ apt-get update. We should have the latest but let's be on the safe side.

Next run the following commands:

$ apt install docker.io
$ docker pull jahaplace/node-test
$ docker run -d -p 80:8080 jahaplace/node-test

First, we install Docker. Next, I have already made node-test a docker repo and it can be pulled from the Docker Hub so that's what we're doing in the second command. If you want to know how to create an image from scratch on an EC2 instance it's very similar to what we did in part one Micro Services Part I: Node, Docker, and Docker Compose. The difference is that you would need to use the terminal and Vim (or some other editor) to create your files. If you would like to see me do that, let me know and I will create a video of me demonstrating how that's accomplished.

The last command is telling docker to run our container in a detached mode and map the outside port to 80 while the app runs on 8080 inside of the container.

After doing that, take your “Public DNS” (which can be found on your instance dashboard), paste it into the browser and voila! Your app is now running in the cloud (on someone else's computer)!

AWS EC2 running Ubuntu terminal

Running node-test

I hope you found this helpful! Please let me know!

Microservices
Docer
Containers
Backend Development
Software Engineering
DevOps