Skip to main content
In this tutorial, you’ll learn how to build a lightweight, custom Docker image for an HTTPD (Apache) web server on CentOS 7. We’ll cover:
  • Setting up the build context
  • Crafting an optimized Dockerfile
  • Adding a simple index.html
  • Building, testing, and pushing your image to Docker Hub
By following these steps, you’ll gain hands-on experience with multi-layered Docker images and best practices for containerized web servers.

1. Prepare the Build Context

First, create an isolated directory for all build artifacts. This ensures that nothing outside the folder is accidentally added to your image.
Everything in this directory (including subdirectories) is sent to the Docker daemon during the build. Keep it lean to speed up image creation.

2. Write the Dockerfile

Create a file named Dockerfile:
Populate it with the following content:
Running containers as root can pose risks. For production workloads, consider adding a non-root user and switching with USER.
For more details on Dockerfile syntax, see the Dockerfile reference.

3. Create the HTML Page

Add a simple index.html in the same directory:
Example content:
Verify both files are present:

4. Build the Docker Image

Use a clear, versioned tag for your image:
You should see output for each of the five build steps. Once complete, confirm the image exists:
Inspect the image layers:

5. Test the Container

Run a container, mapping host port 82 to container port 80:
Open your browser to http://<host_ip>:82. You should see your custom page:
The image shows a web browser displaying a page with the text "Hello from KodeKloud Again" on a plain white background. The URL in the address bar is "52.90.207.2:82".

6. Push to Docker Hub

Authenticate and push your image so others can pull it:
Verify on Docker Hub that your repository is public and the tag is available.

Quick Command Reference


Watch Video