Using Docker to Make a Static Webpage with NginX & Storing the Data in AWS S3

Docker is a containerization tool designed to make it easier to create, deploy, and run applications. Containers are packages that can be efficiently shared and deployed. Hence the Docker logo illustrates cargo being grouped and transported in shipping containers.

It’s important to compare Docker and Virtual Machines. Docker images are much smaller than virtual machines and run faster. Also with Docker the containers running share host OS kernel, whereas VMs consist of user space and kernel space of an operating system. You can learn more about this similarities and differences here.

My goal in this exercise was to deploy a custom image quickly and write a quick script in a file to accomplish a task. The task in this case was to tell the time of day a container was deployed. Then we will store everything in AWS S3.

When you first install Docker & run the application on your machine, you will be offered the opportunity to run a 2-minute tutorial (Image 1). This is recommended if you are new to Docker, but not a pre-requisite for the exercise of this Medium article.

Image 1: Docker’s Introductory Tutorial

I chose to work with an Nginx image, which can be found and pulled from Docker Hub here (Image 2). The Pull command is docker pull nginx.

Image 2: Copying the Pull Command for Nginx

I then executed the command in my Terminal (Image 3).

Image 3: Pulling Nginx Image

Again, our goal was to create a custom Nginx image, so we needed a different file for the webpage. Therefore in Image 4, I used file editor Atom to make a Dockerfile commanding Docker to use the latest Nginx image (“FROM”), remove the default (“RUN rm”), and copy the htmlcontent.html file to Nginx’s html folder (“COPY”).

Image 4: Text Editing

We then needed to build an image from the docker file. To do this I used the command docker build -t <image-name> . where the image name is unique (Image 5). Note, you must include “.” at the end of the command.

Image 5: Building the Dockerfile

At this point I pulled the Nginx image, customized it, and built the file htmlcontent.html, so next I needed to needed to add the date & time function to the .html file. (Remember, the original goal was to tell the time of day a container was deployed). I then save the content from Image 6 to the file czartimedate.

Image 6: HTML script

Now it was time to deploy the container. I ran the below command to do so. Note I used “-p 8080:80” to ensure port 8080 is open (Image 7). More of my options used can be found here.

docker run -it — rm -d -p 8080:80 — name nginxtimeserver czartimedate

Image 7: Docker Run

I then used the command docker ps to confirm that the container was deployed, which it was. (Image 8)

Image 8: Confirming Container Deployment

At this point my remaining goal was to save the container data in an S3 Bucket. First, I ensured my three files so far were in an “Nginx” directory, then I compressed those files in preparation of managing them in AWS. (Image 9)

Image 9: Compressing in preparation for S3

In the AWS Console I made an S3 Bucket “nginxtime” (Image 10).

Image 10: Confirming Bucket in AWS Console

I then uploaded the .tar file to the S3 bucket using the command aws s3 cp (Image 11). For good measure, I followed this command with another command, aws s3 ls, to confirm that nginxtime was seen through the CLI.

Image 11: Managing S3 in the CLI

I confirmed that my .tar file was an object in the bucket (Image 12).

Image 12: Confirming .tar file is in S3 bucket

The date and time of the container deployment are registered (Image 13), and also this can be done with the command docker logs — timestamps [Container ID] because Docker conveniently and automatically creates event logs.

Image 13

Also our data is, thanks to Step 4, in an S3 bucket. For more on how Docker can be used in an AWS environment specifically, you can review AWS’ rundown of Docker.

Thanks for reading.

Cloud & DevOps. Also Blockchain.