Using AWS Cloud9 with Docker to deploy a NGINX static website
Expected outcome: Deploy a static website using a docker container. The website will display the date and time of deployment for the container. Then I will push the container to the newly created ECR (AWS Elastic Container Registry).
NGINX (Pronounced engine-x) is an open-source reverse proxy server for HTTP, HTTPS, SMTP, POP, and IAMP protocols, as well as a load balancer, HTTP cache, and a web server (origin server). (Reference: https://hub.docker.com/_/nginx)
To accomplish this task, we were giving the following instructions:
1. Create your own image using Nginx and add a file that will tell you the date the container has been deployed
2. Deploy your container with port 8080 open
3. Save your container data to the AWS Elastic Container Registry (ECR)
I. Log into your AWS account and select Cloud 9 to create a new environment (I decided to create a new one for this task, however, if you already have an environment, you can utilize a current cloud9 environment)
II. After the EC2 instance has completed the provisioning process, access the Security group section, the Inbound rules and add port 8080 with a 0.0.0.0/0, however, this is only for testing purposes.
III. Create new directory to place two files: Dockerfile and index.html, to be included in the container and image.
a. At the prompt $, enter: mkdir folder’s name ( mkdir nginx)
b. Change into the new directory: ( cd Dockerfile)
IV. Create the index.html file for the static website
a. touch index.html
b. Edit the file either by opening the file and making the changes in the upper plan above the terminal window or using VIM index.html, however when using VIM, you will also need to be familiar with the program.
V. Download (pull) latest Nginx image from the registry
Verify that the image is tagged as latest by running
VI. Create the index.html file:
Enter the following html code or something similar:
VII. Time to build your custom image
Run the command: docker build -t mywebserver . (the “. “ Denotes utilizing the current directory for the file location)
VIII. Let’s run the command to deploy our newly built container:
docker run -it -d -p 8080:80 webserver
(-i = interactive (Keep STDIN open even if not attached; t=Allocate a pseudo-TTY; -d=Run container in background and print container ID; -p=Publish a containers port(s) to the host)
IX. Now it is time to test the code.
a. Access the Share this environment info in your cloud environment
b. Note the public IP address
c. Open a browser and enter the address followed by a colon “:” and 8080 for the port, if everything has been configured correctly in the previous steps, then you should see something like…
X. Now we must create an AWS Elastic Container Registry, which are used to store, share and deploy your container software anywhere. (Reference: https://aws.amazon.com/ecr/)
a. Obtain the AWS credentials for your account to be used with the ECR
aws ecr get-login-password -region us-east-1 | docker login AWS -password-stdin accountID.dkr.ecr.us-east-1.amazonasw.com
b. Aws sts get-caller-identity
c. Aws ecr create-repository -repository-name mdcproject17/nginx-mywebserver
d. Result of the previous steps on creating an AWS ECR
Success and congrats, you have utilized an AWS Cloud9 environment with an EC2 instance, a docker container to build a Nginx static website. Then you created an AWS ECR to push it to the cloud for storage, sharing anywhere.
Originally published at https://medium.com on June 14, 2022.