For the past few years, use of container technology such as Docker has increased drastically within institutions in different sectors. As a result, popular cloud providers such as Amazon and Google have invented advanced tools to support this. Some of them include Amazon Elastic Compute Cloud (EC2), Amazon Elastic Container Service (Amazon ECS), and the EC2 Container Registry (ECR). EC2 is a favorite product that provides scalable virtual servers in the cloud while Amazon ECS is an Amazon Web Service used to run and manage Docker applications on a scalable group of EC2 Instances.
In this article, we'll look at how to deploy a Docker container on a single server in AWS.
Deployment of Docker Containers
To start, we will look at how to run a server in AWS by manually installing and running Docker on it. We will also teach you how to run Docker images on AWS? Well, to make the process successful, you will require a server. In this case, you can go for the AWS EC2 that allows a virtualized server known as EC2 Instance to boot easily.
Set-Up EC2 Instance
Go to your AWS and click the link available that will direct you to the EC2 console. Click Launch Instance:
Next, to help you run EC2 successfully, select an Amazon Machine Image (AMI). AMI contains all the software components needed to configure it and launch it on your server. AWS offers tons of free and subscription options including MYSQL, AMIs or Windows. In this case, we shall pick Amazon Linux AMI.
Then select the Instance Type that determines the type of CPU, storage, network capacity, and memory needed during the installation. Choose t2.micro, and hit the next button as shown below:
Keep clicking the above button to retain the default settings for all Instance features until you reach to the security configuration page. This page contains firewall rules to control the system traffic. By default, all new ports are blocked so this page will be used to allow incoming HTTP and SSH requests coming from any source. Don’t forget to name the Security Cluster then hit the Review and Launch button:
On this page, hit the Launch button and you will see a pop-up page that requires you to select a Key Pair. This contains both a public and private key folder that is used to link to EC2 Instance via SSH. Click on the “create a new key pair” button and give your key a name. Go ahead and download your key pair.
You can now save your key file to a safe place on your system. Proceed to launch the Instances. This should direct you to the launching page. Hit the “View Instances” button to access the EC2 Instances page.
On this page, you will see the whole list of EC2 Instances present. To get more details, click on the new EC2 Instance file on this page. You will see the public address and launch time. Copy the IP address, since you will use it on SSH when installing Docker.
Next, install Docker on the EC2 Instance. On the location where Key Pair is saved, open terminal and type the following command:
> cd ~/my-aws-key-pairs > chmod 400 my-ec2-key-pair.pem > ssh -i my-ec2-key-pair.pem ec2-user@<EC2-INSTANCE-PUBLIC-IP-ADDRESS>
If you type the correct information, you should have the following information:
__| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/ [ec2-user]$
Now, go ahead and install Docker:
[ec2-user]$ sudo yum update -y [ec2-user]$ sudo yum install -y docker [ec2-user]$ sudo service docker start
Include the ec2-user on your docker set and execute the command without having to use sudo. You will need to log out then log in again for this process to apply:
[ec2-user]$ sudo usermod -a -G docker ec2-user [ec2-user]$ exit > ssh -i my-ec2-key-pair.pem ec2-user@<EC2-INSTANCE-PUBLIC-IP-ADDRESS> [ec2-user]$ docker info
If you do everything correct, the above command should show tons of info about Docker installation without showing any errors. Now you can run your image. In this case we will use the training/web app to get something like this:
[ec2-user]$ docker run -d -p 80:5000 training/webapp:latest python app.py
You will note the “-p 80:5000” text which commands Docker on your Container to link port 5000 to port 80 located on the EC2 Instance. Test whether the process has gone through using the following command:
[ec2-user]$ curl http://localhost Hello world!
If you get the message “Help world,” then know that your Docker container is running in the AWS cloud.
Deploy Docker Container
Now it’s time to run your first container.
Create a Nginx container by typing the following command:
[ec2 -user@ip - 172 -31 -8 -51 ~] $ docker run -d -p 80:80 - -name nginx nginx unable to find image 'nginx:latest ' locally latest: pulling from library/nginx 94ed0c431eb5: pull complete 9406c100a1c3: pull complete aa74daafd50c: pull complete Digest: sha256: 788fa27763db6d69ad3444e8ba72f947df9e7e163bad7c1f5614f8fd27a311c3 status: downloaded newer image for nginx: Latest b60fe57f39f49b7de72e6ceff7d1333ea5b2f6a13952064a831cd634e8b5c3c [ec2 -user@ip - 172 -31 -8 -51 ~] $
Run the docker’s command to confirm that an nginx container has been successfully created:
[ec2 -user@ip - 172 -31 -8 -51 ~] $ CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60fe57f39f4 nginx "nginx -g 'daemon...." 12 seconds ago up 12 seconds 0.0.0.0:80->/tcp nginx [ec2 -user@ip - 172 -31 -8 -51 ~] $
The last step is to visit the public DNS name in your browser, you should have something like this:
Now that you have successfully deployed Docker Container or AWS EC2 everything looks perfect. However, you may experience some problems with automation and integration when using the above manual process.
- First is automation since the above process involves deploying one Docker container to a single EC2 Instance. So, how do you go about multiple containers to deploy across several EC2 Instances?
- The next is integration. When you run Docker container on AWS cloud, it’s just one part of the whole configuration process. But you need to integrate Docker with the rest of the structures in your system.
To solve these issues, you can use the EC2 Container Service (ECS) that requires you to create an ECS group or cluster which represents a set of EC2 Instances under the ECS. Then specify the container you need to run and leave it to ECS to deploy those containers in the Cluster given including developing newer versions and using them to integrate other AWS structures.
ECS can be useful in managing different Docker containers on a variety ofEC2 Instances.
Congratulations! You have now learned how to successfully configure and deploy Docker containers on AWS EC2. Thanks for reading this far we hope this information was helpful for those who are stuck on Docker installation and deployment on EC2.