Using the EC2 container service

We just went over creating a Docker image for our application. We saw how easy and fast it is to start a container using Docker. This is a very transformative experience compared to using only virtual machine technologies, such as EC2.

One possibility that we haven't explicitly mentioned so far is that you can start multiple containers with the same image.

We can, for example, start our helloworld container five times, binding five different ports using the following command (adapt the ID based on the image ID you built. If needed, run docker images to find its ID):

$ for p in {3001..3005}; do docker run -d -p ${p}:3000 e7deb47c0528; done
32631a70b37ab827de39d57fd1d415339202779cf8e16963791980dc9212680a
ad69359b9630446700cbf515c67bd8d5e9de461542de7f6f0614045f36be427f
fad5b94bb1d10a31304348b086a95e0bb82d2ee944b3f75ce64b06af0ccf2353
cd0819ce968c497f9002b47a503f315d8494cb615e88b31029504bf6bd4f2630
0b552a8707313f7294ddfe1b4de7b4320a8efb4885697d466491f426c9743b67

We can validate that everything is working using ps and curl:

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0b552a870731 e7deb47c0528 "node helloworld.js" 6 seconds ago Up 5 seconds 0.0.0.0:3005->3000/tcp hungry_easley
cd0819ce968c e7deb47c0528 "node helloworld.js" 7 seconds ago Up 6 seconds 0.0.0.0:3004->3000/tcp nifty_meitner
fad5b94bb1d1 e7deb47c0528 "node helloworld.js" 8 seconds ago Up 7 seconds 0.0.0.0:3003->3000/tcp agitated_khorana
ad69359b9630 e7deb47c0528 "node helloworld.js" 8 seconds ago Up 8 seconds 0.0.0.0:3002->3000/tcp practical_poincare
32631a70b37a e7deb47c0528 "node helloworld.js" 9 seconds ago Up 8 seconds 0.0.0.0:3001->3000/tcp affectionate_pare
$ curl localhost:3005
Hello World
Cleaning up containers
We can clean up everything by stopping and removing all those containers with these two handy one-line commands:
$ docker stop $(docker ps -a -q)
$ docker system prune

This ability to start multiple containers on a single host with almost no overhead or latency makes Docker an ideal candidate for production.

In addition, more and more companies are deciding to take the service-oriented architecture approach to an all new level by breaking out each business function into a separate service. This is often called using a microservices approach. Docker is a natural fit for microservices and for managing micro service architecture. It provides a platform that is language agnostic (you can start any type of application written in any language inside your container), the ability to scale horizontally and vertically with ease, and a common story around deployment as we deploy containers instead of a variety of services.

We will implement our container architecture using the infrastructure as code best practices, and use CloudFormation through the intermediary of Troposphere.

The first service we are going to look at is AWS's EC2 Container Registry (ECR).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset