Introduction

In the previous few posts in this series, we deployed and ran a couple of applications on our EC2 based infrastructure. Here is how our architecture currently looks from the previous post:

Current Architecture to Begin Article

Our applications are running in a private subnet and NGNIX working as a reverse proxy is allowing access over the internet.

Today, we will just run yet another .NET core application on the same private EC2 instance. Just like in the previous post, we will serve this application using NGNIX. However, this time application will be running as a Docker container.

Docker on Ubuntu

Docker is a great tool to simplify application development and running. We will not go into the details of Docker or container technology. I am assuming you already know the basics of Docker. If you are new to this topic, there are a great many resources available online and I also wrote few posts and a book on this topic, which you can check.

AWS offers services to run Docker, e.g., ECS, EKS. It also offers AMIs with Docker installed. A couple of options there to start with Docker, however, we will install and use it on our EC2 instance in the private subnet.

Docker installation is covered in great detail on the official Docker website. DigitialOcean also has a nice article about how to install Docker on Ubuntu. You can check it on this link. Following are commands to install Docker on Ubuntu:

Shell


Once Docker is installed on Ubuntu, you can check the installation status using the following command: 

Shell


Docker Installation Status

Permission for the Current User

Next, I tried to execute the docker images command and it shows an error regarding permissions:

Docker Images Permission Denied

Let’s add the current user to the Docker group, so I can run Docker commands without sudo:

Shell


Now, re-login to EC2 for this to take effect and once again try the Docker images command: 

Trying Docker Images Command Again

For the application, I have a .NET Core project from one of my earlier articles and I have cloned the repository for the LocalLogin project on my Ubuntu EC2 instance. cd into the project directory and it contains a very basic Dockerfile as shown below:

Basic Dockerfile

Next, I build the Docker image:

Shell


Here is the image build process:

Docker Image Build Process

Once the image is built, we can simply run the container from it:

Shell


And it will start our container in detached mode. Now if I use CURL to access the API:

Shell


Application Is Running

You can see that our application is running and we can access the data.

Configure NGNIX

Just like in the previous post. Open the NGNIX configuration file on the Public EC2 instance. Add a location block for the netcore application container, save and restart NGINX

Shell


Adding a Location Block

Shell


Because NGINX webserver can be reached from anywhere in the world (due to our configs) :

Application is Working As Expected

The application is working as expected.

Architecture

Here is the updated view of EC2 instances and applications:

Updated View of EC2 Instances and Applications

Summary

Docker simplifies application delivery, installation, and execution. In this post, we installed Docker on the EC2 instance, configured permissions, and spin a .NET core application container. We also saw that wiring up with the NGINX process is simple. Let me know if you have any questions or comments. Till next time, Happy Coding!