Skip to main content

Command Palette

Search for a command to run...

Hosting Custom Website on NGINX server

Published
3 min readView as Markdown

Step 1: Create Dockerfile and Custom HTML Page

Step 2: Build the Image

Step 3: Create and Deploy the Container

Step 4: Push the Image to Docker Hub

Step 5: Push the Image to AWS ECR

First, create a new directory containing our Dockerfile and custom index.html page.

#mkdir <directory name>
mkdir devops    #here directory/folder name is devops, inside this we can create our index.html, Dockerfiles
#(in index.html file below content should be paste)
<!DOCTYPE html>
<html>
  <head>
    <title>Devops Project!</title>
  </head>
  <body>
    <h1>This is our Custom Home Page</h1>
  </body>
</html>

Next, create a new file called Dockerfile. A Dockerfile contains the commands or instructions used during the build to create the Docker image.

 FROM nginx:1.10.1-alpine
 COPY index.html /usr/share/nginx/html
 EXPOSE 8080
 CMD ["nginx", "-g", "daemon off;"]

Now that we have our Dockerfile and source files, we can build the image. We use the docker build command and specify a tag and the location of the Dockerfile, and a context. The context is the set of files to which the build process can reference. In our case, we are using the COPY command to access our custom index.html file.

#docker build -t <new_image_name:version> <url or path of context>
docker build -t website:v1 .    # here . means current directory where docker file exits

Let’s confirm our image has been created. You can run the following command to list out the images.

docker images

To create the container, run the following command. You may use any name you would like to call the container but you need to use the name or image ID of the image we just created. The “-p” switch will map port 8080 of the host to port 80 of the container.

#docker run -d --name <name-container> -p 8080:80 <image_name:version>
docker run -d --name web-container -p 8080:80 website:v1

Let’s confirm that it is running by entering the following command:

docker ps

check our website with EC2 public IP with port 8080 in browser as shown below:

<publicIP>:8080

Next push of above docker image to Docker Hub and AWS ECR repositories

docker login     # give username and password of dockerhub when it asks
#docker tag <image name>:<tag> <dockerhub username>/<image name>:<tag>
docker tag website:v1 xyz/website:v1    #here just assume username as xyz
docker push xyz/website:v1
#for this aws cli should be installed on your ec2 server before doing this with aws configure with access key and secret access keys
#Note: below shown just format, you can directly copy & paste the commands from ECR repo push commands tab
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <233489832487.dkr.ecr.us-east-1.amazonaws.com>
docker tag <IMAGE_NAME>:<IMAGE_TAG> <REPOSITORY_URI>:<IMAGE_TAG>a
docker push <IMAGE_NAME[:TAG]>

More from this blog