Day 11: Introduction to Docker and Containerization on AWS

Day 11: Introduction to Docker and Containerization on AWS


Welcome to Day 11 of our exciting "30 Days of AWS" journey! If you've been following along from the beginning, kudos to you for diving into the world of Amazon Web Services. Your dedication and curiosity are truly commendable.

For those who might have just joined us or are specifically interested in today's topic, a warm welcome to you as well! While each article in this series delves into a different facet of AWS, rest assured that they are all interconnected, building upon the knowledge we've been cultivating day by day.

If you're here for the first time, I encourage you to take a moment to catch up on our previous discussions. This will enhance your understanding and ensure a seamless flow as we dive deeper into the fascinating journey of AWS together.

In today’s installment, we will explore "Docker and Containerization on AWS." Containers have become a cornerstone of modern application deployment, and understanding Docker will provide a solid foundation for leveraging powerful AWS services like ECS (Elastic Container Service) and ECR (Elastic Container Registry).

As always, feel free to engage, ask questions, and share your thoughts in the comments. Your participation is what makes this series vibrant and valuable. I’m thrilled to have you join us on this journey. Let’s get started!

What is Docker?

Imagine that you’re packing a lunchbox. When you pack a lunchbox, you include everything needed for a perfect meal—rice, curry, a side dish, maybe some pickles, a spoon, and a napkin. When you close that box and hand it over, no matter who opens it or where it’s opened, they get the same meal in perfect condition.

In technical terms, Docker is like that lunchbox, but instead of food, it packages software applications. A Docker container includes everything needed to run an application—code, configurations, and dependencies. Because all of these are packaged together, the application will work the same no matter where it’s run: on your laptop, in a company server, or on AWS.

Key Docker Components (Without the Jargon)

  1. Docker Image: Think of this as a recipe for making your dish (application). It tells Docker exactly what should go inside your lunchbox (container).

  2. Docker Container: The lunchbox itself, which is created based on the recipe.

  3. Dockerfile: The instruction card used to build a Docker Image. It lists out the step-by-step process, like a cooking recipe.

  4. Docker Engine: The chef who uses the recipe (Dockerfile) to make lunchboxes (Docker Containers).

Why Do We Need Docker?

  1. No More "It Works on My Machine" Problems: Ever had that experience where something works perfectly on one computer but not on another? With Docker, that’s solved because it includes everything needed to run, packed neatly inside one container.

  2. Easy to Move Around: Docker containers are portable, meaning you can move them between different machines, servers, or cloud providers (like AWS) with ease.

  3. Saves Resources: Unlike traditional virtual machines, which are like entire homes with their own roofs and walls (operating systems), Docker containers share the same roof but have their own rooms. This makes them use much less space and resources.

How Does Docker Work with AWS?

Now that we understand Docker basics, let’s see how AWS helps us take Docker containers to the next level. AWS has two key services to work with Docker:

  1. Amazon ECS (Elastic Container Service): Manages, scales, and runs Docker containers for you. You don’t need to worry about the servers—just tell ECS what you want to run, and it handles the rest.

  2. Amazon ECR (Elastic Container Registry): This is like a storage room for your Docker recipes (images). It’s a place where you can safely store, organize, and manage all your Docker images.

Let’s start with a simple example where we build a Docker image, store it in ECR, and run it using ECS.

Setting Up Docker on AWS: A Step-by-Step Guide

Step 1: Building a Simple Docker Image

Let’s create a very basic Docker image that prints "Hello, Docker!" to the screen.

  1. Create a New Folder on Your Computer:
    Create a folder called hello-docker:

     mkdir hello-docker
     cd hello-docker
    
  2. Create a File Named Dockerfile:
    In the hello-docker folder, create a new file named Dockerfile (no extension) and add the following content:

     # Use the official Python image as a base
     FROM python:3.8
    
     # Set the working directory
     WORKDIR /app
    
     # Create a small Python script
     COPY <<EOF app.py
     print("Hello, Docker!")
     EOF
    
     # Run the script
     CMD ["python", "app.py"]
    
  3. Build the Docker Image:
    Open your terminal and run the following command:

     docker build -t hello-docker .
    

This command tells Docker to create an image named hello-docker using the instructions in the Dockerfile.

Step 2: Setting Up AWS ECR to Store the Docker Image

  1. Login to Your AWS Account:
    Open the AWS Management Console and go to ECR (Elastic Container Registry).

  2. Create a Repository:
    Click on "Create repository" and name it hello-docker-repo. This will store your Docker image.

  3. Push the Docker Image to ECR:
    Get the login command for ECR and authenticate Docker:

     aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com
    

    Tag your image and push it to ECR:

     docker tag hello-docker:latest <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/hello-docker-repo:latest
     docker push <aws_account_id>.dkr.ecr.us-east-1.amazonaws.com/hello-docker-repo:latest
    

Step 3: Running the Docker Container on ECS

Now that the image is in ECR, let’s deploy it using ECS.

  1. Create an ECS Cluster:
    Go to ECS Console → Click Clusters → Create a new cluster.

  2. Create a Task Definition:
    Create a new task definition, select the Fargate launch type, and specify the ECR image link.

  3. Create a Service:
    Use the task definition to create a service that runs and manages the Docker container.

  4. Deploy and Verify:
    Go to your ECS cluster, and you should see your Docker container running! If you access the task logs, you should see the message:
    "Hello, Docker!"

Summary

Today, we explored:

  • The basics of Docker and why it’s a popular tool in the tech world.

  • How to create a simple Docker container.

  • How to store Docker images in AWS ECR.

  • How to deploy and run Docker containers using ECS.

What’s Next?

In Day 12, we’ll dig deeper into managing Docker containers using Amazon ECS. We’ll explore how to configure ECS to run multiple containers, handle scaling, and monitor container health.

Stay tuned, and let’s keep this AWS learning journey going strong!


Hope you find this blog helpful. Please share your thoughts in the comments—it will help me refine and provide more insightful content. Happy Learning!

Connect with Me - LinkedIn - Twitter/X - Topmate