Deploying a Static Website in a Docker Container using NGINX

#14
Topic created · 1 Posts · 27 Views
  • We are tasked with deploying a custom image and writing script in a file. Prerequisites to complete this project: Docker Account (free), Docker running locally, IDE or text editor (VSCode, Atom, etc.), and an AWS account with AWS CLI configured.

    Steps:

    1. Create your own image using NGINX and add a file that will tell you the time of day the container has been deployed

    2. Deploy your container with port 8000 open

    3. Save your container data in an S3 bucket

    What is Docker?

    Docker is a software platform that simplifies the process of building, running, managing and distributing applications. It does this by virtualizing the operating system of the computer on which it is installed and running.

    What is NGINX?

    Nginx is an open-source web server that, since its initial success as a web server, is now also used as a reverse proxy, HTTP cache, and load balancer.

    What is an AWS S3 Bucket?

    An Amazon S3 bucket is a public cloud storage resource available in Amazon Web Services’ (AWS) Simple Storage Service (S3), an object storage offering. Amazon S3 buckets, which are similar to file folders, store objects, which consist of data and its descriptive metadata.

    Step 1: Create your own image using NGINX and add a file that will tell you the time of day the container has been deployed

    Pull latest NGINX image

    docker pull nginx:latest
    

    Create a new folder for your NGINX image files

    mkdir nginx-html  
    cd nginx-html
    
    #Create an index.html file  
    touch index.html
    

    Add a file that tells the time of day the container has been deployed

    Head over to your IDE (I use VSCODE) and inside your index.html file, insert the code below. Setting the language to HTML will greatly help in formatting and autocompletion.

    Step 2: Deploy your container with port 8000 open

    Run container with flags:

    • -d specifies the container runs in detached mode
    • -p tells Docker to map the ports exposed in the container by the NGINX image to the specified port on the Docker host.
    docker run -d -p 8000:80 -v ~/nginx-html:/usr/share/nginx/html --name <your-nginx-name-here> nginx 
    

    The line of characters returned is the long form of the container ID that has been created.

    Verify container creation by running the command below:

    docker ps 
    

    Go to your browser and visit your port on local host to view static page that was created (http://localhost:8000😞

    Step 3: Save container data in an S3 bucket

    Be sure to have your account configured through AWS CLI for this part!

    Make a new S3 bucket

    aws s3 mb s3://<insert-unique-name-here>
    

    Have the path to your index.html file handy for this next part!

    aws s3 cp "<file-path-here> (example: /Users/user1/nginx-html/index.html" s3://<insert-created-bucket-here>
    

    Head over to your AWS Console to ensure that the new bucket file created allows public access to all, then copy and paste the URL of the bucket file into your browser.

    You should see your index.html page in your browser after copying it and searching it up. After this step, you are finished! Be sure to clean your environment as needed.

    Thanks for checking this out! Hope it helps! Find me on LinkedIn — linkedin.com/in/helenccampbell

Log in to reply