From Dockerfile to ECS: Building, Containerizing, and Deploying an Application Using ECR

From Dockerfile to ECS: Building, Containerizing, and Deploying an Application Using ECR

Firstly create and ec2 instance with ami ubuntu and free tier and launch and instance.

after launching the instance connect to that instance.and run following commands.

sudo apt update -y

sudo apt install docker.io

docker --version

after installing docker when you use docker ps it will give an error. to resolve that error add our user to docker group by using below command and reboot the system.

sudo usermod -aG docker $USER

sudo reboot

after doing connect to your instance again now it will not give error. but it will not show any container as we didn't created any container.

To create a container we need to have a docker image for that we need to have dockerfile. so use below command to clone the application of nodejs. If you have your own application you can create dockerfile of that do the same steps follow below steps.

git clone https://github.com/koolkishan/chat-app-react-nodejs.git

ls

cd chat-app-react-nodejs/

vim Dockerfile

Dockerfile code:

# Use the official Node.js 14 image as a parent image
FROM node:14

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to work directory
COPY package*.json ./

# Install dependencies
RUN npm install

# If you use Nodemon for development, install it globally in the container
RUN npm install -g nodemon

# Copy the rest of your application's code
COPY . .

# Map port 4000 to the outside world (change this if your app uses a different port)
EXPOSE 4000

# Command to run your app using Nodemon
CMD ["nodemon", "index.js"]

ls

cd ..

aws configure

sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o

cd ..

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

sudo apt install unzip

unzip awscliv2.zip

sudo ./aws/install

aws --version

Firstly create a ecr repository give name to it and select operating system as below.

now we have to push our image from ec2 to ecr. for that we need to create a user in IAM and create a secret key and access key. To configure aws cli use below command.

aws configure

after using aws command now use below commands. you can go to ecr repo an click

you will be able to see view push commands run those commands line by line. and you will be able to see your image to ecr repository.

aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/o5o9z9v5

cd practise/

cd chat-app-react-nodejs

aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/o5o9z9v5

docker build -t node-to-do-appliaction .

docker tag node-to-do-appliaction:latest public.ecr.aws/o5o9z9v5/node-to-do-appliaction:latest

docker push public.ecr.aws/o5o9z9v5/node-to-do-appliaction:latest

After that you need to run application using ecs so create a cluster for that. use below setting for that.

To run the application you need to create a task defination as below.

after that click on create task defination. now go to deploy and choose run task. then it will create an enviroment to run application create that. after that you will get below screen.after that you will be able to see a public ip copy that ip and paste it in chome with the port which you have added while creating task defination.

now you will able to see your application running on the public ip.

Thank you for reading my article. Big thanks to Kishan Sheth for his amazing application that I used in my project. His hard work made it easier for me to deploy project on ECS. I really appreciate his effort and are happy to include his work in our project. Thank you, Kishan, for your great work. You can checkout his other projects and this project also by below link :- chat-app-react-nodejs

you can clone the project and create dockerfile on your own and try deploying to ecs.

Thank you Kishan Sheth.

Did you find this article valuable?

Support Aakanksha Deshmukh by becoming a sponsor. Any amount is appreciated!