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 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).
- An account with Docker and Docker Hub
For this article, I used Docker documentation.
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 from Docker Hub.
docker pull nginx:latest
This will bring the image on to your local computer. verify that we pulled the latest image.
docker images
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 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 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.
- Verify the image from Dockerfile, by running the following command
docker images
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
- Run the command to see that your new container has been created and deployed
docker ps -a
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.
- Note the Cloud9-IPAddress
- Open up a browser and enter the followed by a colon : and 8080 for the port
<cloud9 IP Address>:8080
SUCCESSFUL!!!
You see this on your web browser
Top comments (0)