Now that you’re done with the development phase, are you ready to Deploy your Docker Container to AWS? This post will walk you through the process of creating a cloud-based framework that allows you to build and successfully deploy applications without much hassle. It will help you understand the code involved when deploying a container to AWS.
You will need to understand this if you are to get started successfully. It will take you through the entire process from creating AWS and Docker accounts, payment, and the input and installations you will have to go through.
Containers are the future of developing applications as they offer portability and security that are not otherwise as easy to achieve. You will understand the virtualisation of containers and how to move applications from one platform to another. By the time this post concludes, you should have an up and running version of Docker linked to Amazon Web Services.
The AWS Journey Part 1: Deploy Docker Container to AWS
Registration
To begin deploying a docker image, you should start with opening an account with the website. You will not be able to use the platform without doing so. When done, you will be required to create a repository.
This is a form of storage that we will use to publish the Docker image to AWS later. You should also ensure that you have installed Docker on your computer to maximise the use of their services.
Once this is complete, the next move is to initiate an account with Amazon Web Services. This is a simple sign up procedure that will require you to pay a reasonable fee. AWS is very open about how much it charges. Pick one based on your needs.
Designing your Docker Image
To create a Docker image, you will need to use a Docker file.
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [“java”,”-jar”,”/app.jar”]
EXPOSE 8080
What you see above is what is known as file wrapping. It is used to wrap your Spring Boot application. When beginning, you must use OpenJDK with a Linux alpine distribution, which will lead you down the JAR file argument. Once you have completed this stage, you must copy the JAR file into the app.jar that will be using java- jar app.jar. The application should now be able to run.
You shall be using Docker through port 8080 to document all your work.
Once we have completed this section, we will be required to create the Java application.
To do this, we will need to use the following code
.grad/flew clean build
When you input this code, it allows you to create a folder that will be called
build/libs/aws-hello-world-0.0.1-SNAPSHOT.jar
Take note of this because it will be the folder that Docker can recognise automatically. We initially used build/libs/*.jar as the main value for JAR_FILE when we initially invented the Docker file.
Once this is done, we can now move onto the next stage and run the following code
docker build -t reflectoring/aws-hello-world:latest
If you wish to run a testing phase at this point, you can run the following code
docker images | grep aws-hello-world
When you run this code, it should show you all the Docker images deployed through AWS. If you see the correct images, then you have managed to add the code successfully. If not, you should consider going through each step one more time.
Publishing Docker Image
When we have completed the Docker image testing, you should have found that AWS was available in the Docker register because this is the only way we will be able to download it.
You have the option to release docker in any registry you choose. However, you must have access to the internet when doing so. If you are working on something of the utmost confidentiality, such as a business application, you will need to invent your private docker registry.
Now to begin, you should enter in this code to log in to Docker
docker login registry-1.docker.io
This is the information you will be required to enter to gain access. Feel free to remove this part as Docker comes already set as a default in settings. After this, we shall now publish the Docker image to the registry using this code.
docker push reflectoring/aws-hello-world:latest
Deploying Docker containers with AWS EC2 Instance.
We shall now look at the step by step process for deploying Docker containers with AWS EC2.
EC2 Definition
This is a tech way of saying it is a virtual server that has been designed by amazon for its web services programs. In more elite terms, you would refer to this as an Elastic Compute Cloud that can request for a provision on amazon web services facilities.
Step 1: You will need to Initiate the EC2 instance
In doing this, you will need to enter the AWS console and find the location of EC2. Once done, you can engage the icon, and you should see yourself being directed to the AWS EC2 page. Once there, you will be required to click on the Launch Instance button.
If you expect this to run smoothly, then you will need to make use of the AMI. This is Amazon’s Machine Image, and it is the housing for the entire catalog of software configurations that will be necessary when running your instance. For this occasion, we shall be making use of Linux AMI.
Once complete, you will need to figure out which is the best version of CPU, storage, and network capacity you can use while running the installation. When you select the one best suited to your machine, you will need to click on the Next button.
You will then arrive at the Configure Security Group area on which you can maintain your default settings. We always do this to manage the traffic that is coming through the network when running your instance without disabling Firewall. You have full control over this, so you can change things as you like to what best suits you.
When you are ready to take off, you can click on the launch button, which should see a pop-up page appearing. When this page appears, you will be required to enter the Key Pair. This is what will allow the computer to determine whether the public or private folder is used. In doing so, you will ensure that the EC2 instance is being run securely via SSH. When you have created your new key pair, you must provide it with a name. Do not forget this name because it will be useful when accessing the program. Once completed, you must download and save it.
You should then be able to view the intrinsic of your EC2. Make sure to copy the IP address as this will come in handy when it is time to access Docker and install it through SSH.
Step 2: Installing Docker on EC2 instance
To successfully get through this section, we will need to find the location path to save our key pair. I hope you didn’t forget the name. Once you have found it, you must open the terminal and put this in.
ssh -i keypair.pem ec2-user@<EC2-INSTANCE-PUBLIC-IP-ADDRESS>
You should receive a notification that the connection process was a success. Upon receipt of this, you will now be able to access and install Docker through the following code:
sudo yum install -y docker
If you wish to initiate the sequence on Docker, then you can make use of this command below:
sudo service docker start
At this point, you will be able to run the command that will initiate the image easily. You can try running this code as an example.
docker run -d -p 80:8080 chandupriya/nodedemo:0.1
You will find that your instance is now available, meaning it runs in the web services cloud. Well done.
Questions
When is Elastic Container Service the Best Method for Managing Docker Containers on AWS?
This is something that users will often consider when they need microservices. Often these microservices will be directly connected to a host of other amazon web service functions. You will also be able to use unique schedulers, which will offer you different batch loads for EC2. You will be able to access this on-demand, reserved, or in the case of a spot need.
When is Elastic Beanstalk the Best Method for Managing Docker Containers on AWS?
If you have recently begun to engage in amazon web services, you may not be a total expert with containers. Understandably it can be confusing at first, mostly because Docker can offer a large host of services. You may also not understand how to develop applications fully; hence Beanstalk was invented as a pathway to truly understanding the full abilities of Docker containers.
How Do I Put My Application in a Docker Container?
If you are trying to add your application into a Docker Container, you will have the option of 2 things. You can end the application that is currently engaged with the processor on port 80 to add another one, or you can add it to a separate port. Once done, you can stop the other application, and you can run $(docker ps -a -q) to end the process of all currently active containers.
Conclusion
To end the article, I just want to touch on what we achieved here. By this point, you should understand that your application is now in the cloud on a public registry. Hence, if you are trying to create something private, you will need to design a private image instead of a public registry one. You will now be able to take full advantage of the benefits that AWS offers.
You should be willing to go back and check the code multiple times if you feel that you got lost somewhere along the way. Often the smallest mistakes in code can be cause for grand errors. This process is not as straightforward as many expert programmers may make it out to seem. Therefore, you should not get discouraged if it takes a bit of time to get it right. You need to be aware of the immense benefits that arise as a result of using this software.