How to Deploy a Multi Container Docker Compose Application On Amazon EC2

This article was originally posted on Everything DevOps.

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. 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.

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. 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.

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.

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.”

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.

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

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.

  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.

  1. 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.

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.

  1. 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 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.

  1. Configure storage: You can leave the storage configuration as default for this article.

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.

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

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

Connect to your Amazon EC2 instance

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

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.

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

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. 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.

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.

If you used a different AMI, check out the Docker installation documentation 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 "[github.com/docker/compose/releases/download.. -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.

Cloning the demo Docker Compose application The multi container todo application you will deploy is one of the Awesome Docker Compose Github repository samples.

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

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.

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

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.

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.

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

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

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: