How to Deploy a Multi Container Docker Compose Application On Amazon EC2
Tags: aws, tutorial, cloud, docker
Published: 2023-03-16 01:24:32

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/how-to-deploy-a-multi-container-docker-compose-application-on-amazon-ec2/).

Container technology streamlined how you’d build, test, and deploy software from local environments, to the cloud or on-premise data centers. But with the benefit of building applications with container technology, there was the problem of manually starting and stopping each container while building multi-container applications.

To solve this problem, Docker Inc created [Docker Compose](https://docs.docker.com/compose/). You can use Docker Compose to simplify the running of multi-container applications with as little as two commands; `docker-compose up` and `docker-compose down`. 

In this article, you will learn 

- What Amazon EC2 is, 
- How to create and connect to an Amazon EC2 instance, and 
- How to deploy a Docker compose application on EC2

## Prerequisites

To follow along in this article, you must have the following:

- Basic knowledge of the terminal.
- Some experience with Docker and Docker Compose.
- An AWS account — Learn how to create one [here](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).

## What is Amazon EC2?

Amazon Elastic Compute Cloud (EC2) provides secure, resizable, and scalable computing capacity for virtually any workload in [Amazon Web Services (AWS) Cloud](https://aws.amazon.com/). By using Amazon EC2, you can eliminate the overhead of investing in hardware up front so you can focus on developing and deploying your applications faster. 

With Amazon EC2, you can launch as many or as few virtual servers (instances) as you need, configure security and networking, and manage storage. Also, Amazon EC2 enables you to easily scale your applications up or down to handle changes in requirements or spikes in popularity, reducing your need to forecast traffic.

To learn more about Amazon EC2, see the [Amazon EC2 product page](https://aws.amazon.com/ec2). 

## Creating and connecting to an Amazon EC2 instance 

After creating an AWS account, you create and connect to an Amazon EC2 instance with the following steps:

- Select your desired region for the instance
- Navigate to the EC2 console
- Launch the instance 
- Connect to the instance from your local computer via SSH

### Selecting your desired region to deploy
To select a region on your AWS console, click the dropdown as annotated on the top right corner of the image below and select your desired region. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662391449294_annotely_image+57.png)


You may ask, “How would I know which region is best for my application?” To know the best region to deploy your application check out this article on “[What to Consider when Selecting a Region for your Workloads](https://aws.amazon.com/blogs/architecture/what-to-consider-when-selecting-a-region-for-your-workloads/).”

### Navigating to the EC2 console
After selecting your desired AWS region, to go to the EC2 Console, search for “EC2”  in the search box as in the image below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662403475117_Screenshot+2022-09-05+at+19.44.30.png)


Once you arrive on the EC2 console, locate the **Launch Instance** button and click on it to start a new EC2 instance launch flow. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662494043346_annotely_image+60.png)


### Launch the instance
In the EC2 instance launch flow, you will be required to configure your instance with the following:


1. **Application and OS Images (Amazon Machine Image):** An Amazon Machine Image **(**AMI) is a template that contains the software configuration (operating system, application server, and applications) required to launch your instance.


    This article will use a Linux Ubuntu Server 22.04 with the configurations seen in the image below.
![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662545028543_Screenshot+2022-09-07+at+11.03.41.png)

2. **Instance type:** An ****Instance type is a combination of CPU, memory, storage, and networking capacity. There are various EC2 instance types to give you the flexibility to choose the appropriate mix of resources for your applications.


    This article will use a **t2.micro** with the CPU and memory seen in the image below. To learn more about Amazon EC2 instance types, check out this [documentation](https://aws.amazon.com/ec2/instance-types/).


![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662545989993_Screenshot+2022-09-07+at+11.19.46.png)



3. **Key pair (login):** You can use a key pair to securely connect to your instance remotely from your local computer. To create a key pair, click on **Create new key pair** as seen in the image below.
![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662546287879_annotely_image+62.png)


After clicking **Create new key pair**, you will see a pop up. In the popup, name your pair and leave the rest of the configurations as default (if your local computer is Windows OS, select `.ppk` private key file format), then click **Create key pair.** 

After clicking **Create key pair**, a `.pem` private key file will be automatically downloaded on your computer; store the private key in a secure and accessible location on your computer. **You will need it later to connect to your instance.** 

To learn more about Amazon EC2 key pairs, check out this [documentation](https://docs.aws.amazon.com/console/ec2/key-pairs/create). 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662546731883_Screenshot+2022-09-07+at+11.32.08.png)



4. **Network settings:** You can leave the **Network settings** as default. The default network setting as in the image below uses:
    - The default [VPC and Subnet](https://docs.aws.amazon.com/vpc/latest/userguide/how-it-works.html) of your AWS account, and enables auto-assigning a public address to your instance.
    - It also creates a new security group, a set of firewall rules that control the traffic for your instance. The default is the allow SSH traffic from anywhere; this works for this article, but in production use cases, you should set more strict rules. 


![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662548020906_Screenshot+2022-09-07+at+11.53.36.png)

5. **Configure storage:** You can leave the storage configuration as default for this article.
![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662548938892_Screenshot+2022-09-07+at+12.08.54.png)


The last configurations are the advanced settings, you can ignore them as they are out of scope of this article. To learn more about them click the **Info** text as seen in the image below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662549075859_annotely_image+63.png)


After all the above configurations, click on **Launch instance** and wait a few mins for your instance to launch as in the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662549262912_annotely_image+64.png)


Then click on **View all Instances** as in the above image and select the instance as in the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662549384724_annotely_image+65.png)


### Connect to your Amazon EC2 instance
With your EC2 instance selected, at the top as annotated in the image below, click **Connect.** 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662550265611_annotely_image+66.png)


After clicking **Connect** you will see a page with options to connect to your instance. This article will us the SSH client option.

Follow the steps you see on the page as in the image below using the `.pem` private key that was downloaded upon creation of you key pair. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662550594579_Screenshot+2022-09-07+at+12.36.31.png)


After following the above steps, you should be connected to your instance like in the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662550856297_Screenshot+2022-09-07+at+12.40.52.png)


Next, you will deploy a demo multi container Docker Compose application that has a React frontend, Node.js backend and MongoDB database containers, and make it accessible on the internet. 

## Deploying a multi container Docker Compose application on Amazon EC2

Before you can deploy the Docker Compose application, you need to:

- First install Docker Engine and Docker compose on your EC2 instance.
- Then for this article clone the demo Todo Docker Compose application from Github on your instance 
- And then deploy the application

**Installing Docker Engine on an Ubuntu EC2 instance**
The quickest way to install the Docker engine is using the `get-docker.sh` bash script on [get.docker.com](https://get.docker.com/). Run the command below to download the script on your EC2 instance.


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

After running the above command confirm that it downloaded the `get-docker.sh` like the image below.  

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662552572763_Screenshot+2022-09-07+at+13.09.28.png)


To run the script, run the following command:


    $ sh get-docker.sh

And after a while Docker will be installed on the Ubuntu EC2 instance. To confirm the installation run `$ docker` `--``version` and you should see the Docker version as in the image below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662553100698_Screenshot+2022-09-07+at+13.18.17.png)


If you used a different AMI, check out the [Docker installation documentation](https://docs.docker.com/get-docker/) to learn how to install on your choice AMI. 

**Installing Docker Compose on an Ubuntu EC2 instance**
Now to install Docker Compose, run the following command to get the release from Github:\


    $ 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

And then the following command to make it executable:

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

With that done, when you run `$ docker-compose -v`, you should see the version you just installed as in the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662553600416_Screenshot+2022-09-07+at+13.26.37.png)


**Cloning the demo Docker Compose application**
The multi container todo application you will deploy is one of the [Awesome Docker Compose Github repository samples](https://github.com/docker/awesome-compose/tree/master/react-express-mongodb).

To access the application, clone the repository on your instance with the following command:


    $ git clone https://github.com/docker/awesome-compose.git

After cloning the project, run the following command to change into the `react-express-mongodb` directory where the todo application is:


    $ cd awesome-compose/react-express-mongodb


![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662574731889_annotely_image+68.png)


Once in the project directory, to start the containers using `compose.yaml` file as annotated above run:


    $ docker compose up -d

And after Docker pulls the images and creates the network for the containers to communicate with each other, you will see an output like in the image below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662569831336_Screenshot+2022-09-07+at+17.57.04.png)


To confirm that the 3 containers are running, run `$ docker ps` and you should see an output similar to the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662569888329_Screenshot+2022-09-07+at+17.58.03.png)


Next, you will test the deployment to be sure the todo application works.

## Testing your deployment

The application you just deployed talks on port `3000` but if you visit port `3000` with the public address of your EC2 instance, you won’t be able to view it. This is because of the security group settings which currently only allows inbound traffic from SSH. 

To enable viewing your application, on the EC2 instance console, with your EC2 instance selected, move to the **Security** tab and then click on the security group as in the image below. 

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662584229391_annotely_image+69.png)


After clicking on the security group, in the next menu, click on **Edit Inbound rules** to add the rule that allows inbound traffic through port `3000`.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662584672391_annotely_image+70.png)


And then, add the rule as seen in the screenshot below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662585149010_annotely_image+71.png)


With that done, when you visit the `your_instance_public_ip:3000` you should see the Todo application as in the image below.

![](https://paper-attachments.dropbox.com/s_1701B796B04D6A41B10A33A9310053D92BBACF6EF3B2C91B9CE84D4E683E62A0_1662570033409_Screenshot+2022-09-07+at+18.00.28.png)

## Conclusion

In this article, you learned what Amazon EC2 is, how to create and connect to an Amazon EC2 instance, and how to deploy a Docker compose application on EC2. 

There is so much more to learn about Amazon EC2. To learn more, check out the following resources:

- [AWS training website](https://aws.amazon.com/training/) 
- [Amazon EC2 courses on Pluralsight](https://www.pluralsight.com/browse/cloud-computing/AWS/global-infrastructure/aws-ec2?aid=7010a000002LUv2AAG&promo=&utm_source=non_branded&utm_medium=digital_paid_search_google&utm_campaign=XYZ_EMEA_Dynamic&utm_content=&cq_cmp=1576650371&gclid=Cj0KCQjwguGYBhDRARIsAHgRm48kbwhFfG_wO2zFQtl9a1bjz2Eg3GTNXojxsLT8XJaxFGDuB-VnLcYaAjGuEALw_wcB)
- [7 Best AWS EC2 [Amazon Elastic Compute Cloud] Online Courses for Beginners in 2022](https://medium.com/javarevisited/7-best-aws-ec2-amazon-elastic-compute-cloud-online-courses-for-beginners-in-2021-f7a1a55ea719)