Deploying Docker Compose to AWS
local graph
Chapter 11 — Deploying Docker Compose to AWS
Section titled “Chapter 11 — Deploying Docker Compose to AWS”In the previous chapter, we deployed our application to a Docker host. The feature of deploying to a remote host could help in many ways, for example, we could share the application with another individual or use the remote host for development and testing purposes. Deploying to a remote host brings us closer to the context of deploying to production. However, a deployment to a remote host is not up to the standards of a production deployment. A production deployment needs our application to be highly available, secure, and accessible through a load balancer, and the logs of the application need to be easy accessible and securely stored.
This chapter is all about bringing our Docker Compose application to production. Elastic Container Service (ECS) is one of the container orchestration services that AWS provides. ECS is integrated with Docker Compose, therefore by using an existing Compose application we can have a cloud-native application deployed through ECS.
We will start by pushing our Docker images to the AWS Elastic Container Registry (ECR). Then, we shall apply some minor adjustments to an existing Compose application to use the images, from the registry provisioned, and deploy to ECS using a Docker profile for AWS. Once we have deployed our application, we shall proceed to more advanced concepts, such as using an existing cluster in a private network, as well as scaling and secret management.
Here are the topics we will cover in this chapter:
- Introduction to AWS ECS
- Hosting your Docker images on AWS ECR
- Deploying your application to an ECS cluster
- Adapting Compose files for ECS deployment
- Running your Compose application to existing infrastructure
- Advanced Docker Compose concepts on ECS
Technical requirements
Section titled “Technical requirements”The code for this book is hosted on the GitHub repository at https://github.com/PacktPublishing/A-Developer-s-Essential-Guide-to-Docker-Compose. If there is an update to the code, it will be updated in the GitHub repository.
In order to provision AWS resources through the command line, it is essential to install the AWS CLI tool at https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html. The AWS CLI tool can help us with administrative tasks as well as troubleshooting issues on our AWS infrastructure.
In this chapter, Terraform plans will be used. To install Terraform on your system, follow the instructions from the official documentation (https://learn.hashicorp.com/tutorials/terraform/install-cli).
Introduction to AWS ECS
Section titled “Introduction to AWS ECS”ECS is a container orchestration engine that is provided by AWS. We can use ECS in order to deploy, manage, and scale our container applications. Since it is provided by AWS, it is integrated with the rest of the AWS platform. By deploying an application on ECS it will use an elastic load balancer to expose an application; it will use EC2 instances to run the application, and the application will reside on an AWS Virtual Private Cloud (VPC) and its subnets. The logs of the applications will also be accessible through CloudWatch.
ECS comes with the option of AWS Fargate. AWS is a serverless compute option that enables you to deploy your Docker workloads without needing to manage EC2 instances and autoscaling groups. If the application’s workloads are small, require low overhead, and have non-frequent bursts of requests and usage, then Fargate is a solution that our application can benefit from. We will choose Fargate for our application, since our application is still in the prototype phase.
For applications with more demanding workloads, or workloads that have certain needs, we can back ECS with EC2 and autoscaling groups. For example, if an application is CPU intensive, it makes sense to create an autoscaling group that uses a compute-optimized EC2 type.
Working locally with containers is easy, and concerns such as image distribution might not be present in the beginning. However, to use a container that we created, it is essential to be able to distribute the image of that container. Instead of creating and provisioning a registry, AWS provides us with a container registry. In the next section, we shall push our images to an AWS-hosted container registry.
Hosting your Docker images on AWS ECR
Section titled “Hosting your Docker images on AWS ECR”Previously, we have been building images for Compose applications that would later be stored and retrieved from the Docker host. To use the Docker images on ECS, a Docker Image registry is essential. The registry to use on AWS is ECR. By using ECR, we push and pull the images from the registry and use them on any workstation or server that has Docker installed, provided we have configured access to that registry.
ECR is a fully managed container registry. It is a highly available solution backed by AWS Simple Storage Service (S3), thus images are stored across multiple systems. Also, it has features such as scanning the images for vulnerabilities. By using a managed container registry, the maintenance overhead is reduced; for example, there is no need to provision a server or plan the storage capacity.
Another benefit of ECR is how well integrated it is with the rest of the AWS services. An ECS server or a Lambda function, provided they have the right AWS IAM permissions, can pull images seamlessly without any extra configuration.
Since ECR is going to be the Docker registry that we shall use, let’s proceed and provision our registry.
Provision ECR using AWS CLI
Section titled “Provision ECR using AWS CLI”With the AWS CLI, provisioning an ECR is a one-liner:
$ aws ecr create-repository --repository-name developer-guide-to-compose-ecr{ "repository": {… " "repository"ri": "111111111111.dkr.ecr.eu-west-1.amazonaws.com/ developer-guide-to-compose-e"r ",... " "imageScanningConfigurat"on": { " "scanOnP"sh": false }, " "encryptionConfigurat"on": { " "encryptionT"pe": "AES"56" } }}We can get some valuable information from the output. For example, we shall use repositoryUri to tag our images, imageScanningConfiguration scans the images for vulnerabilities when enabled, and encryptionConfiguration is how the images we push are encrypted at rest. repositoryUri has a number prefix. In the preceding example, it is 111111111111. This should be your AWS account number, therefore it will be different whenever another AWS account is being used.
Since we managed to create the registry from the command line, we can also try and create the registry using Terraform.
Be aware that you might want to delete the registry first if you want to create it again:
$ aws ecr delete-repository --repository-name developer-guide-to-compose-ecrOnward! We shall provision the ECR we need using Terraform.
Provision ECR using Terraform
Section titled “Provision ECR using Terraform”In the previous chapter, Terraform helped us to provision the infrastructure without much effort. We can use Terraform to create the Docker registry.
This is how we can define the ECR registry:
resource "aws_ecr_repository" "developer_guide_to_compose_ecr" { name = "developer-guide-to-compose-ecr" image_tag_mutability = "MUTABLE"
image_scanning_configuration { scan_on_push = true }}We can now initialize the Terraform plan:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 terraform initAnd then proceed to execute the Terraform plan:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 terraform applyThese commands will handle the Terraform state locally. However, for an application that is deployed publicly, and various engineers interact with it or create infrastructure for it, we want a better way of storing and sharing the state.
Storing a Terraform state file
Section titled “Storing a Terraform state file”When applying the Terraform plan commands for testing purposes, storing the state locally can be an option. However, for production use, the state should reside in the form of storage that can be accessible by other individuals. In our case, we shall store the Terraform state in an S3 bucket.
Before creating a bucket on AWS, we need to be aware of the bucket naming rules on AWS. A bucket name should be unique across all AWS accounts. This means that if an AWS account has created a bucket with a name you want to use, it will not be possible, and another name should be used.
Let’s create the bucket:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 aws s3api create-bucket --bucket developer-guide-to-compose-state --region eu-west-1 --create-bucket-configuration LocationConstraint=eu-west-1$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 aws s3api put-bucket-versioning --bucket developer-guide-to-compose-state --versioning-configuration Status=EnabledSince the bucket is created, we can configure Terraform to use it to store the state:
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 4.16" } } backend "s3" { bucket = "developer-guide-to-compose-state" region = "eu-west-1" key = "terraform.tfstate" }
required_version = ">= 1.2.0"}
provider "aws" { region = "eu-west-1"}Since this bucket has already been created on S3, the name is reserved. When you apply the script, another bucket name should be chosen before you create your bucket.
Before running terraform apply, it is worth making sure that the changes that will take place are the ones we want. We can use terraform plan, which helps us to preview the changes we will apply to our infrastructure:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 terraform planNow that we are confident with the changes that will take place, we shall apply the Terraform plan:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 terraform applyAlso, you can benefit from enabling versioning on the bucket. Apart from the Git commit history, versioning can be another way to have a history of the state changes.
Since the container registry has been created, we can proceed and push the images to the registry.
Pushing images to ECR
Section titled “Pushing images to ECR”Before interacting with a registry, we need to set up authentication.
To authenticate to ECR, we shall use the AWS CLI:
aws ecr get-login-password --region eu-west-1|docker login --username AWS --password-stdin 111111111111.dkr.ecr.eu-west-1.amazonaws.comThis will generate a token that will be used by the Docker client to communicate with the registry. As we can see in the first section of the command, the token is generated and in the second section after the pipe, the output of the command is used to log in to the registry we created previously.
We can now test that it works and push an image to the registry:
$ docker tag nginx 111111111111.dkr.ecr.eu-west-1.amazonaws.com/developer-guide-to-compose-ecr:ngnix$ docker push 111111111111.dkr.ecr.eu-west-1.amazonaws.com/developer-guide-to-compose-ecr:ngnixf2089ca22bc1: Pushed9e13ccef5ed0: Pushed9dfe3def52f1: Pushed7b11943dbe46: Pushed80730baf8465: Pushed5978b6b69f17: Pushedngnix: digest: sha256:ec2290b7c5d15abb4b3384ad66a89e9c523a4668c057898f3114fa6 1df4a5586 size: 1570We tagged the Nginx image and pushed it to the registry. By using the registry, we can share our images with a container orchestration engine such as ECS.
Adapting the Compose application images
Section titled “Adapting the Compose application images”The application to use would be the Task Manager we created in Chapter 5, Connecting Microservices. We will modify the Compose file to push the images to ECR.
We should change the images on each service:
services: location-service:... image: 111111111111.dkr.ecr.eu-west-1.amazonaws.com/developer-guide-to-compose-ecr:location-service_0.1... event-service: image: 111111111111.dkr.ecr.eu-west-1.amazonaws.com/developer-guide-to-compose-ecr:events-service_0.1... task-manager:... image: 111111111111.dkr.ecr.eu-west-1.amazonaws.com/developer-guide-to-compose-ecr:task-manager_0.1...The images we created previously are dependent on the platform our workstation is running on. ECS can support various platforms. In our case, in order to simplify the deployment, we shall pick the linux/amd64 platform.
Let’s specify the platform to use before building:
services: location-service: platform: linux/amd64 ... event-service: platform: linux/amd64 ... task-manager: platform: linux/amd64...We can now build and push the images using Compose:
$ docker compose build --no-cache$ docker compose pushWe used the –-no-cache option. This is essential to make sure that the images that are built are going to be built on the platform we specified. Also, it will not use any cached images from our previous builds. By using compose push, all our images will be pushed to the ECR registry.
We can now proceed and deploy the application to an ECS cluster.
Deploying your application to an ECS cluster
Section titled “Deploying your application to an ECS cluster”Deploying to ECS is seamless; however, we need to create a profile on Docker that will use AWS credentials to be able to interact with AWS and provision resources.
Therefore, we need to create a Docker Context that’s specific to AWS ECS scenarios:
$ docker context create ecs guide-to-compose? Create a Docker context using: [Use arrows to move, type to filter]> An existing AWS profile AWS secret and token credentials AWS environment variables? Select AWS Profile [Use arrows to move, type to filter] default> guide-to-docker-composeSuccessfully created ecs context "guide-to-compose"By using the credentials, the context will try to provision infrastructure or evaluate if infrastructure already exists for the Compose application. Thus, it is important for the user/role behind the AWS profile to have sufficient permissions for those actions.
Behind the scenes, Compose will generate and apply a CloudFormation template, provisioning the necessary infrastructure to facilitate running the application.
We can check the CloudFormation template, since we have export functionality in Compose, by using the convert command:
$ docker --context=guide-to-compose compose convertAWSTemplateFormatVersion: 2010-09-09Resources: CloudMap: Properties: Description: Service Map for Docker Compose project aws Name: aws.local Vpc: vpc-8efaaceb...CloudFormation is an infrastructure as code solution similar to Terraform.
An example file can be found on https://github.com/PacktPublishing/A-Developer-s-Essential-Guide-to-Docker-Compose/tree/main/Chapter11/compose-cloudformation.yaml.
By applying a CloudFormation template, newly defined infrastructure will be created, or existing infrastructure will be deleted or updated, depending on the changes. Essentially, based on the services and the various elements defined through the Compose application, a CloudFormation file will be generated that in turn will be applied to the existing stack created previously.
Be aware that in the previous command, we specified context. As we saw in Chapter 10, Deploying Docker Compose Using Remote Hosts, we can set context permanently. This is done for convenience purposes since we will not do as many administrative actions; instead, we shall focus on deployments.
We are now ready to deploy the Compose application:
$ docker --context=guide-to-compose compose -f compose.backup.yaml up LocationserviceService CreateComplete 56.0s EventserviceService CreateComplete 76.1s TaskmanagerTCP8080Listener CreateComplete 2.0s TaskmanagerService CreateComplete 55.1s...This will create the Compose application and the infrastructure.
We can also go to CloudFormation and check the progress of the application (https://eu-west-2.console.aws.amazon.com/cloudformation/home?rfilteringText=&viewNested=true&hideStacks=false#/stacks?filteringStatus=active&filteringText=&viewNested=true&hideStacks=false).
We can see the progress of our application while it’s being created:

Figure 11.1 – CloudFormation progress
As we can see, the name of the stack is aws. This is because it uses the same name that a Compose deployment will use; therefore, in our case it is the name of the directory we are currently located in.
We can change this by using the project name flag:
$ docker --context=guide-to-compose compose -p guidetocompose upOnce the application has been deployed, we can check the running containers by using docker compose ps:
$ docker --context=guide-to-compose compose psNAME COMMAND SERVICE STATUS PORTStask/aws/337ff364e53e4a59b14bdaf65e4fe655 "" redis Runningtask/aws/6f08bff5e5ee42a0b2d9995124debd8c "" location-service Runningtask/aws/bf76ab48000d49b38017e6b3b4d6b073 "" event-service Runningtask/aws/cea75425182c4e08ad571efcce0c82f2 "" task-manager Running aws-LoadBal-1Q44XRW9WNYF5-5fdfe7e98727ac85.elb.eu-west-1.amazonaws.com:8080:8080->8080/tcpAs we can see, the Task Manager application that we exposed the port of is running and is accessible through a DNS entry.
Let’s issue a test request:
$ curl aws-LoadBal-1Q44XRW9WNYF5-5fdfe7e98727ac85.elb.eu-west-1.amazonaws.com:8080/pingpongSince we have achieved our goal, we will proceed with deleting the infrastructure:
$ docker --context=guide-to-compose compose -f compose.backup.yaml downWe did manage to deploy our Compose application to AWS ECS. We did not have to provision any infrastructure since it was created by Compose using the credentials we configured through the context.
This can be a way to deploy our application; however, it forces us to create a new cluster for each application. This causes various billing concerns, and it is not the optimum way to run applications since it can lead to a maintenance overhead. In the next section, we will deploy Compose to an existing ECS cluster.
Running your Compose application to an existing cluster
Section titled “Running your Compose application to an existing cluster”Previously, we managed to run a Compose application on ECS by using an ECS Docker context. By deploying the application, a new infrastructure was provisioned through CloudFormation and an entire new ECS cluster was created for the application.
If we take our time and check the CloudFormation file, we can see that various AWS components have been created:
- A VPC and its subnets
- A CloudWatch log group
- Security groups
- A load balancer
- CloudMap for service discovery
- An ECS cluster
- ECS tasks
By default, CloudFormation will use the default VPC and subnets that already exist in our AWS account. The load balancer, security groups, and CloudMap, which assist with service discovery, will have to be created, as well as the ECS cluster and the ECS tasks. Those applications will be deployed to AWS Fargate.
It is obvious that these resources are provisioned and we have no control over their settings. There might be a business use case where we would like to have a private network. Also, we might want more strict egress and ingress rules.
Compose provides the option to deploy the application to an existing cluster and use resources that we have already provisioned.
In the following subsections, we will create an ECS cluster and then make adjustments to our existing application so it will use the existing infrastructure.
We will be using Terraform to provision the infrastructure.
Creating a log group
Section titled “Creating a log group”We want to keep our logs for our application in one Cloudwatch group. Therefore, we will create a log group and a log stream dedicated to our application:
resource "aws_cloudwatch_log_group" "task_api" { name = "/ecs/task-api"}resource "aws_cloudwatch_log_stream" "cb_log_stream" { name = "tasl-log-stream" log_group_name = aws_cloudwatch_log_group.task_api.name}Let’s now move on to a very important aspect, which is networking.
Creating a private network
Section titled “Creating a private network”We will create a private network that will span across the specified availability zones, therefore creating a subnet per availability zone.
The Terraform plan for the VPC:
resource "aws_vpc" "compose_vpc" { cidr_block = "172.17.0.0/16" enable_dns_hostnames = true enable_dns_support = true}As we can see, we have created the VPC and specified a cidr_block value. Also, we enabled DNS hostnames and DNS support. This is crucial for our ECS application since communication between services requires this option to be enabled.
Let’s create a subnet per availability zone:
resource "aws_subnet" "private_subnet" { count = length(var.availability_zones) cidr_block = cidrsubnet(aws_vpc.compose_vpc.cidr_block, 8, count.index) availability_zone = var.availability_zones[count.index] vpc_id = aws_vpc.compose_vpc.id}resource "aws_subnet" "public_subnet" { count = length(var.availability_zones) cidr_block = cidrsubnet(aws_vpc.compose_vpc.cidr_block, 8, length(var.availability_zones) + count.index) availability_zone = var.availability_zones[count.index] vpc_id = aws_vpc.compose_vpc.id}The region is specified when we run the terraform command. In a file containing the variables, we can have the chosen availability zones:
variable "availability_zones" { type = list(string) default = [ "eu-west-1a" ,"eu-west-1b" ]}Our application might need to have access to the internet. For example, we use the Redis image, which we did not deploy on the ECR registry we created. To enable access to the internet, we need an internet gateway, and we need to specify a route table that will route traffic to the internet gateway:
resource "aws_internet_gateway" "internet_gateway" { vpc_id = aws_vpc.compose_vpc.id}resource "aws_route" "internet_route" { route_table_id = aws_vpc.compose_vpc.main_route_table_id destination_cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.internet_gateway.id}This will work for the instances deployed to the public subnet; however, it won’t work for instances that reside on the private subnet. To enable connectivity to the internet, we should use a NAT gateway.
A NAT gateway needs a public IP to operate, thus we create two for each availability zone:
resource "aws_eip" "nat_ips" { count = length(var.availability_zones) vpc = true depends_on = [aws_internet_gateway.internet_gateway]}A NAT gateway will be set for each public subnet:
resource "aws_nat_gateway" "nat_gateway" { count = length(var.availability_zones) subnet_id = element(aws_subnet.public_subnet.*.id, count.index) allocation_id = element(aws_eip.nat_ips.*.id, count.index)}resource "aws_route_table" "private_route_table" { count = length(var.availability_zones) vpc_id = aws_vpc.compose_vpc.id
route { cidr_block = "0.0.0.0/0" nat_gateway_id = element(aws_nat_gateway.nat_gateway.*.id, count.index) }}Finally, we define a route table association for the private subnets:
resource "aws_route_table_association" "private_association" { count = length(var.availability_zones) subnet_id = element(aws_subnet.private_subnet.*.id, count.index) route_table_id = element(aws_route_table.private_route_table.*.id, count.index)}This is the most important part as it enables us to use a private network on ECS and have network connectivity.
Security groups
Section titled “Security groups”Security groups allow ingress and egress traffic. The full security group configuration can be found on GitHub. An important parameter is to enable connectivity between two Compose services.
To enable ingress between Compose services, use this code:
resource "aws_security_group_rule" "allow_services_connectivity" { type = "ingress" from_port = 0 to_port = 0 protocol = "-1" source_security_group_id = aws_security_group.compose_security_group.id security_group_id = aws_security_group.compose_security_group.id}This way, Compose services will be able to communicate with each other.
Configuring the ECS cluster and the load balancer
Section titled “Configuring the ECS cluster and the load balancer”Since networking ingress and egress rules are in place, we can now configure the load balancer and the ECS cluster:
resource "aws_alb" "compose_alb" { name = "guide-to-compose-load-balancer" subnets = aws_subnet.public_subnet.*.id security_groups = [aws_security_group.lb.id]}resource "aws_ecs_cluster" "compose_ecs" { name = "guide-to-compose-ecs"}Updating the Compose file
Section titled “Updating the Compose file”Let’s adapt the Compose configuration so it can use the existing cluster, VPC, and load balancer:
x-aws-vpc: "vpc-0144f03210f0da8e5"x-aws-cluster: "guide-to-compose-ecs"x-aws-loadbalancer: "guide-to-compose-load-balancer"
services: location-service: ... logging: options: awslogs-group: "/ecs/task-api" redis: ... logging: options: awslogs-group: "/ecs/task-api" task-manager: ... logging: options: awslogs-group: "/ecs/task-api" links: - "redis:redis"...As we can see, we specified the VPC in which our workloads will be located, the ECS cluster that will orchestrate our application’s containers, and the load balancer that will serve as the entry point to our application. We are now ready to deploy the application to the recently provisioned cluster.
Running your Compose application on existing infrastructure
Section titled “Running your Compose application on existing infrastructure”We have provisioned the infrastructure, and we have adjusted our Compose application so that it will use the existing infrastructure. The command to provision our infrastructure will be the same as before, and the Docker profile used will be the one we used previously.
Run the application:$ docker --context=guide-to-compose compose -f ./compose.aws.yaml -p guidetocompose up –dCheck the application containers:
$ docker --context=guide-to-compose compose -f ./compose.aws.yaml -p guidetocompose pstask/guide-to-compose-ecs/13ddf3ab03f146cc95b59005c308c5ad "" redis Runningtask/guide-to-compose-ecs/af645c4f87ad4a9f8d3aac5fd313bdc5 "" location-service Runningtask/guide-to-compose-ecs/d96338ba685a401394ba9d61b80238e6 "" task-manager Running guide-to-compose-load-balancer-1956561308.eu-west-1.elb.amazonaws.com:80:80->80/http$ curl guide-to-compose-load-balancer-1956561308.eu-west-1.elb.amazonaws.com:80/pingWe did it! Our application is now running on a private network using the ECS cluster we defined using Terraform.
When you don’t need the infrastructure anymore, don’t forget to decommission the ECS cluster and the other resources provisioned since it will lead to extra costs.
You can do this using terraform destroy:
$ AWS_ACCESS_KEY_ID=*** AWS_SECRET_ACCESS_KEY=*** AWS_DEFAULT_REGION=eu-west-1 terraform destroyOr you can do it manually by visiting the AWS console.
Since we took more advantage of AWS components, we can also explore more advanced concepts of a Compose application deployed on ECS. Let’s move on to updating and scaling our application, as well as configuring secrets.
Advanced Docker Compose concepts on ECS
Section titled “Advanced Docker Compose concepts on ECS”We have managed to deploy our Compose application to a dedicated VPC and a dedicated ECS cluster. This gives us more control over our application and the resources we use. Building a cloud-native application comes with various benefits. We can have a rolling application update without any downtime, we can tune and scale an application based on its requirements, and we can manage and share secrets efficiently.
Updating the application
Section titled “Updating the application”The docker compose up command is sufficient to update the application. Hence, the same command that is used to spin up the services will be used to update them.
Since Compose is backed by CloudFormation, the update will take place, but it can also cause downtime if a certain component of the infrastructure is removed and recreated. Rolling updates need to be configured. By using a rolling update, the container instances of a service are updated incrementally. This ensures that the application can still service requests while the containers are being updated with the latest version one at a time. We should dive deeper into scaling an application and configuring a rolling update.
Scaling the application
Section titled “Scaling the application”Since the application is deployed on an orchestration engine, we can scale up the replicas of a service. We will have two replicas of each service, except for Redis, and will configure a rolling update.
Let’s put scaling instructions on all services:
deploy: mode: replicated replicas: 2 update_config: parallelism: 1 delay: 10s order: start-firstBy increasing the replicas, the service will have two instances and ECS will handle the internal load balancing. Also, by using update-config, we ensure we update one service at a time.
By using compose ps, we can see an increase in the number of running Docker containers:
$ docker --context=guide-to-compose compose -f ./compose.aws.yaml -p guidetocompose psNAME COMMAND SERVICE STATUS PORTStask/guide-to-compose-ecs/505c7b2c962642459a8b156c4c069f73 "" redis Runningtask/guide-to-compose-ecs/50c74375216842dba62fea2c19772e55 "" location-service Runningtask/guide-to-compose-ecs/8b84d785dee14441bb4a3b1808b02419 "" event-service Runningtask/guide-to-compose-ecs/ac84fe91790244d1836238a40890c54d "" event-service Runningtask/guide-to-compose-ecs/b42f9c9b18704dfb888f97112b343e0b "" task-manager Running guide-to-compose-load-balancer-1757643662.eu-west-1.elb.amazonaws.com:80:80->80/httptask/guide-to-compose-ecs/bcce928092294fc1bcc57875954307bc "" task-manager Pending guide-to-compose-load-balancer-1757643662.eu-west-1.elb.amazonaws.com:80:80->80/httptask/guide-to-compose-ecs/cfd3370bfb61467e9ec15a2a1fc0763c "" location-service RunningInstead of manually increasing the number of replicas, autoscaling can be configured:
deploy: mode: replicated replicas: 2 update_config: parallelism: 1 delay: 10s order: start-first x-aws-autoscaling: min: 2 max: 3 cpu: 75We did it! We have a minimum of 2 replicas that will scale up to 3 if over 75% of the allocated CPU is utilized.
Using secrets
Section titled “Using secrets”Secrets are a big part of our application. Access to a database or cloud resources needs to be configured properly. Compose gives us the option to create secrets and mount them to containers. We will mount a secret to the Task Manager:
secrets: - secret-file command: - /bin/sh - -c - | ls /run/secrets/secret-file /task_managernetworks: location-network: redis-network:secrets: secret-file: file: ./secret.file.txtWe also changed command of the container, before we run the task-manager application, we ls the secret-file, thus we justify that the secret is indeed mounted and resides on the container. By default, secrets are mounted on /run/secrets/secret-name.
If we check the console, the file path is printed:
/run/secrets/secret-file[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.In a production environment, secrets need to be handled very carefully. A local file mounted as a secret might need to be encrypted. KMS can be used for encrypting secrets and decrypting them once they’ve been deployed during initialization. For example, sops (https://github.com/mozilla/sops) is a tool that can be used for securely storing secrets on a repository using KMS.
Summary
Section titled “Summary”In this chapter, we managed to deploy our Compose application to an AWS environment. We created a container registry on AWS and pushed the Docker images to the registry. We then deployed our application to ECS and a new set of infrastructure was provisioned for our application. Then we created a private network and an ECS cluster. Our Compose application has the security benefits of a private network and our infrastructure is reusable for other Compose applications. This was achieved by adapting our Compose file and specifying the infrastructure to be used. We moved on to more advanced deployment concepts, such as autoscaling and storing secrets. By adapting the Compose application, we took advantage of ECS’ autoscaling capabilities and the rolling update functionality, and we shared our secrets among multiple applications.
In the next chapter, we shall deploy our Compose application to another popular cloud provider, Microsoft Azure.