AWS Application Deployment Basics – Docker Containers

Introduction

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

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

Today, we will just run yet another .NET core application on same private EC2 instance. Just like in 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 knows basics of docker. If you are new to this topic, there are 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. Couple of options there to start with docker, however, we will install and use on our EC2 instance in private subnet.

Docker installation is covered in great details on official docker website. DigitialOcean also have 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:

>> sudo apt update

>>sudo apt install apt-transport-https ca-certificates curl software-properties-common

>>curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

>>sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

>>sudo apt update

>>apt-cache policy docker-ce

>>sudo apt install docker-ce

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

sudo systemctl status docker

Permission for the Current User

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

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

sudo usermod -aG docker ubuntu

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

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

Next, I build the docker image:

docker image build -t locallogin .

here is image build process:

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

docker run -d --rm --name locallogin -p 6000:5000 locallogin

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

curl localhost:6000/api/weatherforecast

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 Public EC2 instance. Add a location block for the netcore application container, save and restart NGINX

sudo nano /etc/nginx/nginx.conf
sudo service nginx restart

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

The application is working as expected.

Architecture

Here is the updated view of EC2 instances and applications:

Summary

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

My Recent Books