Hosting a static portfolio website in AWS using  EC2 instance and a docker container as well as creating multi-stage distroless docker image

Hosting a static portfolio website in AWS using EC2 instance and a docker container as well as creating multi-stage distroless docker image

Creating a new EC2 instance

Connecting to the instance

Installing Docker

Installing apache server

Installing Git

Cloning my portfolio website

https://github.com/Rupak-Shrestha/portfolio.git

Copying the website files to /var/www/html

To copy everything from the current directory to a destination directory in Linux, you can use the cp command with the -r option which stands for "recursive".

Here's the syntax:

cp -r * /path/to/destination

In this command, the * wildcard symbol specifies that all files and directories in the current directory should be copied. The /path/to/destination is the destination directory where the files and directories will be copied to.

Restart the httpd service

Checking the website with the public IP of EC2 instance

Containerizing the website

Creating a Dockerfile inside the portfolio directory

Writing a Dockerfile

In this Dockerfile, we start with a lightweight base image nginx:alpine that already has the Nginx web server installed.

Next, we copy the website files from the current directory (where the Dockerfile is located) to the appropriate directory for Nginx (/usr/share/nginx/html).

Then, we expose port 80 to allow incoming web traffic to access the site.

Finally, we start the Nginx server with the CMD command, specifying that it should run in the foreground with the daemon off option.

Building an image from the Dockerfile

Creating a new bridge network

Running a conatiner

  • docker run: This command tells Docker to run a container.

  • -d: This option runs the container in detached mode, which means that the container will run in the background and won't attach to the current terminal session.

  • --network my-bridge-network: This option specifies the network that the container should connect to. In this case, the container is connected to the my-bridge-network network, which is a user-defined network.

  • -p 80:80: This option maps port 80 of the container to port 80 of the host machine. This means that the website running in the container will be accessible at publicIPaddress:80 on the host machine.

  • --name portfolio-container: This option gives a name to the container. In this case, the container is named portfolio-container.

  • my-portfolio: This is the name of the Docker image that the container is based on.

Checking the website

Writing a multi-stage distroless image

This is a multi-stage Dockerfile that creates a Docker image for a static website using the nginx web server and the distroless base image.

The Dockerfile has two stages:

  1. Build stage: This stage starts with the nginx:alpine image as the base image and sets it as build stage. It copies the content of the current directory (which contains the website files) to the /usr/share/nginx/html directory inside the container. This stage is only used for building the website files and it produces an intermediate image that contains the built website files.

  2. Production stage: This stage starts with the gcr.io/distroless/base image, which is a minimal image that includes only the necessary libraries to run the application. The COPY command with the --from option is used to copy the website files from the build stage to the appropriate directory in the production stage. In this case, the files are copied from /usr/share/nginx/html in the build stage to the same location in the production stage. The EXPOSE instruction is used to indicate that the container will listen on port 80 for incoming web traffic. Finally, the CMD instruction is used to start the nginx web server in the foreground with the daemon off option.

Building multi-stage distroless image

Comparing the image size

By using a multi-stage Dockerfile, we are able to create a smaller, more secure production image by only including the necessary files and libraries needed to run the application. The build stage is used to create the necessary website files, while the production stage is used to create the final Docker image that runs the website using the nginx web server.

Thank you for reading! Happy Learning!