How to deploy a docker image using Nginx and save it to Amazon ECR.

black and gray laptop computer turned on doing computer codes
Reading Time: 4 minutes

We all know a docker image is a read only template that contains a set of instruction for creating a container that runs on docker platform.So today we we will see how to deploy image using Nginx and further would push it to the Amazon ECR.

Prerequisite :-

  • A Docker account
  • AWS account
  • Brief knowledge about Nginx

1- Lets create the nginx container

The first step is to create the container. To do this pull an Nginx image using the below command.

docker pull nginx:latest

The above command will pull the latest image of nginx.

Now next create a directory in which we will store our static file. Make a folder and move into that .Next create a file that that would print the date and time .To do this follow the below code

- mkdir <folder-name>
- cd <folder-name>
- touch <file-name>

Next the html code that i wrote to print the current date and time is :-

2- Deploy the docker container using nginx .

To deploy we will follow the below steps. Using the <docker run > command we can achive that .

The below command will help achieve that.

docker run -d -p 8082:80 -v <your-src-path>:/<your-dest-path of nginx-html > -- name <name of image> <image name>

docker run -p 8084:80 -v ~/Documents/nginxblog:/usr/share/nginx/html/  --name nginx-html nginx

Flags Explanation :-

  • – d :- To run in detached mode
  • – p :- For port-forwarding on our own port
  • – v :- For mounting volume from src to dest
  • – – name :- for naming the conatiner.

The below output shows the logs on running the above container.

To see the running container use the below

docker ps 

Next go on the port that you have mentioned while forwarding . In my case it is 8084. So when i type localhost:8084 the output comes a s follows :-

Now the first part of our objective is done. Next we have to push this Container to ECR .

3- Now Pushing The nginx container to ECR – Elastic Container Registry

As of now we have finished container creation . Next we will move to sharing this to Amazon’s Elastic Container Registry. As already mentioned before you should have an aws account. Next login into the aws console and search for Elastic container Registry .

Lets start with the below steps and share the container into ECR.

a – Next click on get started

b – On creating repository you will get something like below that will show your repository

c- Now to push the nginx container using the below command.

aws ecr get-login-password --region ap-south-1 | docker login --username AWS --password-stdin < your name of repo>.dkr.ecr.ap-south-1.amazonaws.com

docker build -t nginx-website .
docker tag < your name of repo - aws account no. >.dkr.ecr.ap-south-1.amazonaws.com/nginx-website:latest
docker push < your aws account number >dkr.ecr.ap-south-1.amazonaws.com/nginx-website:latest

NOTE : – The above command will be available in the aws repo. There is an option of <view push command > . After clicking that you will get all the push command.

For using the above push commands you need to have the following prerequisite

  • aws cli configured
  • aws access key and secret key declared <aws configure>
  • iam role defined to a user with ecr policy

d – After running the above command we finally created our nginx container .

Conclusion

This was all about how to create aws elastic container registry . This is really helpful; if you want to store your private images . The steps have been simplified and will help to make a container . The best use case of Amazon ECR is when you want to have your own private repository.

For more information about docker container click here :- https://blog.knoldus.com/what-are-docker-containers-and-why-use-them/

Reference

https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html

Written by 

Rishivant is a enthusiastic devops learner at Knoldus. He believes in going 1% up everyday and showcases his learning in his work.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading