This project demonstrates deploying a simple webpage using the official Apache HTTP server Docker image (httpd). The task involves creating a directory, writing an index.html file, and running a container to serve the webpage. We'll map the container's HTTP port to a port on the host system for easy access.
- First, create a directory to organize the project files:
mkdir suraj
cd suraj
- Create an index.html file with basic HTML content:
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
</head>
<body>
<h1>Welcome to My Suraj Page</h1>
<p> This is a simple HTML page served by Apache server inside a Docker container</p>
</body>
</html>
- Run the container and map your index.html file to the container's web root:
docker run -d -p 8200:80 --name my-httpd-container -v $(pwd)/index.html:/usr/local/apache2/htdocs/index.html httpd:latest
-
-d: Runs the container in detached mode.
-
-p 8200:80: Maps port 8200 on the host to port 80 in the container.
-
--name my-httpd-container: Names the container my-httpd-container.
-
-v $(pwd)/index.html:/usr/local/apache2/htdocs/index.html: Mounts the local index.html file to the container's web server document root.
-
httpd:latest: Uses the latest version of the official Apache HTTP server image.
- Alternatively, copy the index.html file into a running container:
docker run -d -p 8200:80 --name my-httpd-container httpd:latest
docker cp index.html my-httpd-container:/usr/local/apache2/htdocs/index.html
Once the container is running:
- Open a browser and navigate to http://localhost:8200.
- You should see the content of the index.html file: "Welcome to Suraj's Page".
-
Why Use the Volume Mount (-v)?
- Changes made to the local index.html file will immediately reflect on the container, making it easier for testing and development.
-
Why Use docker cp?
- This is useful when a container is already running, and you want to copy files into it without restarting.
-
Apache HTTP Server (httpd) in Docker:
- httpd is a lightweight and efficient web server image provided officially on Docker Hub.
- The web root for httpd in the container is /usr/local/apache2/htdocs.
-
Stopping and Removing the Container:
- To stop the container:
docker stop my-httpd-container
- To remove the container:
docker rm my-httpd-container
- Docker Fundamentals:
- Running containers using the docker run command.
- Mapping host ports to container ports for web accessibility.
- Using volume mounts (-v) and docker cp to transfer files.
- Serving Static Content:
- Using the httpd image to serve HTML files.
- Container Management:
- Starting, stopping, and managing Docker containers.
👨💻 𝓒𝓻𝓪𝓯𝓽𝓮𝓭 𝓫𝔂: Suraj Kumar Choudhary | 📩 𝓕𝓮𝓮𝓵 𝓯𝓻𝓮𝓮 𝓽𝓸 𝓓𝓜 𝓯𝓸𝓻 𝓪𝓷𝔂 𝓱𝓮𝓵𝓹: [email protected]