In today’s fast-paced development environment, containerization has become a popular approach for deploying and managing applications. Docker, a leading containerization platform, allows developers to encapsulate applications and their dependencies into lightweight, portable containers. In this blog post, we will explore how to run an Apache web server on a Docker container using CentOS as the base image. This setup provides a flexible and scalable solution for hosting web applications.
Why Use CentOS as the Base Image?
CentOS is a popular Linux distribution known for its stability, security, and long-term support. Using CentOS as the base image for our Docker container ensures a reliable and secure foundation for running our Apache web server.
Setting Up Apache Web Server on Docker
Step 1: Installing Docker on AWS Amazon Linux
First, connecting to Amazon Linux instance using SSH. To install Docker on Amazon Linux, execute the following command. Once Docker is installed, start the Docker service.
Step 2: Creating a Dockerfile
To begin, create a file named “Dockerfile” without any file extension. This file will define the steps for building our Docker image. Open the Dockerfile in a text editor and add the following content.
RUN yum update -y && yum install -y httpd
COPY index.html /var/www/html/
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
In the above Dockerfile, specifying that your base image is the latest CentOS image available on Docker Hub. and then install Apache httpd webserver using the yum package manager. Next, you copy website files to the appropriate location in the container that is the Apache document root directory. These files will be served by the Apache web server. Finally, expose to port 80, which is the default port for HTTP traffic, and start the Apache web server when the container launches. The
-D FOREGROUND flag keeps the container running in the foreground, allowing us to see the Apache logs.
Step 3: Building the Docker Image
To build the Docker image, navigate to the directory containing the Dockerfile in the terminal or command prompt and run the following command. This command builds the Docker image using the Dockerfile and assigns it the tag apache_webserver_docker.
docker build -t apache_webserver_docker .
Step 4: Running the Apache Web Server Container
After successfully building the Docker image, run a container based on it with the following command.
docker run -dit -p 8081:80 --name http_web apache_webserver_docker:latest
This command starts a container in detached mode (
-d), maps port 80 from the container to port 8081 on the host (
-p 8081:80), and uses the
Step 5: Testing the Apache Web Server
Simply, one can check connectivity to the server by using curl command curl http://host_ip:8081/index.html or Open a web browser and visit or
http://<host_ip_address:8081/index.html> (if running Docker on a remote server) to test your Apache web server. You should see your website content being served by Apache.
Step 6: Pushing Image to Docker Hub
Running an Apache web server on a Docker container using CentOS provides a flexible and scalable solution for hosting web applications. By leveraging the benefits of containerization, such as isolation, portability, and easy deployment, developers can streamline their web server setup and focus more on developing and improving their applications.
In this post, we covered the steps involved in setting up Apache on a Docker container using CentOS as the base image. By following these steps, you can create a portable and self-contained environment for hosting your web applications. With Docker’s ease of use and CentOS’s reliability, you can ensure a robust and efficient web server setup for your development and production environments.
Thank you so much for Reading :)