Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is AWS?
3.
What is Docker?
4.
Why Use Docker with AWS?
5.
Ways to Use AWS Docker
5.1.
Using Elastic Beanstalk
5.2.
Using ECS
5.3.
Using EKS 
5.4.
Using Lambda with Docker
6.
Steps to Use AWS Docker
6.1.
Installing Docker 
6.2.
Creating the Application and Dockerizing
6.3.
index.js
6.4.
package.json
6.5.
Dockerfile
6.6.
Deploying Docker Application in AWS
7.
Frequently Asked Questions
7.1.
Is Docker available on AWS for free?
7.2.
Can we use Docker with AWS Lambda?
7.3.
Is AWS the only cloud provider that supports Docker?
7.4.
Can we use Docker to deploy applications on AWS without using an AWS service?
8.
Conclusion
Last Updated: Mar 27, 2024
Medium

AWS Docker

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Amazon Web Services(AWS) is one of the biggest saviors in this cloud revolution. It offers several services that can help an organization to grow its business. An organization can easily deploy its applications without worrying about storage and flexibility. There is a tool that is frequently used with AWS services is Docker. It is a containerization platform that simplifies application deployment and management.

aws docker

In this blog, we will discuss about AWS Docker. Firstly, we will discuss about what AWS and Docker are. Then we will understand why to use Docker with AWS. In the last, we will discuss steps to implement AWS Docker. 

So, let's get started.

What is AWS?

AWS is a cloud computing platform presented by Amazon. It offers various services to help organizations. It helps them to store, manage, and access data on the internet. Without using hardware or a physical server, we can easily do all the tasks that are required for an application. 

aws

AWS also provides storage, database, and computing power. It is used to store and backup the data securely on the cloud. It also provides hosting services. It is scalable if an organization wants to scale its business.

Now, you might be wondering what docker is.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is Docker?

Docker is a tool that helps developers and testers to create a more efficient and scalable environment. We call it as a container. This container contains everything that is required to run the application or the test cases. Docker is an open-source platform. 

docker

In the Docker container, we can put our code, dependencies, libraries, and other important things that are required. This helps to run the application on any computer or server.

Now, you might be thinking why we should use Docker with AWS. Let us discuss this.

Why Use Docker with AWS?

When we combine Docker with AWS, it offers several advantages for us:

  1. Docker containers provide a consistent environment. This makes it easy to move applications between different AWS services and even on-premises servers
     
  2. AWS's auto-scaling capabilities combined with Docker's container orchestration enable seamless scaling of applications based on demand
     
  3. Docker's lightweight containers consume fewer resources compared to traditional virtual machines. This helps in optimizing resource utilization on AWS
     
  4. Docker containers allow faster application deployment. This helps to enable organizations to roll out updates and new features very quickly
     
  5. Docker containers offer application-level isolation. This helps in enhancing security and reducing the risk of conflicts between applications
     

Now, you might have a doubt here about the ways to use AWS Docker. 

Ways to Use AWS Docker

As we discussed earlier, AWS offers several services to make things easy. So, to use Docker with AWS, we have several ways:

Using Elastic Beanstalk

We can use AWS Elastic Beanstalk. It provides a platform for deploying and managing applications. It also supports Docker containers alongside other deployment options.

Using ECS

We can also use AWS ECS(Elastic Container Service). It is a fully managed container orchestration service. It enables running Docker containers at scale, managing cluster resources effortlessly. It is one of the most preferred ways to use AWS Docker. 

Using EKS 

We can also use AWS EKS(Elastic Kubernetes Service). It allows deploying, managing, and scaling containerized applications using Kubernetes. It is an open-source container orchestration system.

Using Lambda with Docker

We can also use AWS Lambda. It is a serverless computing service. It supports the use of custom Docker containers for running functions.

Now, you might be wondering how we can use AWS Docker. Let us discuss this with the help of one of the above-mentioned ways.

Steps to Use AWS Docker

There are several steps to use Docker with AWS. Before moving on to the steps, we need to install Docker on our local machine. 

Installing Docker 

There are several steps to install the Docker on our local machine.
Step 1: Go to the official website of Docker and download it.

installing docker desktop

Step 2: Now, go to the file location and install it on your machine.

installing docker

Step 3: After installation is done, you need to restart your machine.

restart your machine after installation

Step 4: After the restart, you need to accept the license agreement.

accepting license and agreement

Step 5: Now, you need to install the newest version of the WSL(Windows Subsystem for Linux) kernel. So you can open Windows Powershell and write the command:
wsl --update

performing wsl --update

Step 6: Now, you can start the Docker desktop application and sign in or sign up with your account.

start the docker

Now, Docker is installed, so we need to create an application. This can be our ReactJs web application, ExpressJs, etc. Suppose we are creating an ExpressJs web application.

Creating the Application and Dockerizing

There are several steps to create an application:

Step 1: We need to create a folder and open our favorite IDE in it. Suppose we are using VS Code.

opening vs code

Step 2: Now, we need to create a file index.js. In this file, we are going to write code for our Express application.

creating index.js

We need to write the following code in the index.js file:

  • index.js

index.js

// Importing the Express library
const express = require("express");


// Creating a new Express application
const app = express();


// Defining a route for the root URL ("/") of the application
app.get("/", (req, res) => {
  // Sending a response to the client
  res.send("Hello Ninjas, this is my Express application");
});


// Defining another route for the URL "/me" of the application
app.get("/me", (req, res) => {
  // Send a response to the client
  res.send("Hi Ninjas, I am Narayan Mishra");
});


// Starting the Express application on port 5000 and listening for incoming requests
app.listen(5000, () => {
  // Output a message to the console indicating that the server is listening
  console.log("listening");
});

Step 3: Now, we need to add the dependencies to our application. So, create another file package.json and add the following dependencies.

  • package.json

package.json

{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  }
}

Step 4: Now, we need to Dockerize our application. So, we need to create a file with the name Dockerfile and the following commands in that file.

  • Dockerfile

Dockerfile

# Using the official Node.js image with the alpine Linux distribution as the base image
FROM node:alpine

# Setting  the working directory
WORKDIR /app

# Copy the package.json file from the current directory
COPY package.json .

# Installing the Node.js dependencies
RUN npm install

# Copy all the files from the current directory
COPY . .

# Expose port 5000 to allow external access to the application running inside the container
EXPOSE 5000

# Set the command to be executed when the container starts
CMD [ "node", "index.js" ]

Step 5: Now, we need to build our application. So, type the following command in the terminal:

docker build -t express-app .
running docker build

Step 6: Now, we need to check whether the docker image is created or not. So, type the following command in the terminal:

docker images
checking docker image

Step 7: Now, we need to run our container. So, we need to type the following command in the terminal:

docker run -p 8080:5000 74d532c888a2
running docker image

Now, we can see the result as listening, which means we can go to our port and check our application is working fine.


Step 8: Now, open your browser and type the following URL:

http://localhost:8080/
output of first route

To check another route, type the following URL:

http://localhost:8080/me
output of second route

We have successfully Dockerized our Express application. Now, we will try to deploy it using AWS.

Deploying Docker Application in AWS

There are several steps to deploy the application:

Step 1: Go to AWS and sign in to the console.

signing in

Step 2: Now, search for ECS in the search box and click on it.

search ecs

Step 3: Now, we need to get the image that we have created. We will be using  ECR, so click on the ECR.

go to ecr

Step 4: Now, we need to create a new repository. ECR is like Docker Hub, where we can store our images. So, create a new repository by clicking on the get started button.

ecr home

Step 5: Now, we need to make a public repository so that we can access it easily. We need to give a suitable name for our repository. We need to leave other things as default and click on the create repository button.

creating public repo

Now, we will see that we have created a repository successfully.

created repo successfully

Step 6: Now, we need to build our image and push it into the repository which we have created. So, click on the repository.

click on repo

Before the next step, make sure you have installed the AWS CLI on your local machine so that you can run commands of AWS easily.

installing aws

Step 7: Now, click on the view push commands button. We will use different commands to build and push the image. So, firstly we need to authenticate ourselves. So, copy the first command and paste it to your terminal.

commands to copy

We need to paste these commands into our terminal one by one.
 

Step 8: After running all the commands, we will find that the image has been successfully pushed into our repository. So, now to run our application, we need a server and some kind of networking. For this, we need to create a cluster.
 

Step 9: Now, we need to create a cluster. So, click on the cluster and create a new cluster.

click on cluster

Step 10: Now, we need to create a cluster.

creating a cluster

Now, we need to fill the details accordingly to create a new cluster. After filling the details, click on the Create button.

create a cluster

Step 11: Now, we need to create a task definition. So, go to the ECS and click on the task definition. 

click on task definition

Now, you need to create a new task definition. So, we need to click on the create new task definition button and click on the first option.

create a new task definition

Now, we need to fill the details for the new task definition accordingly. 

fill the details for task definition

After filling the required details, we need to configure the environment.

configure the environment

Now, leave all other fields after configuring the environment and press the create button.

click on create button

After this, we will see the task definition created successfully.
 

Step 12: Now, we need to configure the security groups. So, go to the instances and find the running instance and click on it.

configuring security groups

Now, configure the security groups. So that anyone can access our application through Public DNS. So, edit the inbound rules.

editing the inbound rules

Then go to the URL we have copied from the public DNS with our port 8080. We will see our output easily.

output of first route

 

With the route /me, we will see

output with me route

Frequently Asked Questions

Is Docker available on AWS for free?

Docker is available for free, but the AWS services you use, such as ECS or EKS, may have associated costs based on your usage.

Can we use Docker with AWS Lambda?

Yes, we can use custom Docker containers with AWS Lambda to run serverless functions.

Is AWS the only cloud provider that supports Docker?

No, Docker is supported by various cloud providers, but AWS is one of the leading providers with a wide range of Docker-compatible services.

Can we use Docker to deploy applications on AWS without using an AWS service?

Yes, we can deploy Docker containers on AWS without using an AWS service(ECS or EKS). AWS provides support for Docker. It allows us to use EC2 instances to run Docker containers or leverage Elastic Beanstalk to deploy Docker containers with minimal management.

Conclusion

In this blog, we have discussed about AWS Docker. We covered what AWS and Docker are. Then we explained ways to use AWS Docker. We have also discussed the steps to use it. If you want to learn more about AWS, then you can check out our blogs:

We hope this blog helps you to get knowledge about AWS Docker. You can refer to our guided paths on the Codestudio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Interview ExperienceCoding interview questions, and the Ultimate Guide path for interviews.

Happy Learning!!

Previous article
Amazon Analytics
Next article
Application Integration in AWS
Live masterclass