Cloud Native 23 min read

Master Dockerfile: Build, Run, and Publish Custom HTTPD Images with Podman

This guide explains how to write Dockerfiles, use key instructions such as FROM, LABEL, RUN, CMD, EXPOSE, and others, and demonstrates step‑by‑step building of HTTPD images on both CentOS and Alpine, testing containers, and publishing them to Docker Hub with Podman.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Dockerfile: Build, Run, and Publish Custom HTTPD Images with Podman

Basic Structure

A Dockerfile is a plain‑text configuration file that lets users quickly create custom images. It consists of line‑by‑line commands and supports comments starting with #. A Dockerfile typically includes four parts: base image information, maintainer information, image operation instructions, and the default command executed when the container starts.

# First line must specify the base image
FROM centos
# Maintainer information
LABEL MANTAINER "lvnanhai66 [email protected]"
# Image operation instruction
RUN useradd -r -M -s /sbin/nologin apache
# Default command when the container starts
CMD ["/usr/local/apache/bin/httpd","-D","FOREGROUND"]

The FROM instruction must be the first line and defines the base image. LABEL MANTAINER sets the maintainer. RUN executes a command during image build, creating a new layer each time. CMD specifies the command that runs when the container starts; only the last CMD in a Dockerfile is effective.

Dockerfile Instructions

Common instructions include:

FROM : specify the base image (e.g., FROM centos or FROM scratch).

LABEL MAINTAINER : set maintainer metadata.

RUN : execute commands during build; two forms are supported: RUN command (shell) and RUN ["executable","param1","param2"] (exec).

CMD : define the default runtime command; three formats exist – exec JSON array, shell form, or default arguments for ENTRYPOINT.

EXPOSE : declare ports the container will listen on (e.g., EXPOSE 80 443).

ENV : set environment variables that persist in later layers and at runtime.

ADD : copy files, URLs, or tar archives into the image.

COPY : copy files/directories from the build context into the image.

ENTRYPOINT : configure the container’s entry point; only one ENTRYPOINT is effective.

VOLUME : create a mount point for persistent data.

USER : specify the user (or UID) under which subsequent commands run.

WORKDIR : set the working directory for RUN, CMD, ENTRYPOINT, COPY, and ADD.

ONBUILD : define instructions that are triggered when the image is used as a base for another build.

Creating an Image (CentOS version)

The following steps illustrate building an HTTPD image on CentOS using Podman, controlling the Apache version, and employing most Dockerfile instructions.

# mkdir httpd && cd httpd
# touch Dockerfile && mkdir 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/#//g' /usr/local/apache/conf/httpd.conf
exec \"$@\"" > files/entrypoint.sh
# chmod +x files/entrypoint.sh

Dockerfile (CentOS):

FROM centos
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 && \
    yum install -y 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, test, and push the image:

# podman build -t httpd:v3.0 .
# podman run -d httpd:v3.0
# podman ps
# podman inspect -l | grep IPAddress
# podman tag httpd:v3.0 docker.io/lvnanhai66/httpd:v3.0
# podman push docker.io/lvnanhai66/httpd:v3.0

Creating an Image (Alpine version)

The Alpine variant aims for a small footprint (≈70 MB) while using the same set of Dockerfile instructions.

# mkdir httpd && cd httpd
# touch Dockerfile && mkdir 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/#//g' /usr/local/apache/conf/httpd.conf
exec \"$@\"" > files/entrypoint.sh
# chmod +x files/entrypoint.sh

Dockerfile (Alpine):

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 verify the Alpine image:

# podman build -t httpd:v6.0 .
# podman run -d --name web -p 80:80 -v /srv/web/:/usr/local/apache/htdocs/:Z httpd:v6.0
# curl $(podman inspect -l | grep -i IPAddress | head -n1 | cut -d '"' -f4)

Finally, the image is saved, transferred, and loaded into a Docker environment, then pushed to Docker Hub:

# podman save -o myhttpd.tar docker.io/lvnanhai66/httpd:v6.0
# scp myhttpd.tar remote-host:/root
# docker load -i myhttpd.tar
# docker push lvnanhai66/httpd:v6.0

The resulting image size is around 115 MB, considerably smaller than the original CentOS‑based image, and can be pulled and run directly with Docker.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdLinuxContainerDockerfilePodmanImage Buildinghttpd
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.