How to Build Custom Apache Docker Images on RockyLinux and Alpine with Podman
This guide explains Dockerfile fundamentals, enumerates all major instructions, and provides step‑by‑step examples for creating minimal Apache (httpd) images on both RockyLinux and Alpine using Podman, including building, testing, tagging, and pushing to Docker Hub.
Dockerfile Basics
A Dockerfile is a plain‑text configuration file that defines how to assemble a Docker image. It consists of a series of instruction lines, optionally preceded by comments starting with #. A typical Dockerfile includes four parts: base image information, maintainer metadata, image build commands, and the default command executed when a container starts.
Key Dockerfile Instructions
FROM – specifies the base image and must be the first instruction.
LABEL MAINTAINER – records maintainer contact information.
RUN – executes commands during image build, creating a new layer for each line.
CMD – defines the default command when the container runs (only one CMD is effective).
EXPOSE – declares which ports the container will listen on.
ENV – sets environment variables for later RUN steps and at runtime.
ADD – copies files or URLs into the image, automatically extracting tar archives.
COPY – copies files from the build context into the image (preferred for local files).
ENTRYPOINT – configures the container’s entry point; unlike CMD it cannot be overridden by docker run arguments.
VOLUME – creates a mount point for persistent data.
USER – sets the user (UID/GID) under which subsequent commands run.
WORKDIR – sets the working directory for RUN, CMD, ENTRYPOINT, COPY, and ADD.
ONBUILD – registers instructions that will be executed when the image is used as a base for another build.
Building an Apache Image on RockyLinux
First, create a project directory and download the required source tarballs.
# mkdir httpd && cd httpd
# touch Dockerfile
# mkdir files
# cd files
# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz \
https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz \
https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
# echo -e "#!/bin/bash
sed -i '/^#ServerName/s/#//' /usr/local/apache/conf/httpd.conf
exec \"$@\"" > entrypoint.sh
# chmod +x entrypoint.shThen write the Dockerfile:
FROM rockylinux
LABEL MANTAINER "lvnanhai66 [email protected]"
ENV apache_version 2.4.54
ENV PATH /usr/local/apache/bin:$PATH
ADD files/apr-1.7.0.tar.gz /usr/src/
ADD files/apr-util-1.6.1.tar.gz /usr/src/
ADD files/httpd-${apache_version}.tar.gz /usr/src/
ADD files/entrypoint.sh /
RUN useradd -r -M -s /sbin/nologin apache && \
cd /etc/yum.repos.d && rm -rf * && \
curl -o CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo && \
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' CentOS-Base.repo && \
yum clean all && yum makecache && \
yum -y install gcc gcc-c++ make openssl-devel pcre-devel expat-devel libtool && \
cd /usr/src/apr-1.7.0 && ./configure --prefix=/usr/local/apr && make && make install && \
cd /usr/src/apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install && \
cd /usr/src/httpd-${apache_version} && \
./configure --prefix=/usr/local/apache \
--enable-so --enable-ssl --enable-cgi --enable-rewrite \
--with-zlib --with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && \
make && make install && \
yum clean all && yum -y remove gcc gcc-c++ make && \
rm -rf /tmp/* /usr/src/*
EXPOSE 80
WORKDIR /usr/local/apache
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]
ENTRYPOINT ["/bin/bash","/entrypoint.sh"]Build and test the image with Podman:
# podman build -t httpd:1.0 .
# podman run -d --name web -p 80:80 httpd:1.0
# curl localhost:80 # should return the default Apache test pageTag and push the image to Docker Hub:
# podman tag httpd:1.0 docker.io/lvnanhai66/httpd:1.0
# podman login docker.io # provide Docker Hub credentials
# podman push docker.io/lvnanhai66/httpd:1.0Building a Minimal Alpine‑Based Apache Image
The Alpine variant aims for a small footprint (≈70 MB). The steps are analogous but use apk for package management.
# mkdir httpd && cd httpd
# touch Dockerfile && mkdir files && cd files
# wget https://downloads.apache.org/apr/apr-1.7.0.tar.gz \
https://downloads.apache.org/apr/apr-util-1.6.1.tar.gz \
https://downloads.apache.org/httpd/httpd-2.4.54.tar.gz
# echo -e "#!/bin/bash
sed -i '/^#ServerName/s/#//' /usr/local/apache/conf/httpd.conf
exec \"$@\"" > entrypoint.sh
# chmod +x entrypoint.sh
# cd ..Alpine Dockerfile:
FROM alpine
LABEL MANTAINER "lvnanhai66 [email protected]"
ENV apr_version=1.7.0 apr_util_version=1.6.1 httpd_version=2.4.54
ADD files/* /tmp/
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
apk update && \
adduser -SHs /sbin/nologin apache && \
apk add --no-cache gcc libc-dev make expat-dev pcre-dev openssl-dev libtool && \
cd /tmp/apr-${apr_version} && ./configure --prefix=/usr/local/apr && make && make install && \
cd /tmp/apr-util-${apr_util_version} && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install && \
cd /tmp/httpd-${httpd_version} && \
./configure --prefix=/usr/local/apache \
--enable-so --enable-ssl --enable-cgi --enable-rewrite \
--with-zlib --with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
--enable-modules=most --enable-mpms-shared=all --with-mpm=prefork && \
make && make install && \
mv /tmp/entrypoint.sh / && \
apk del gcc make && rm -rf /tmp/* /var/cache/*
EXPOSE 80
WORKDIR /usr/local/apache
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]
ENTRYPOINT ["/bin/sh","/entrypoint.sh"]Build, run, and push the Alpine image:
# podman build -t httpd:alpine .
# podman run -d -p 80:80 httpd:alpine
# podman tag httpd:alpine docker.io/lvnanhai66/httpd:alpine
# podman push docker.io/lvnanhai66/httpd:alpineBoth images have been verified by pulling them on a clean host, running a container, and confirming that the default Apache page or a custom test page is served correctly.
Full-Stack DevOps & Kubernetes
Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
