Docker Deployment, NGINX and AWS ECR

Christopher Lawshe
4 min readJan 28, 2023

--

I complete a project this week in my pursuit of learning docker to become a DevOps engineer. For this one I had to complete the following 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)

Preprequisites for my project:

  • Access to a Linux environment with Docker installed & enabled
  • A directory in the Linux system with Git initialized
  • AWS Free-Tier account with an existing ECR repository
  • A Free Docker Account with an existing repository

To start this project, I created a new directory in my Linux environment named ‘week16’:

mkdir week16 [enter]

Then entered the new repository to start my Docker build:

cd week16 [enter]

Next, I needed to run a command to start my Docker container running NGINX and open it on port 8080:

docker container run -d -p :8080 --name week16 nginx

The ‘-d’ runs my docker container in the background and displays the container ID on the screen.

The ‘-p’ allows me to publish my container’s port(s) to the host.

The “- -name” allows me to assign my container a unique name

After creating the container I ran the docker container ls command to display a list of my running docker containers:

The highlighted container is my newly created Docker container running NGINX

Next, I needed to alter my index.html file. To accomplish this I had to enter my docker container’s CLI:

docker container exec -it week16 bash

Once inside we will want to install vim to the container so that we can edit the index.html file:

apt-get update
apt-get install vim -y

Next I need to navigate to the right directory to store a custom index.html file. The common location for this in an NGINX web server is /usr/share/nginx/html:

cd usr/share/nginx/html

Once inside this directory, all that’s necessary is to edit the index.html file that’s already there:

vim index.html

To edit the file, I typed [shift] + I and changed the HTML file as I pleased:

<!doctype html>
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body>
<h1>Welcome to World BlackTeam!</h1>
<p>Christopher was here...</p>
</body>
</html>

To exit the file and save I typed [esc] and “:wq!” [enter].

To confirm that my NGINX server is working correctly I need to find the IP address of the container housing it. To do this, I exited the container CLI by typing ‘exit’ [enter] to get back to my Linux environment. Then I needed the container’s ID. To get this I typed:

docker container ls

The ID is listed for my ‘week16’ container in the far-left column of the output on-screen. I copied the ID of ‘week16’ and then typed:

docker container inspect <ContainerID>

The output from this command gives a large amount of data about the container. In the body of text, I found the “IPAddress” attribute and copied it, then typed:

curl <IPAddress>

The output reflects the changes to the index.html file that I changed earlier.

The Final step is to get my image stored in 2 places:

  • Docker Hub
  • AWS ECR

Starting with Docker Hub, I first needed to login to my account from the terminal:

docker login --username=yourhubusername

After entering this command I was prompted for my Docker Hub account password and the configuration was complete. Next I pushed my docker image to the repository. To do so I need to commit my docker image and add a tag to the image:

docker commit <ContainerID> <Docker Hub account name/Docker Hub repository name:tag>

Next I need to push to docker image to my repository:

docker push <Docker Hub account name/Docker hub repository name:tag>>

Once the terminal shows the push as successful I checked my Docker Hub repository to confirm:

Next I started the process of pushing the image to my AWS ECR repository. First I needed to Docker client to the Amazon ECR registry:

aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com

(the above command will not work if proper permissions are not configured for the repository to allow access)

NOTE: This command and the following aws commands can be found by selecting the repository ‘push commands’ button in the Amazon ECR console page. More Info here

Once this command succeeds I had to tag my docker container again for AWS:

docker tag xxxxxxxxx aws_account_id.dkr.ecr.region.amazonaws.com/my-repository:tag

Now all that’s left to do is push the container to the ECR repository:

(IMPORTANT: For this push I had to procure the name of the container given from the <docker container ls> command rather than the repository name used for the Docker Hub push)

docker push aws_account_id.dkr.ecr.region.amazonaws.com/<container-name>:tag

The final step is confirming the push goes through from the AWS ECR console container registry:

With the push going through successfully, my container image is backed up in two locations and my project is complete.

--

--

Christopher Lawshe
Christopher Lawshe

Written by Christopher Lawshe

IT enthusiast — looking to learn from and connect with IT professionals to help push the world forward through Cloud DevOps

No responses yet