AWS Application Deployment Basics: Docker Containers
In this post, we will use Docker containers and NGINX to deploy applications to AWS.
Join the DZone community and get the full member experience.Join For Free
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:
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:
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 the following command:
sudo systemctl status docker
Permission for the Current User
Next, I tried to execute the
docker images command and it shows an error regarding permissions:
Let’s add the current user to the 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 command:
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:
Next, I build the Docker image:
docker image build -t locallogin .
Here is the 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:
You can see that our application is running and we can access the data.
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
sudo nano /etc/nginx/nginx.conf
sudo service nginx restart
Because NGINX webserver can be reached from anywhere in the world (due to our configs) :
The application is working as expected.
Here is the updated view of EC2 instances and applications:
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!
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.