Deploy a Custom Docker Image with Nginx and Save it to AWS ECR
Tags: devops, docker, nginx, aws
Published: 2022-10-08 17:52:01

In this article, I am going to show you how to create a **Custom Docker image** with a **Docker file** that will tell you the date and time the container has been deployed using **Nginx** and save that data to the AWS’s **ECR** (Elastic Container Registry).


## What is Docker?

**Docker** is an open platform for developing, shipping, and running applications in a container. **Containers** don’t use any of the host resources like hardware or CPU. An **image** is a read-only template with instructions for creating a Docker container. You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a **Dockerfile** with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast, when compared to other virtualization technologies.

When we use **_Docker_** we will use images, such as an **_NGINX image_** and spin up a **_container_** with it.

This is my [GitHub Link](https://github.com/awsmine/Docker_projects/tree/main/Docker_Nginx_Image_ECR) for this project.

_**Let’s get started!**_


## Objectives

- Create your own image using Nginx and add a file that will tell
  you the date the container has been deployed
- Deploy your container with port 8080 open
- Save your container data to the AWS Elastic Container Registry 
  (ECR)

## Pre-requisites:

- AWS user account with admin access, not a root account.
- Cloud9 IDE, comes with Docker installed.
- Allow the EC2 instance to be accessible by our IP ([Add an 
  inbound rule to the EC2 Security Group to allow the instance to
  access port 8080](https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html#app-preview-share-security-group)).
- An account with [Docker](https://www.docker.com/) and [Docker Hub](https://hub.docker.com/)

For this article, I used [Docker documentation](https://docs.docker.com/get-started/overview/).


## Create a Directory

In the Cloud9 IDE, create a directory to save your HTML & Dockerfile.
Then cd into that directory

**`mkdir <Directory name>
    mkdir docker_work`**

**`cd <Directory name>
    cd docker_work`**

## Pull Latest Nginx Image from Docker Hub

We have to use Nginx to deploy our custom website, so pull the [latest version of the Nginx image](https://hub.docker.com/_/nginx) from Docker Hub.

**`docker pull nginx:latest`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kqo044h9iyppkzb2xyr.png)

This will bring the image on to your local computer. verify that we pulled the latest image.

**`docker images`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oechchr1zymp04540n0.png)


## Create Dockerfile and index.html

**Create Dockerfile**

- Modify the **Dockerfile** to make it run our script on start-
  up!

**Note:** make sure the D is capitalized or it will not be recognized by docker.

**`docker_work $ vi Dockerfile`**


Add the following code to the [Dockerfile](https://github.com/awsmine/Docker_projects/blob/main/Docker_Nginx_Image_ECR/Dockerfile) and save it

```
# ---  Docker_projects/Docker_Nginx_Image_ECR/Dockerfile ---


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

- Create a new [index.html](https://github.com/awsmine/Docker_projects/blob/main/Docker_Nginx_Image_ECR/index.html) file in the same directory by typing the following command:

**`docker_work $ vi index.html`**

Add the following html code to the file and save it.

```
# ---  Docker_projects/Docker_Nginx_Image_ECR/index.html ---

<!DOCTYPE html>
<html>
  <head>
    <title> Docker nginx page</title>        
  </head>

    <body>
        <b>
          <h1> Hello! The container has been successfully deployed.
          </h1> 

          <h2> The current date and time:
            <p><span id="datetime"></span></p>
              <script>
                var dt = new Date();
                document.getElementById("datetime").innerHTML = dt.toLocaleString();
              </script>
          <h2/>         
        <b/>
     
    <body/>
<html/>
```

## Build your Custom Image from a Dockerfile

- To start building the **docker container**, we will run the following command:

**`docker build -t <name of the image — create your own>
   docker build -t nginxrev .`**

**Note: the “.” period after your container name is very 
  important.**

This will ensure that we look for files in the current working directory.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6hp22y7wo3iqh94sj5n.png)


- Verify the image from Dockerfile, by running the following 
  command

**`docker images`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txrtzwzrjb2wzlr7t2np.png)

## Create and Deploy Docker Container

- Run the command to create the container from your new image
  and to deploy your newly built container:

**`docker run -d — name <container-name, you name it> -p 8080:80 <image-name>
 docker run -d --name my-container -p 8080:80 nginxrev`**

**-d** #Run container in background and print container ID
**-p** #Publish a containers port(s) to the host


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0zwr13by8qp52rsnhn1.png)

- Run the command to see that your new container has been created and deployed

**`docker ps -a`**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5uhpdard8hyujdveg1e9.png)

## Deploy your container with port 8080 open

Now it is time to test the code.

- First, Run the command to check your container has Internet
  access:

**`curl localhost:8080`**

It prints out the index.html

- Now click on the _Share_ button on the extreme right of **Cloud9 IDE**.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cullffe1dffh2stklqv0.png)


- Note the **Cloud9-IPAddress**
- Open up a browser and enter the **<cloud9-IPAddress>** followed by a **colon :** and **8080** for the port

**`<cloud9 IP Address>:8080`**

_SUCCESSFUL!!!_

You see this on your web browser