Docker Install Apache

Docker Install Apache

Method#1: docker pull httpd

Find the httpd image on Docker Hub:

Docker Install Apache

You can check other versions of httpd by Sort by, the default is the latest version httpd:latest.

In addition, we can also use the docker search httpd command to see the available versions.

apidemos@apidemos:~/apache$ docker search httpd
NAME                           DESCRIPTION                  STARS  OFFICIAL AUTOMATED
httpd                          The Apache HTTP Server ..    524     [OK]       
centos/httpd                                                7                [OK]
rgielen/httpd-image-php5       Docker image for Apache...   1                [OK]
microwebapps/httpd-frontend    Httpd frontend allowing...   1                [OK]
lolhens/httpd                  Apache httpd 2 Server        1                [OK]
publici/httpd                  httpd:latest                 0                [OK]
publicisworldwide/httpd        The Apache httpd webser...   0                [OK]
rgielen/httpd-image-simple     Docker image for simple...   0                [OK]
solsson/httpd                  Derivatives of the offi...   0                [OK]
rgielen/httpd-image-drush      Apache HTTPD + Drupal S...   0                [OK]
learninglayers/httpd                                        0                [OK]
sohrabkhan/httpd               Docker httpd + php5.6 (...   0                [OK]
aintohvri/docker-httpd         Apache HTTPD Docker ext...   0                [OK]
alizarion/httpd                httpd on centos with mo...   0                [OK]

Here we pull the official mirror:

apidemos@apidemos:~/apache$ docker pull httpd

After waiting for the download to complete, we can check the local mirror list for the image with REPOSITORY as httpd.

apidemos@apidemos:~/apache$ docker images httpd
REPOSITORY     TAG        IMAGE ID        CREATED           SIZE
httpd          latest     da1536b4ef14    23 seconds ago    195.1 MB

Method#2: Build via Dockerfile

Create Dockerfile

First, create the directory apache, which will be used to store related stuff later.

apidemos@apidemos:~$ mkdir -p ~/apache/www ~/apache/logs ~/apache/conf 

The www directory will be mapped to the apache container configuration application directory.

The logs directory will be mapped to the apache container’s log directory.

The configuration files in the conf directory will be mapped to the configuration files of the apache container.

Go to the created apache directory and create the Dockerfile.

FROM debian:jessie

# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
#RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data

ENV HTTPD_PREFIX /usr/local/apache2
RUN mkdir -p "HTTPD_PREFIX" \
    && chown www-data:www-data "HTTPD_PREFIX"

# install httpd runtime dependencies
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libapr1 \
        libaprutil1 \
        libaprutil1-ldap \
        libapr1-dev \
        libaprutil1-dev \
        libpcre++0 \
        libssl1.0.0 \
    && rm -r /var/lib/apt/lists/*


RUN buildDeps=' \
        ca-certificates \
        curl \
        bzip2 \
        gcc \
        libpcre++-dev \
        libssl-dev \
        make \
    ' \
    set -x \
    && apt-get update \
    && apt-get install -y --no-install-recommends buildDeps \
    && rm -r /var/lib/apt/lists/* \
    && curl -fSL "HTTPD_BZ2_URL" -o httpd.tar.bz2 \
    && curl -fSL "HTTPD_BZ2_URL.asc" -o httpd.tar.bz2.asc \
# see
    && export GNUPGHOME="(mktemp -d)" \
    && gpg --keyserver --recv-keys A93D62ECC3C8EA12DB220EC934EA76E6791485A8 \
    && gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2 \
    && rm -r "GNUPGHOME" httpd.tar.bz2.asc \
    && mkdir -p src \
    && tar -xvf httpd.tar.bz2 -C src --strip-components=1 \
    && rm httpd.tar.bz2 \
    && cd src \
    && ./configure \
        --prefix="HTTPD_PREFIX" \
        --enable-mods-shared=reallyall \
    && make -j"(nproc)" \
    && make install \
    && cd .. \
    && rm -r src \
    && sed -ri \
        -e 's!^(\s*CustomLog)\s+\S+!\1 /proc/self/fd/1!g' \
        -e 's!^(\s*ErrorLog)\s+\S+!\1 /proc/self/fd/2!g' \
        "HTTPD_PREFIX/conf/httpd.conf" \
    && apt-get purge -y --auto-remove $buildDeps

COPY httpd-foreground /usr/local/bin/

CMD ["httpd-foreground"]

COPY httpd-foreground /usr/local/bin/ in the Dockerfile file is to copy httpd-foreground from the current directory into the image as the httpd service startup script, so we have to create a script file httpd-foreground locally.

set -e

# Apache gets grumpy about PID files pre-existing
rm -f /usr/local/apache2/logs/

exec httpd -DFOREGROUND

Grant executable permissions to the httpd-foreground file.

apidemos@apidemos:~/apache$ chmod +x httpd-foreground

Create an image via Dockerfile and replace it with your own name.

apidemos@apidemos:~/apache$ docker build -t httpd .

Once created, we can find the image we just created in the local list of mirrors.

apidemos@apidemos:~/apache$ docker images httpd
REPOSITORY     TAG        IMAGE ID        CREATED           SIZE
httpd          latest     da1536b4ef14    23 seconds ago    195.1 MB

Use apache image

Run the container

docker run -p 80:80 -v PWD/www/:/usr/local/apache2/htdocs/ -vPWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf -v $PWD/logs/:/usr/local/apache2/logs/ -d httpd

Command Description.

-p 80:80: The first port 80 is the host port, the next one is the container port, the effect is to map the container’s port 80 to the host’s port 80.

-v $PWD/www/:/usr/local/apache2/htdocs/: Mount the www directory in the current directory of the host to /usr/local/apache2/htdocs/ of the container.

-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf: Mount the conf/httpd.conf file from the current directory in the host to the container’s /usr/local/apache2/conf/httpd.conf.

-v $PWD/logs/:/usr/local/apache2/logs/: Mount the logs directory in the current directory of the host to /usr/local/apache2/logs/ of the container.

For more detailed commands, please refer to: Docker run command

To view the container startup status.

apidemos@apidemos:~/apache$ docker ps
CONTAINER ID  IMAGE   COMMAND             ... PORTS               NAMES
79a97f2aac37  httpd   "httpd-foreground"  ...>80/tcp  sharp_swanson