Deploy a Nginx Docker Image and push to AWS ECR

Adam Leonard
Nerd For Tech
Published in
7 min readJan 28, 2023

What is Docker?

Docker is a software platform that allows you to build, test, and deploy applications quickly. Docker packages software into standardized units called containers that have everything the software needs to run including libraries, system tools, code, and runtime. Using Docker, you can quickly deploy and scale applications into any environment and know your code will run.

Running Docker on AWS provides developers and admins with a highly reliable, low-cost way to build, ship, and run distributed applications at any scale.

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Images are pulled from Dockerhub.

A Docker container is a standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

What is Nginx?

NGINX is open source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

Prerequisites

🐳IDE (AWS Cloud9 for this example)

🐳AWS Console User Access

🐳DockerHub Account

🐳Basic Knowledge of Linux

Objectives

  1. Create your own image using Nginx. Include a customized index.html file that can be viewed from a local browser.
  2. Deploy your container with port 8080 open
  3. Save your container data to the AWS Elastic Container Registry (ECR), and to your Docker Hub account.

What are you waiting on? Let’s get started!

Step 1: Update and enable the Docker CLI

Docker CLI needs to be installed on our IDE. To accomplish this, simply type in the command below.

sudo yum docker -y

Next, we need to ensure Docker is running on Cloud9. The following commands will start Docker, enable it on startup, and then check if it is running.

sudo systemctl start docker
sudo systemctl enable docker
docker ps
Docker is working and showing we do not have anything running

Step 2: Create a new Directory for Docker Images and create our Dockerfile

Create a new directory to keep the Image and Dockerfile, then move into that directory

mkdir <Directory Name>
cd <Directory Name>

Pull the Official Docker nginx image from Dockerhub. This will get us the default nginx build. We will edit the image with our Dockerfile and deploy it as a container.

Open the Vi editor to create our Dockerfile.

Input the following code to use the file FROM nginx image, COPY the index.html file into the below directory, and EXPOSE port 8080 on the container. Save the file.

FROM nginx
COPY index.html /usr/share/nginx/html
EXPOSE 8080

Open Vi again to create the index.html webpage file. This Dockerfile will output the current date and time with a special image when viewed in a browser.


<html>
<body style="background-color:rgb(222, 49, 99);"><center>
<head>
<title>Docker Project</title>
</head>
<body>
<h1>Hello!! The current date & time :
<p><span id="datetime"></span></p>
<script>
var dt = new Date();
document.getElementById("datetime").innerHTML = dt.toLocaleString();
</script>
</h1>
</body>
<img src="https://i0.wp.com/winkgo.com/wp-content/uploads/2019/11/congratulations-memes-04.jpg">"
</html>

Step 3: Building the Docker Image and Create the Container

The code below will build out a custom image, with the build commands located in the Dockerfile. The period at the end informs the CLI where to look for our Dockerfile. The period is known as the current directory.

docker build -t <image name> .

The below code with create a new container, expose port 8080 to the internet and build the container from the custom Docker image we just created.

docker container run -d --name <Container_Name> -p 8080:80 <Image_Name>

Check to make sure our container was created.

docker container ls

Check to see if our container is accessing the internet in the CLI. If everything is correct, the HTML code we added to the Dockerfile should display in the CLI.

curl localhost:8080

*If you are using Cloud9 IDE like me, check the EC2 instance Cloud9 is running on to obtain the public IP address to check the webpage.

**If the request continues to time out, check the inbound and outbound security groups of the subnet the EC2 is running in. For this example, ensure port 8080 is allowed, or just allow all traffic if needed.

Inserting the Public IP address with port 8080 into an internet browser should give us the output of the HTML code entered.

<Public IP>:8080

Step 4: Save the container data to Amazon Elastic Container Registry and DockerHub

Amazon ECR is a fully managed container registry offering high-performance hosting, so you can reliably deploy application images and artifacts anywhere.

The following command will create the repository in ECR. We still need to push to contents to the repository in a later step.

aws ecr create-repository --repository-name <Repo Name> --region <Region Info>

Head over to the ECR Console in AWS to verify it was created.

Now that a new repository was created, the image needs to be pushed to ECR. Inputting this command will generate a token to access the registry. Save this token for the next step.

aws ecr get-login-password --region <Your Region>

Next, input the following command to select the region, login into the docker CLI, and push to the correct ECR URI. The URI can be obtained within the ECR Console in AWS.

aws ecr --region <Region>| docker login -u AWS -p <Token Output> <ECR Repository URI>

Name the image we created and input the ECR Repository URI again.

docker tag nginx-custom 089867102438.dkr.ecr.us-east-1.amazonaws.com/docker-nginx-custom

Lastly, Push the image up to ECR.

docker push <ECR URI>

Step 5: Push the Container Image to DockerHub

Similar to GitHub, DockerHub allows users to save images and containers to repositories. This allows for public or private repositories than can be utilized later.

Commit the changes we want to send.

docker commit <Container Name> <Dockerhub_User>/<Repo_Name>:TAG

Push the container data up the Dockerhub.

docker push <Dockerhub_User>/Repo_Name>:TAG

Look in our Docker Hub account and there is the image we just tagged and pushed.

Congrats. We successfully created a custom Docker Image and pushed it up to our Repository.

Please check out my medium page for more videos and be sure to give me a follow. See you on the next one!

--

--