Running Dockerized Applications On AWSPrasad JoshiBlockedUnblockFollowFollowingJan 23OverviewThere is a lot of content and resources available to get started with using Docker on AWS.
However it is also very easy to get lost in using the plethora of tools and techniques that are available.
This tutorial provides very basic step-by-step information and will help you get started with running Dockerized Application (Java) on your workstation(local) and on AWS (cloud).
Basic ConceptsVirtualizationThe concept of running one or more virtual instances of an operating system/s on a physical server is called Virtualization.
This basically lets us create multiple server environments on top of one physical server.
This way we can drive more power from the host server and easily create or destroy virtual machines (VM).
This concept of virtualization can be extended to other components of computing such as storage devices, network interfaces etc.
Some of the popular Virtualization platforms are VMWare & Xen.
ContainerizationThis involves using a specialized software to create one or more instances of an operating-system on top of a physical or virtual host.
The container is not a full operating system, but rather a collection of files, env variables and libraries required to run an application.
The container is lightweight and uses a lot of shared resources from the underlying OS.
So although this is similar to VMs, the Containers are lot faster to startup and consume far less resources compared to a VM.
Popular containerization platforms are Docker & rkt.
Docker EngineDocker installation includes a collection of tools to help with creating and managing Containers.
One of the core components of Docker is the Docker Engine.
(If you are familiar with VMWare ESX, think of the Engine as the ESXi Server).
This runs as a daemon process on the host (physical or virtual) and is responsible for images, containers, networks and volumes.
This also provides a REST API.
Clients use the CLI to interact with the daemon process to orchestrate the creation and management of Containers.
Images & ContainersImage is an immutable file that contains a list of everything that needs to run inside the container.
This is designed to be composed using layers (Each layer is a reusable part that can be referenced by multiple images).
The idea of using layers, makes the downloading of images more efficient, as only the layers that are not available need to be downloaded and the existing ones can just be reused.
A "Container" is an instantiation of the "Image".
We can have multiple instances of the same image, that can be run simultaneously.
RepositoryThis is a specialized storage service for saving all the Docker Images.
Think of this as a typical package repository like Nexus etc.
Having a central repository makes it easy to launch Containers from the same Image on multiple hosts.
The most popular repository is Docker Hub.
The repositories can be public or private.
Scheduling/Orchestration FrameworksAs we get deeper into using containers to run applications, we will soon run into one of following issues:How do we run multiple instances of the container ?How do we scale the number of containers up or down to match the demand ?How do we make sure at least one instance is always running ?How do we make sure that the containers are evenly spread across a pool of hosts?Orchestration Frameworks solve these problems and provide additional services to help with the management of the Containers.
There are multiple frameworks (all evolving very fast), to solve these problems.
Some of the popular ones areDocker Swarm ModeKubernetesAWS EKSAWS ECS (with EC2 or Fargate)We will come back to using the Orchestration Frameworks in a later tutorial.
For now let us get started with using Docker.
Installing Docker CE on Windows WorkstationThe term "Docker" is used to refer to a set of tools that help with creating and managing Containers.
As we discussed earlier, Docker Engine is one of the core modules.
Along with this, the Docker installation includes the Docker CLI, Docker Client etc.
Docker is available in two flavors, Docker CE (Community Edition) & Docker EE (Enterprise Edition).
Community Edition is ideal for local development.
It could include some experimental features and hence not intended for Production usage.
The Enterprise Edition should be used for all Production deployments.
Please follow the following official guide to install Docker CE on Windows.
com/docker-for-windows/install/Similar instructions are available for installation on Mac and Linux.
On Windows and Mac OS, Docker runs inside a light-weight VM.
After installation is complete, validate using the following command.
This will give you the version of Docker Engine and Client.
docker versionRun an Java/Tomcat application using Docker on your workstation.
Note: Docker started as a project on the Linux OS.
Windows Containers are now available and the technology is catching-up fast.
However we will be using Linux based Images for all of our examples.
Java/Tomcat ApplicationYou can download a very simple WAR file from the following (file is called “sample.
war”:Download Sample Tomcat ApplicationCreate a DockerfileDockerfile is used to create a Docker Image.
It consists of a set of instructions regarding what is packaged inside a Docker Image.
It also defines the executable that needs to be launched when we create a Container from the Image.
To make it easier to create Images, we can reference an already created Image as a starting point.
Here is our Dockerfile.
The comments in the code describe the purpose of each line.
# Pull base image.
We reference a pre-built image available on DockerHubFROM tomcat:8-jre8 # Maintainer for this Image MAINTAINER "prasad.
com" # Copy to war file to tomcat path ADD sample.
war /usr/local/tomcat/webapps/ # Typically you would add custom configuration for # the tomcat server using the following files.
Skipping this for simplicity# ADD settings.
xml /usr/local/tomcat/conf/# ADD tomcat-users.
xml /usr/local/tomcat/conf/Create a new directory and copy the Dockerfile and the sample.
war (downloaded earlier) to this directory.
Navigate inside the directory using a terminal or command window.
We build an image from a Dockerfile using the following commands.
(“tag-name” is a name of your choice.
The “tag-name” can be used to reference the image while launching a container)docker build -t <tag-name> .
And we create a Container (instance of the Image) using the following command:# docker run -d -p <host-port>:<container-port> <tag-name>docker run -d -p 8888:8080 <tag-name>We are using a simple port mapping here.
This basically means that the port 8888 on the host is mapped to port 8080 of the Container.
Validate the sample app by navigating to the following URL using your web-browser:http://localhost:8888/sampleThis will bring up the sample app.
Congratulations !!.You have successfully run a Java app inside a docker container.
As you probably observed, we never installed Java or Tomcat on the server.
All the necessary software is packaged inside the docker Image.
Using a Registry/RepositoryRun the following command :docker imagesYou will see a list of Docker Images.
You will notice an image with the <tag-name> that you provided earlier.
This is how the Docker Engine is managing the images locally.
What would you do if you have to run a container on a different host ?.You could build an image on that host.
But you don’t have to.
This is where the power of repository comes into play.
You could save your image to a repository and then create Containers by pulling in the Image from the repository.
DockerHub is the most popular repository for storing Docker Images.
However since we are in the AWS ecosystem, I would recommend using ECR (Elastic Container Repository) – which is a managed repository offering from AWS.
This is a private registry and you can acquire the login credentials using the following command (assuming you have AWS CLI installed and configured with credentials locally)aws ecr get-login –no-include-emailThe output of this command will look something like this :docker login -u AWS -p <password> https://<aws_account_id>.
comRun this command to login to the Repository.
After you have logged in, you can use the following commands to push (save) Images to the Registry.
First we create a new Tag on the image using the following:docker tag <local-image-name> <repo-url>/myappE.
replace the image name and repo-url as followsdocker tag myapp <aws_account_id>.
com/myappThen use the following to push the image to the repo:docker push <aws_account_id>.
com/myappYou can now create a Container using the following (this is similar to the ‘run’ command we used earlier.
This just references the Image from the repository:docker run -d -p 8888:8080 <aws_account_id>.
com/myapRunning the Dockerized Application on AWS EC2Now let us see what is involved in running this application on an EC2 instance.
First step would be to create a new EC2 instance (or use an existing Linux instance).
Windows instance could also work, but a Linux instance would be much easier.
If you have private connectivity to your VPC (using VPN or AWS DirectConnect) use a private-IP for the instance or else you can create the instance with a public-IP.
In either case, please make sure the security-group allows inbound TCP traffic on port 8888 from your workstation.
Note: Preferably create an instance using AWS Linux AMI (this will ensure aws cli is already installed)Install Docker CE on the EC2 host.
Login to your repository.
(generate a login command as described earlier using the CLI)Run a container by referencing the Image.
docker run -d -p 8888:8080 <aws_account_id>.
com/myappdocker run -d -p 8888:8080 <aws_account_id>.
com/myappVerify by using the following URL in a browserhttp://<private-ip>:8888/sample (Over VPN) OR http://<public-ip>:8888/sampleNote: The Security Group for the EC2 instance should allow inbound traffic on port 8888.
Congratulations !!.You are now running a Tomcat App inside a Docker Container on AWS EC2.
Plan for Orchestration FrameworksAs we learnt in the beginning of this article, an orchestration-framework will help with various concerns related to running containers e.
load-balancing, scaling, scheduling a service etc.
A lot of orchestration frameworks are available, and in the AWS ecosystem I highly recommend using ECS (Elastic Container Service).
In the next part, we will learn about the various frameworks (EKS, ECS etc) and deep-dive into using AWS ECS (with Fargate)References:AWS ECRhttps://docs.
htmlDocker Commands Referencehttps://docs.
com/engine/reference/commandline/docker/.. More details