Amazon EC2 Container Service and Elastic Beanstalk: Docker on AWS

Technology keeps moving. In just a few years, we’ve gone from servers running on dedicated hardware, through virtualization, and then cloud computing. And now we’ve reached the container age. As we will see, the Amazon EC2 Container Service (ECS) has made containers a major element of their deployment family. We’ll soon discuss three ways to run Docker containers on AWS, but first a few words about why you would want to.

Traditional servers living in datacenters could do one thing at a time. There would often have been one server for Active Directory, another for Exchange, and a third running a database. But it was common for any or all of these servers to have unused excess processing capacity going to waste. Virtualization was developed to make use of that extra capacity. Through virtualization, a single physical server could host multiple virtual servers, allowing for far greater scalability and more efficient resource utilization.

But even virtualization required significant up-front hardware (and software license) investments and couldn’t completely remove the risks of excess capacity. That’s where cloud computing – with its multi-tenancy models, on-demand pricing, and dynamic scalability – proved so powerful.

But modern DevOps practices demand the ability to quickly build servers and ship code to be run in different environments. Welcome to the world of containers: extremely lightweight, abstracted user space instances that can be easily launched on any compatible server and reliably provide a predictable experience. Containers – of which the best-known example is Docker – are able to achieve all this by capitalizing on the ability to share the Linux kernel of any host.

Docker

The DevOps development process depends, in large part, on portability. Even a slight difference between your development, test, and production environments may completely break your application. Traditional development models follow a change management process to solve these kind of the problems. But this process won’t fit in today’s rapid build and deploy cycles. Docker streamlines and automates the software development process. You can simply package an application with references to all its dependencies into standardized containers (defined by plain-text Dockerfiles) that include everything needed to run your application wherever you end up shipping it.

Docker uses a client-server architecture. The docker client talks to a host daemon which both builds and then runs the container.

In this post we won’t talk all that much about running Docker natively (that’s been discussed elsewhere). Instead, we will concentrate on AWS deployments. We’ll explore three ways to do that:

  1. Deploying Docker containers directly from an Ec2 instance.
  2. Using Docker containers on Elastic Beanstalk.
  3. Docker cluster management using the AWS EC2 Container Service.

Deploying Docker containers on an Ec2 instance

Docker can run anywhere, on a racked server, an old laptop, and perhaps, if you worked at it hard enough, even on a smart phone. So, since it requires only the Linux kernel to run, there’s definitely no reason why it shouldn’t work on a Linux-powered Amazon EC2 instance. Let’s see how we can install and run Docker on Amazon Linux.

    1. Launch an Amazon Linux Instance.
    2. Install the docker engine.
      sudo yum install -y docker
    3. SSH into your EC2 instance and start the Docker service.
      sudo service docker start
    4. Sign up for a Docker Hub Account.
    5. Search for Ubuntu OS in Docker Hub.
      sudo docker search ubuntu
    6. Download the ubuntu image.
      sudo docker pull
    7. start the container. The -t and -i flags allocate a pseudo-tty and keep stdin open.
      sudo docker run -t -i ubuntu /bin/bash
    8. At the regular BASH prompt (not in the Docker shell), install the Apache webserver.
      apt-get update && apt-get install apache2
    9. Save the container. We need to commit these changes to our container using the container ID and the image name.
      docker commit a2d424f5655ea nitheesh86/apache
    10. Push the changes to docker hub.
      docker push nitheesh86/apache

At this point, sharing the container with others is easy. Just run this command on any other server, anywhere on the Internet:

docker run -d -p 80:80 nitheesh86/apache /usr/sbin/apache2ctl -D FOREGROUND

Using Docker containers on Elastic Beanstalk

Elastic Beanstalk is an AWS PAAS platform. All you have to do is upload your application code, and Elastic Beanstalk takes care of the deployment, load balancing, and capacity provisioning. With a single click, you can start all the necessary application servers running. There is no charge for the Elastic Beanstalk service itself, just for the AWS resources you actually use.

EC2 Container Service

Since AWS integrated Docker into Elastic Beanstalk, you can build and test your application on your local workstation, and then directly deploy it on Elastic Beanstalk. As always, you install all the required software and dependencies through the Dockerfile. This file will include the image to be used (Ubuntu, CentOS, etc), and the volume to be mapped. Any new EC2 instance Elastic Beanstalk will launch to run your application will be configured based on the commands passed by your Dockerfile.

Let’s deploy a sample application on a Docker container

  1. Create a Dockerrun.aws.json file to deploy an existing Docker image. This file describes how to deploy a Docker container as an Elastic Beanstalk application. Your dockerrun.aws.json file should look something like this:

    EC2 Container ServiceLet me explain this, line by line.

    • AWSEBDockerrunVersion – Specifies the version number as the value “1” for Single Container Docker environments.
    • Image – Specifies the Docker base image on an existing Docker repository from which you’re building your Docker container.
    • Ports – Lists the ports to expose on the Docker container.
    • Volumes – Maps volumes from an EC2 instance to your Docker container.
    • Logging – Maps the log directory inside the container.
  2. Create a zip file including Dockerfile or Dockerrun.aws.json and any application file, then upload to Elastic Beanstalk.
  3. From there  you can follow this guide deploy your application.

Docker cluster management using the EC2 Container service (ECS)

When your container grows from one to many, maintenance can become a significant burden. Managing containers at scale requires proper cluster management software. Since “Amazon” and “scale” are like two sides of the same coin, it wasn’t much of a surprise when they created the AWS EC2 Container Service to handle the installation, operation, scaling and general cluster management for you.

Container instances communicate with the ECS service through an “ECS agent.” When you create a new cluster, an Amazon ECS-enabled AMI will be copied into a your cluster.

EC2 Container Service Benefits

  • Simplified Cluster Management
    ECS creates and manages clusters of Docker Containers. It can scale from one to thousands of containers, spread across multiple Availability Zones.
  • Easy Scheduling
    ECS supports its own scheduler to spread containers out across your cluster, to balance availability and utilization.
  • Portability
    ECS uses the standard Docker daemon so that you can  easily move your on-premises application to the AWS cloud and vice versa.
  • Resource Utilization
    A containerized application can make very efficient use of resources. You can choose to run multiple containers on the same EC2 instance.
  • Integrated with AWS services
    You can access most AWS features, such as Elastic IP addresses VPC, ELB, and EBS.

Defining Amazon EC2 Container Service Building Blocks 

EC2 Container Service
  • Cluster
    A group of EC2 instances managed by ECS that lives inside your VPC. One cluster can contain multiple instance types and sizes and can be stretched across multiple Availability Zones.
  • Scheduler
    An integral part of each cluster. The scheduler is responsible for assigning containers to instances.
  • Container
    The virtual machines that execute your application. You can run any number of containers in a single cluster.
  • Task Definition
    Blue prints for your application that define the way your containers will work.
  • ECS AMI
    An Amazon Machine Image that includes the ECS Agent.

Launch a simple EC2 Container Service container

Create a cluster
Create task definitions. To get used to the process the first time through, I would suggest you stick with the sample task definition that’s provided.
EC2 Container ServiceCreate a task definition from  the sample. You have option of modifying the parameters (CPU resources or change the port mappings) in the task definition.

EC2 Container Service
EC2 Container Service

You can schedule a task to run once (which is ideal for batch jobs) or create a service to launch and maintain a specified number of copies of the task definition in your cluster. So that the sample application we are running will run continuously, let’s choose “Create a Service.”
EC2 Container Service
Here’s the final step to launch our containers. The more instances you have in our cluster, the more tasks we can give them. Click Review, and Launch.
EC2 Container Service
Click on your cluster and at the bottom you’ll see an external link for your application.
EC2 Container Service
When you click on that link you should be able to see your sample application running in the browser.
EC2 Container Service

Written by

My professional IT career began nine years back when I was just out of my college. I worked with a great team as an infrastructure management engineer, managing hundreds of enterprise application servers. I found my passion when I got the opportunity to work with Cloud technologies: I'm addicted to AWS Cloud Services, DevOps engineering, and all the cloud tools and technologies that make engineers' lives easier. Currently, I am working as a Solution Architect in SixNines IT. We are an experienced team of engineers that have helped hundreds of customers move to the cloud responsibly. I have achieved 5 AWS certifications, happily helping fellow engineers across the globe through my blogs and answering questions in various forums.

Related Posts

— November 28, 2018

Two New EC2 Instance Types Announced at AWS re:Invent 2018 – Monday Night Live

Let’s look at what benefits these two new EC2 instance types offer and how these two new instances could be of benefit to you. Both of the new instance types are built on the AWS Nitro System. The AWS Nitro System improves the performance of processing in virtualized environments by...

Read more
  • AWS
  • EC2
  • re:Invent 2018
— November 21, 2018

Google Cloud Certification: Preparation and Prerequisites

Google Cloud Platform (GCP) has evolved from being a niche player to a serious competitor to Amazon Web Services and Microsoft Azure. In 2018, research firm Gartner placed Google in the Leaders quadrant in its Magic Quadrant for Cloud Infrastructure as a Service for the first time. In t...

Read more
  • AWS
  • Azure
  • Google Cloud
Khash Nakhostin
— November 13, 2018

Understanding AWS VPC Egress Filtering Methods

Security in AWS is governed by a shared responsibility model where both vendor and subscriber have various operational responsibilities. AWS assumes responsibility for the underlying infrastructure, hardware, virtualization layer, facilities, and staff while the subscriber organization ...

Read more
  • Aviatrix
  • AWS
  • VPC
— November 10, 2018

S3 FTP: Build a Reliable and Inexpensive FTP Server Using Amazon’s S3

Is it possible to create an S3 FTP file backup/transfer solution, minimizing associated file storage and capacity planning administration headache?FTP (File Transfer Protocol) is a fast and convenient way to transfer large files over the Internet. You might, at some point, have conf...

Read more
  • Amazon S3
  • AWS
— October 18, 2018

Microservices Architecture: Advantages and Drawbacks

Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs).Microservices have become increasingly popular over the past few years. The modular architectural style,...

Read more
  • AWS
  • Microservices
— October 2, 2018

What Are Best Practices for Tagging AWS Resources?

There are many use cases for tags, but what are the best practices for tagging AWS resources? In order for your organization to effectively manage resources (and your monthly AWS bill), you need to implement and adopt a thoughtful tagging strategy that makes sense for your business. The...

Read more
  • AWS
  • cost optimization
— September 26, 2018

How to Optimize Amazon S3 Performance

Amazon S3 is the most common storage options for many organizations, being object storage it is used for a wide variety of data types, from the smallest objects to huge datasets. All in all, Amazon S3 is a great service to store a wide scope of data types in a highly available and resil...

Read more
  • Amazon S3
  • AWS
— September 18, 2018

How to Optimize Cloud Costs with Spot Instances: New on Cloud Academy

One of the main promises of cloud computing is access to nearly endless capacity. However, it doesn’t come cheap. With the introduction of Spot Instances for Amazon Web Services’ Elastic Compute Cloud (AWS EC2) in 2009, spot instances have been a way for major cloud providers to sell sp...

Read more
  • AWS
  • Azure
  • Google Cloud
— August 23, 2018

What are the Benefits of Machine Learning in the Cloud?

A Comparison of Machine Learning Services on AWS, Azure, and Google CloudArtificial intelligence and machine learning are steadily making their way into enterprise applications in areas such as customer support, fraud detection, and business intelligence. There is every reason to beli...

Read more
  • AWS
  • Azure
  • Google Cloud
  • Machine Learning
— August 17, 2018

How to Use AWS CLI

The AWS Command Line Interface (CLI) is for managing your AWS services from a terminal session on your own client, allowing you to control and configure multiple AWS services.So you’ve been using AWS for awhile and finally feel comfortable clicking your way through all the services....

Read more
  • AWS
Albert Qian
— August 9, 2018

AWS Summit Chicago: New AWS Features Announced

Thousands of cloud practitioners descended on Chicago’s McCormick Place West last week to hear the latest updates around Amazon Web Services (AWS). While a typical hot and humid summer made its presence known outside, attendees inside basked in the comfort of air conditioning to hone th...

Read more
  • AWS
  • AWS Summits
— August 8, 2018

From Monolith to Serverless – The Evolving Cloudscape of Compute

Containers can help fragment monoliths into logical, easier to use workloads. The AWS Summit New York was held on July 17 and Cloud Academy sponsored my trip to the event. As someone who covers enterprise cloud technologies and services, the recent Amazon Web Services event was an insig...

Read more
  • AWS
  • AWS Summits
  • Containers
  • DevOps
  • serverless