Deploying Multi Container Docker Compose Application on AWS EC2

Deploying Multi Container Docker Compose Application on AWS EC2

Today in this detailed article I will demonstrate how to deploy simple Voting App running across multiple Docker containers on AWS EC2 instance.

First of all we will deploy and run a Python sample Docker Compose app which already available on the official documentation of Docker. Also you can check the GitHub repository of the multi container application for more info.

The Architecture of the Application

No alt text provided for this image

  • A front-end web app in?Python?which lets you vote between two options
  • A?Redis?which collects new votes
  • A?.NET?worker which consumes votes and stores them in…
  • A?Postgres?database backed by a Docker volume
  • A?Node.js?web app which shows the results of the voting in real time

I will divide the steps of deploying the Docker Compose app into the following:

  • Creating EC2 Instance on AWS
  • Connecting to EC2 instance
  • Updating EC2 instance packages and installing Docker
  • Deploying Voting App Docker Compose Application

1. Creating EC2 Instance

In order to deploy Voting web application on AWS EC2 virtual server using docker compose file you should follow these steps:

  • Login to your AWS account and then choose the preferred region for deploying the application

No alt text provided for this image

  • From AWS console choose EC2 from AWS services

No alt text provided for this image

  • Then from EC2 console click on launch instance

No alt text provided for this image

  • Create a name for your virtual EC2 server on AWS

No alt text provided for this image

  • Select Amazon machine image (AMI) for EC2 instance:

The suitable version for docker compose deployment is Ubuntu server 22.04 LTS (HVM) SSD Volume

No alt text provided for this image

  • After that we should choose EC2 instance type: In our sample project the free tier type t3 micro with 2 vCPU and 1 GB RAM would be sufficient. for more intensive computing applications C and M series will be better option

No alt text provided for this image

  • Important step here is to create key pair to connect to your EC2 instance securely via SSH: After clicking create key pair it will download .pem file which contains private key that allow you to directly connect to your EC2 instance from you local machine using SSH protocol. Ensure to store this file in secure location in your computer

No alt text provided for this image

  • For the network configuration of EC2 instance you can use the default VPC and Subnet of your AWS account which I did it or if you want you can set your own VPC with different configurations. next you should create new Security Group and set the rules that control the traffic to your EC2 instance. the default rule is to allow SSH traffic from any where It’s not recommended for security reasons. it’s better to allow only specific IP addresses to connect via SSH to your instance. Also, you can change the name of security group later.

No alt text provided for this image

  • Finally for you should set your storage configuration in our case we will leave the default which is 8 GB with General Purpose SSD (gp2)

No alt text provided for this image

For the advanced details we don’t need to use it in our case. After that you can click on launch instance to create our EC2 virtual server on AWS.

2. Connecting to EC2 instance

We should connect now to the created EC2 virtual server from our local machine via SSH by following these steps:

  • From EC2 dashboard you can check all AWS EC2 instances select the created EC2 instance then click on connect

No alt text provided for this image

  • Then you need to follow the commands in SSH client tab

No alt text provided for this image

a. Open your terminal or command line

b. Change your directory on the terminal to the location which contains the key pair .pem file you downloaded previously from AWS console.

c. Connect to your instance using the final command mentioned in Example by AWS SSH client

ssh -i "test ec2.pem" ubuntu@ec2-3-28-57-37.me-central-1.compute.amazonaws.com        

then it prompt you with the message:

Are you sure you want to continue connecting (yes/no/[fingerprint])?         

just type yes and the process will be completed.

No alt text provided for this image

After that the connection will be established with your EC2 virtual server on AWS and the terminal will change its heading to ubuntu@ip-172-31-14-157:~$ which is the IP address of your EC2 Ubuntu server on AWS.

3. Updating EC2 instance packages and installing Docker

  • Run the following commands to update your Ubuntu server packages:

sudo apt update

sudo apt upgrade        
No alt text provided for this image

then It will prompt you to upgrade Ubuntu just follow the default setting.

  • Now we should download and install Docker on EC2 instance

a. Run this command to download docker engine

curl -fsSL https://get.docker.com -o get-docker.sh        

b. Then run ls command to check if the get-docker.sh installer downloaded

No alt text provided for this image

c. Run this command to run Docker installer on the Ubuntu server

sh get-docker.sh        

d. After that to confirm that docker installed in Ubuntu server run

docker --version        
No alt text provided for this image

  • Finally we need to install Docker Compose in EC2 instance

a. First download docker compose from the GitHub repository using the command

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose        
No alt text provided for this image

b. Run the command to make it executable

sudo chmod +x /usr/local/bin/docker-compose        

c. Then confirm if installed on Ubuntu

docker-compose -v        
No alt text provided for this image

4. Deploying Voting App Docker Compose Application

First we need to clone Voting App GitHub repository which contains the docker compose file for deploying multi container application which define all services required for running it:

  • In order to be authorized to connect to your GitHub account and clone the repository we need to set up SSH key on Ubuntu server

run this command to generate SSH key:

?ssh-keygen -t rsa        
No alt text provided for this image

To view the public SSH encryption key just run this command with the path of the key:

cat /home/ubuntu/.ssh/id_rsa.pub        
No alt text provided for this image

Now you need to copy this encrypted key to your GitHub account in order to authorize it to clone the repo in behalf of you

you just need to go to your GitHub account and add the encrypted SSH key then you have the permission to clone the repo

No alt text provided for this image

After that we need the SSH URL of the repo to clone it we will clone the repo at current directory in Ubuntu server, run the following command

git clone git@github.com:dockersamples/example-voting-app.git        
No alt text provided for this image

After cloning the repo we can confirm if it was cloned by running ls in the current directory you should find the directory example-voting-app

No alt text provided for this image

  • After that we need to change our directory to the location of docker compose file for deploying Voting App containers

run this command to change directory to example-voting-app

cd example-voting-app/        

when you view the files on the current directory using ls you should find docker-compose.images.yml file

No alt text provided for this image

  • One last step we should confirm that the defined services on docker-compose.images.yml point to our docker service IP address on the EC2 Ubuntu server instead of host.docker.internal which is the local Docker desktop service for running containers

First we can get the IP address of the docker service when we connect to the EC2 instance via SSH

No alt text provided for this image

you should find the address on IPv4 address for docker0: 172.17.0.1

if the docker compose file services doesn't point to host.docker.internal then we are ready to start deploying process of docker-compose on EC2 Ubuntu server

  • First we should give docker socket permission on Ubuntu server using the following command

sudo chmod 777 /var/run/docker.sock        

then we are good to go for running our voting compose application

run the compose file in detach mode

docker-compose -f docker-compose.images.yml up -d        
No alt text provided for this image

this will run all voting app container services on our EC2 Ubuntu server

we can confirmed that they are running successfully using the command

docker ps
docker-compose ps        
No alt text provided for this image

  • Finally we need to request the public IP address of EC2 Ubuntu server instance before that we should edit security group of our instance to allow HTTP requests to our web server from EC2 dashboard select your instance go to security tab after that click on the security group then click on edit inbound rules and add the rule that allow TCP traffic to 5000 and 5001 ports from anywhere and save it

No alt text provided for this image

Finally copy the public IP address of your instance and put it in your browser add the port of voting app after the IP address https://3.28.57.37:5000 and the?results?will be at?https://3.28.57.37:5001

No alt text provided for this image

We are done :) Voting App is running successfully on our AWS EC2 instance

No alt text provided for this image
No alt text provided for this image

Follow me on Linkedin Mohammad Oghli for more interesting Articles!

Created by Mohamad Oghli

Serhii Illarionov

???? Frontend Engineer

5 个月

Thanks! That was very helpful!

赞
回复

Thank you for this article! I'm fascinated by your job!

赞
回复
Hasnain Askari

—Full Stack Software Developer. HTML, CSS, JavaScript, MERN, React, Next.js

1 å¹´

I was stuck on the "docker compose up" command, which was giving me an error. However, after running the command "sudo chmod 777 /var/run/docker.sock" and "docker-compose -f docker-compose.yml up -d". I'm able to run Docker Compose on my EC2 instance successfully. Thank you so much!

what about the .env-file?

赞
回复
Volodymyr Shynkar

CEO & Co-founder at AppRecode - Innovator for the Future of Cloud | Product stability and DevOps | AWS Well-Architected FTR | Kubernetes & Infrastructure as Code

1 å¹´

Impressive work, Mohammad! Your demonstration of AWS, Python, and Docker integration is top-notch. It's evident that you have a deep understanding of these technologies and how they can be leveraged effectively. Keep up the fantastic job!

要查看或添加评论,请登录

Mohammad Oghli的更多文章

社区洞察

其他会员也浏览了