Welcome to part 2 of my blog post on getting started with AWS Containers (read part 1 here). In part 2, You will learn about the following concepts. Setting up AWS for Containers, Building, and Packaging Containerized Applications, Setting Up Docker on AWS, Deploying ECS containers. Not to worry, if these terms are new to you — you'll learn about them here!
You will learn about the following topics in this second part of the blog post.
Creating an AWS account
Configuring AWS Identity and Access Management (IAM) roles and policies
Creating a VPC with Public and Private Subnets for Your Clusters.
To follow this tutorial, It will be great if you read part 1 here.
Watch this YouTube video created by Cloud Tech on how to create an AWS account :
Watch this YouTube video created by Inquirinity on how to configure IAM for an AWS account :
You can also visit the Amazon Webservice documentation website to learn all about IAM on AWS
Container instances in your clusters need external network access to communicate with the Amazon ECS service endpoint. However, you might have tasks and services that you would like to run in private subnets. Creating a VPC with both public and private subnets provides you the flexibility to launch tasks and services in either a public or private subnet. Tasks and services in the private subnets can access the internet through a NAT gateway. Services in both the public and private subnets can be configured to use a load balancer so that they can still be reached from the public internet.
This tutorial guides you through creating a VPC with two public subnets and two private subnets, which are provided with internet access through a NAT gateway.
A NAT gateway requires an Elastic IP address in your public subnet, but the VPC wizard does not create one for you. Create the Elastic IP address before running the VPC wizard.
To create an Elastic IP address, open the Amazon VPC console or visit this URL https://console.aws.amazon.com/vpc/ and sign in. Follow the below steps:
In the left navigation pane, choose Elastic IPs.
Choose Allocate new address, Allocate, Close.
The Allocation ID for your newly created Elastic IP address; you will enter this later in the VPC wizard.
The VPC wizard automatically creates and configures most of your VPC resources for you.
To run the VPC wizard, in the left navigation pane, choose VPC Dashboard.
Choose Launch VPC Wizard, VPC with Public and Private Subnets, Select.
For VPC name, give your VPC a unique name.
For Elastic IP Allocation ID, choose the ID of the Elastic IP address that you created earlier.
Choose Create VPC.
When the wizard is finished, choose OK.
The Availability Zone in which your VPC subnets were created. Your additional subnets should be created in a different Availability Zone.
Non-default subnets, such as those created by the VPC wizard, are not auto-assigned public IPv4 addresses. Instances launched in the public subnet must be assigned a public IPv4 address to communicate with the Amazon ECS service endpoint.
To modify your public subnet's IPv4 addressing behavior
The wizard creates a VPC with a single public and a single private subnet in a single Availability Zone. For greater availability, you should create at least one more of each subnet type in a different Availability Zone so that your VPC has both public and private subnets across two Availability Zones.
To create an additional private subnet
To create an additional public subnet
In the left navigation pane, choose Subnets and then Create Subnet.
For Name tag, enter a name for your subnet, such as a Public subnet.
For VPC, choose the VPC that you created earlier.
For Availability Zone, choose the same Availability Zone as the additional private subnet that you created in the previous procedure.
For IPv4 CIDR block, enter a valid CIDR block. For example, the wizard creates CIDR blocks in 10.0.0.0/24 and 10.0.1.0/24 by default. You could use 10.0.2.0/24 for your second public subnet.
Choose Yes, Create.
Select the public subnet that you just created and choose Route Table, Edit.
By default, the main route table is selected. Choose the other available route table so that the 0.0.0.0/0 destination is routed to the internet gateway (igw-xxxxxxxx) and choose Save.
With your second public subnet still selected, choose Subnet Actions, Modify auto-assign IP settings.
Select Enable auto-assign public IPv4 address and choose Save, Close.
Containerization has become a popular approach for packaging and deploying applications. Several containerization technologies are available, including Docker, containerd, and others. In this tutorial, you will focus on Docker as the containerization technology of choice.
Docker has revolutionized the way developers package and deploy applications, offering a lightweight and portable solution for containerization. With Docker, developers can create self-contained units called containers that encapsulate their applications and all their dependencies, including libraries, frameworks, and runtime environments. This approach ensures that applications run consistently across different environments, from development to production, regardless of the underlying infrastructure.
One of the key benefits of Docker is improved application isolation. By leveraging containerization, applications are encapsulated within their own runtime environment, isolated from the host system and other applications. This isolation prevents conflicts between dependencies and ensures that applications operate reliably and predictably.
Furthermore, Docker brings consistency to application deployment. With Docker containers, developers can create a standardized environment, ensuring that applications behave consistently across different development machines, testing environments, and production servers. This eliminates the "works on my machine" problem and facilitates smoother collaboration between teams.
Docker is a popular containerization technology that enables developers to package and deploy applications in lightweight and portable containers. Amazon Web Services (AWS) provides a robust infrastructure for running Docker containers. This guide will walk you through the steps to set up Docker on AWS.
If you don't have an AWS account already, go to the AWS website (https://aws.amazon.com/) and click on Create an AWS Account. Follow the instructions to set up your account. You will need to provide billing information and create a new IAM user with appropriate permissions.
Once you have an AWS account, launch an EC2 instance to host your Docker containers. Follow these steps:
After the EC2 instance is launched, you need to connect to it using SSH. Follow these steps:
ssh -i your-key-pair.pem ec2-user@your-instance-ip
Note: You may need to adjust the SSH command based on the operating system and key pair you used during the EC2 instance setup.
Once connected to the EC2 instance, install Docker by executing the following commands:
sudo yum update -y
sudo amazon-linux-extras install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-user
To confirm that Docker is installed and running correctly on the EC2 instance, run the following command:
docker info
If Docker is installed properly, you will see information about the Docker version and configuration.
Once you have Docker set up on your AWS EC2 instance, you can begin deploying and managing Docker containers. This guide will walk you through the steps to deploy and manage Docker containers on your AWS EC2 instance.
Before deploying a container, ensure you have a Docker image ready for your application. You can either build your own Docker image using a Dockerfile or pull an existing image from a registry like Docker Hub.
If you are using an existing Docker image from a registry, use the docker pull command to fetch the image onto your EC2 instance. For example:
docker pull your-image:tag
If you have a locally-built Docker image or a saved image file, use the docker load command to load the image into Docker on your EC2 instance. For example:
docker load -i your-image.tar
To deploy a Docker container based on your image, use the docker run command. Customize the command based on your application's requirements. Here's an example:
docker run -d --name your-container -p host-port:container-port your-image:tag
Replace your container with a desired name for your container, host port with the port number on your EC2 instance you want to map to the container's port, and container port with the port your application listens on inside the container.
You can manage and monitor your Docker containers on your EC2 instance using various Docker commands. Here are some commonly used commands:
docker ps
: Lists the running containers on the EC2 instance.docker stop container-id
: Stops a running container. Replace container-id with the actual container ID or name.docker start container-id
: Starts a stopped container.docker logs container-id
: Displays the logs of a specific container.docker exec -it container-id /bin/bash
: Accesses the terminal inside a running container for executing commands or debugging.
To keep your EC2 instance clean and efficient, it's essential to remove unused containers. Use the docker rm command followed by the container ID or name to remove a stopped container. For example:
docker rm container-id
Be cautious while removing containers as it permanently deletes the container and its associated data.
To streamline container deployment and management, consider using container orchestration tools like Amazon Elastic Container Service (ECS) or Kubernetes on AWS. These tools provide more advanced features, such as automatic scaling, load balancing, and improved container management capabilities.
However, if you like this article and want to encourage me to write more articles, you can donate to this ETH, USDC and USDT Address on the Ethereum network: 0xfd446002b2979559ef4Cd06e5DCC84bbCC6b49D3
Feel free to visit the official Amazon Container Documentation Website and explore further resources from Amazon.