In the modern cloud-native landscape, containerization has emerged as the go-to approach for deploying and managing applications. Docker, the industry-leading containerization platform, provides a consistent and isolated environment for your applications, ensuring portability and scalability across different environments.
In this comprehensive blog post, we'll delve into the process of hosting a static website using an Apache HTTPD web server running inside a Docker container on the Amazon Web Services (AWS) cloud platform.
Step 1: Installing Docker on AWS Amazon Linux
sudo yum install -y docker
Once installed, start the Docker service:
sudo systemctl start docker
Step 2: Creating a Dockerfile
Create a file named "Dockerfile" (without a file extension). This file defines the steps to build your Docker image.
FROM centos:7
RUN yum update -y && yum install -y httpd
COPY index.html /var/www/html/
EXPOSE 80
ENTRYPOINT ["/usr/sbin/httpd"]
CMD ["-D", "FOREGROUND"]
Breakdown of the Dockerfile:
FROM: Specifies the base image (CentOS:7 or httpd:latest)
RUN: Updates the package list and installs the Apache web server (httpd) (CentOS option only)
COPY: Copies your website files (index.html in this example) to the Apache document root directory (/var/www/html/)
EXPOSE: Exposes port 80 for HTTP traffic
ENTRYPOINT: Defines the default command to run when the container starts.
CMD: Sets the argument for httpd which can be overwritten
Step 3: Building the Docker Image
Navigate to the directory containing the Dockerfile in your terminal and run the following command to build the Docker image:
docker build -t apache_webserver_docker .
This command builds the image using the Dockerfile and tags it with the name "apache_webserver_docker".
Step 4: Running the Apache Web Server Container
After building the image, run a container based on it using the following command:
docker run -dit -p 8081:80 --name http_web apache_webserver_docker:latest
Explanation of the Run Command:
-d: Runs the container in detached mode (background)
-p 8081:80: Maps port 80 from the container to port 8081 on the host machine
--name http_web: Assigns a name to the container for easier management
apache_webserver_docker: latest: Specifies the image to use and its latest version
Step 5: Testing the Apache Web Server
There are two ways to verify your web server is running:
- Using curl:
curl http://<host_ip>:8081/index.html
Replace <host_ip>
with the IP address of your machine.
- Using a web browser:
Open a web browser and visit http://<host_ip_address:8081/index.html>
(if running on a remote server). You should see your website content displayed.
Step 6: Pushing the Image to Docker Hub
Pushing your Docker image to Docker Hub allows you to share it with others or store it for future use. Here's a detailed breakdown of the process:
Create a Docker Hub Account: If you don't have one already, head to
https://www.docker.com/
and sign up for a free account.Login to Docker Hub: Once you have an account, log in to Docker Hub using the following command in your terminal:
docker login
You'll be prompted to enter yourusername
& password
.
- Tag your Image for Docker Hub: By default, your image is named with a tag like "<your_username>/apache_webserver_docker: latest". If you want to push it to a different location on Docker Hub, you can tag it with a new name using the following command:
docker tag apache_webserver_docker <your_username>/my-apache-webserver:latest # Replace with your username and desired name\
- Push the Image to Docker Hub: Finally, push the image to Docker Hub using the following command:
docker push <your_username>/my-apache-webserver:latest
Benefits of Pushing to Docker Hub:
Sharing: Share your image with collaborators or the wider community for easy access and deployment.
Version Control: Push different tagged versions of your image to track changes and maintain a history.
Centralized Storage: Store your images in a secure and reliable location for future use across different environments.
Using a Pushed Image:
Once you've pushed your image, anyone with access can pull and run it using the following command:
docker run -dit -p 8081:80 --name http_web <your_username>/my-apache-webserver:latest
This command retrieves the image named <your_username>/my-apache-webserver:latest
from Docker Hub and runs a container based on it.
Conclusion
Using ENTRYPOINT and CMD instructions, you can efficiently deploy Apache web servers with a clean Dockerfile. And, also further push it to the docker hub repository to use further. This approach promotes isolation, portability, and scalability for your web applications.
With the power of Docker and AWS at your fingertips, you can focus on delivering an exceptional user experience for your static website while leaving the infrastructure management to the robust and reliable services provided by AWS. Whether you're hosting a personal blog, a portfolio site, or a small business website, this approach offers a streamlined and efficient solution for bringing your static content to the world.