Skip to main content

[[TOC]]

Demo: Creating container of cats Docker image


Prerequisites:


  1. EC2 Amazon Linux instance - t2.micro 8gib storage with open http/ssh ports in the security group
  2. DockerHub account

Getting Started


  1. Run sudo amazon-linux-extras install docker to install the docker container engine onto your EC2 instance.
  2. Start the service by running sudo service docker start
  3. If we try to run docker ps now, it will fail as we don't have permissions
  4. Run sudo usermod -a -G docker ec2-user and exit
  5. Log back into the instance and you'll be able to run docker ps
  6. Install Git - sudo yum install git
  7. Clone down the repository into this folder by running git clone https://github.com/Dupo24/ecscontainerdemo1.git
  8. cd into this directory and then build the image by running docker build -t soloscat .
    image.png
  9. Run the container: docker run -t -i -p 80:80 soloscat
  10. Navigate to the IP address and you should see the page below:
    image.png
  11. It's a pretty simple version of what could be a production app, I just wanted to create a simple website dedicated to my little coding buddy, Rolex, who passed away here in Oct 2021 in the middle of my AWS learning experience.

Uploading your image to DockerHub


  1. In your ec2 instance, log into your dockerhub account using docker login --username yourusername
  2. You will be prompted for your password here.
  3. Run docker images
  4. Copy the image ID of the specific image tag that you want to upload
  5. Run docker tag 4a52d70afb12 solostroup/soloscat replacing the number with your ID and the solostroup with your dockerhub ID.
  6. Push the image up to your DockerHub image repository by running docker push solostroup/soloscat:latest

Clean up


Terminate the EC2 instance Delete the image from your dockerhub repository if you wish.

Demo: Deploying this container image using Fargate


Basically we're just going to take the container that we have create above into AWS Fargate.

Create the Fargate cluster


  1. Navigate to the ECS console
  2. Click on Clusters and then Create Cluster
  3. Select the Networking Only cluster.
  4. Name the cluster SolosCat
  5. Leave the Create VPC box unchecked and click Create.
    a. if you get an error here about some services not linking, just go back and start over.
    image.png
  6. Browse through the options for this Fargate cluster if you wish

Create Task Definition to run image


  1. Click Task Definitions and click Create New Task Definition
  2. Select the Fargate definition.
  3. Name the task definition "SolosCat"
  4. Select 1gb and 0.5 CPU
  5. Click Add Container
  6. Name this one SolosCatWeb (as in the web component of what could be a full stack)
  7. in the image field, add the link to your repository in the format dockerio/dockerid/imagename. Example: docker.io/solostroup/soloscat
  8. Set the soft memory limit to 1gb (1024mb)
  9. Select port 80 (http port) for the web app
  10. Browse through the rest of the options if you wish and then click Add at the bottom
  11. Browse through the options and the click Create
    image.png

Viewing the Task Definition


  1. Click View Task Definition or go back to the ECS console and select Task Definitions and then your task.
  2. You can view the raw JSON of the task before you can launch.

Creating Container on your Fargate Cluster


  1. Click on Clusters
    image.png
  2. Click on your cluster that you created
    image.png
  3. Click Run New Task
    image.png
  4. Click FARGATE as the launch type
  5. Make sure the Task Definition shows SolosCat and the Cluster is your cluster
  6. Scroll down to Cluster VPC and select the default VPC.
  7. On the subnets, click at least two just to simulate putting your cluster into two availability zones.
  8. On the Security Groups, you can edit the security groups to allow other ports. Think about how a microservices architecture looks and how they talk to each of the microservices over other ports.
  9. Scroll to the bottom and click Run Task.
  10. Refresh until it says RUNNING in green
    image.png
  11. Click on the task and then copy the IP address
  12. Navigate to this IP address in a browser and you should have your website that you created.

Pricing


image.png

Clean Up

Stop Container Deregister the task definition Clusters and then delete the cluster