Skip to main content

Deploying Docker Containers on Amazon EC2 Instances

Hands-On Lab

 

Photo of Craig Arcuri

Craig Arcuri

AWS Training Architect II in Content

Length

00:30:00

Difficulty

Advanced

In this hands-on lab, we will see how to deploy Docker containers on EC2 Instances.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Deploying Docker Containers on EC2 Instances

Introduction

In this hands-on lab, the objective will be learning how to deploy Docker containers on EC2 Instances.

Log in to the AWS live environment via the cloud_user credentials provided. We must also ensure that we are working in the us-east-1 (N. Virginia) region.

Create an EC2 Instance

  1. From the AWS Management Console dashboard, navigate in to EC2.
  2. Click Launch Instance.
  3. Select an Amazon Linux AMI (first in the list).
  4. Select t2.micro.
  5. Click Next: Configure Instance Details.
  6. Navigate to Auto-assign Public IP, and select Enable from the dropdown menu.
  7. Click Review and Launch.
  8. Click Launch.
  9. Enter a key pair name, and click Download Key Pair.
  10. Click Launch Instances.

Create an Inbound Rule for HTTP

  1. Go to the EC2 dashboard.
  2. Navigate to Security groups and select the group launch-wizard-1.
  3. Click the Inbound tab, then Edit.
  4. Click Add Rule and select HTTP from the dropdown menu.
  5. Click Save.
  6. Navigate back to the instance, click Services, and then EC2.
  7. We can run the following to apply all updates:

    sudo yum update

Install Docker

  1. From the EC2 Management Console, click on the EC2 instance.

  2. Go to the Security Group for that instance and add an inbound rule on Port 80 for HTTP.

  3. Log in via SSH to the EC2 instance.

  4. Click Connect at top of the console page.

  5. From the Connect to Your Instance window copy/paste the connection information for our EC2 instance.

  6. Enter yes to continue when prompted.

  7. On the EC2 Management Console, select the EC2 instance's radio button.

  8. From the Description tab, next to Securtiy groups, click launch-wizard-1.

  9. Click the Inbound tab.

  10. Click *Edit**.

  11. Click Add Rule.

  12. In the Type dropdown, click HTTP.

  13. Click Save.

  14. On the console page click Services > EC2 to go back to our instance.

  15. Run the following command to apply updates:

    sudo yum update
  16. Install Docker with the command:

    sudo yum install -y docker`

    Note: The installation should state: Complete!

  17. Start the Docker service:

    sudo service docker start

Add the EC2 User to Docker

  1. While still logged in via SSH into the instance, run the command:
    sudo usermod -aG docker ec2-user

Deploy Docker

  1. While still logged in via SSH to the EC2 instance, verify that a successful installation of Docker has occurred with:

    sudo docker ps
  2. Ensure that the docker ps command is recognized, then proceed with running the following to view more information:

    sudo docker info

    Note: We may see a value of 0 for the fields: Containers, Running, Paused, and Images. However, the version, driver type etc., should be present beneath this with more insight on Docker's installation setup.

  3. Let's deploy our Docker container now, using:

    sudo docker run -d -p 80:80 --name nginx nginx

    Note: The container name and image are both set to nginx. An "Unable to find image" message is normal and that will also indicate the deployment finished. We should see the origin of the pull for the deployment and status messages of Pull complete.

  4. From the instance's Description tab, we can copy the _Public DNS(IPV4).

  5. Add a tab on your browser, then paste the address in and hit enter to browser to it.

  6. We can run the following command again, to check whether the container is up and running:

    sudo docker ps

    Furthermore, we can check Docker's current status with:

    sudo systemctl status docker

    The status should display as active(running).

  7. Lastly, we can try launching another container for additional practice too, by running the following:

    sudo docker run hello-world

    Note: Once again, we will see an "Unable to find image" message, but should also see that it states "your installation appears to be working correctly" also.

  8. Run the following to view more information and see our two containers:

    sudo docker info
  9. We can go to an interactive container, with:

    sudo docker run -it ubuntu bash

    Note: We should now see a new command-line prompt because we are inside the container.

  10. To view contents further, use the ls command.

  11. Run the following to update the Unbuntu Docker container:

    apt update
  12. Now exit the container.

  13. Ensure that we can see all three containers now, with:

    docker info
  14. Run containers in the background, with --detach=true:

    docker run -it --detach=true ubuntu bash
  15. List the containers to get a closer look inside:

    docker ps
  16. Use the following to rename our container to nginx4docker:

    docker rename nginx nginx4docker
  17. List the containers to verify that the name change was successful:

    docker ps
  18. Use the following command to view more detailed information about our container:

    docker inspect
  19. View network interfaces associated with the Docker container:

    docker network ls
  20. Next, create a network interface:

    docker network create testdocker
  21. Lastly, verify that the new interface exists:

    docker network ls

Conclusion

Congratulations⁠ — you've completed this hands-on lab!