Docker 安裝 Apache

方法一、docker pull httpd

查詢 Docker Hub 上的 httpd 映象:

可以通過 Sort by 檢視其他版本的 httpd,預設是最新版本 httpd:latest

此外,我們還可以用 docker search httpd 命令來檢視可用版本:

[email protected]:~/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]
...

這裡我們拉取官方的映象

[email protected]:~/apache$ docker pull httpd

等待下載完成後,我們就可以在本地映象列表裡查到REPOSITORY為httpd的映象。

[email protected]:~/apache$ docker images httpd
REPOSITORY     TAG        IMAGE ID        CREATED           SIZE
httpd          latest     da1536b4ef14    23 seconds ago    195.1 MB

方法二、通過 Dockerfile 構建

建立 Dockerfile

首先,建立目錄apache,用於存放後面的相關東西。

[email protected]:~$ mkdir -p  ~/apache/www ~/apache/logs ~/apache/conf 

www 目錄將對映為 apache 容器配置的應用程式目錄。

logs 目錄將對映為 apache 容器的日誌目錄。

conf 目錄裡的配置檔案將對映為 apache 容器的配置檔案。

進入建立的 apache 目錄,建立 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
ENV PATH $PATH:$HTTPD_PREFIX/bin
RUN mkdir -p "$HTTPD_PREFIX" \
    && chown www-data:www-data "$HTTPD_PREFIX"
WORKDIR $HTTPD_PREFIX

# install httpd runtime dependencies
# https://httpd.apache.org/docs/2.4/install.html#requirements
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/*

ENV HTTPD_VERSION 2.4.20
ENV HTTPD_BZ2_URL https://www.apache.org/dist/httpd/httpd-$HTTPD_VERSION.tar.bz2

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 https://httpd.apache.org/download.cgi#verify
    && export GNUPGHOME="$(mktemp -d)" \
    && gpg --keyserver ha.pool.sks-keyservers.net --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/

EXPOSE 80
CMD ["httpd-foreground"]

Dockerfile檔案中 COPY httpd-foreground /usr/local/bin/ 是將當前目錄下的httpd-foreground拷貝到映象裡,作為httpd服務的啟動指令碼,所以我們要在本地建立一個指令碼檔案httpd-foreground

#!/bin/bash
set -e

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

exec httpd -DFOREGROUND

賦予 httpd-foreground 檔案可執行許可權。

[email protected]:~/apache$ chmod +x httpd-foreground

通過 Dockerfile 建立一個映象,替換成你自己的名字。

[email protected]:~/apache$ docker build -t httpd .

建立完成後,我們可以在本地的映象列表裡查詢到剛剛建立的映象。

[email protected]:~/apache$ docker images httpd
REPOSITORY     TAG        IMAGE ID        CREATED           SIZE
httpd          latest     da1536b4ef14    23 seconds ago    195.1 MB

使用 apache 映象

執行容器

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

命令說明:

-p 80:80: 將容器的 80 埠對映到主機的 80 埠。

-v $PWD/www/:/usr/local/apache2/htdocs/: 將主機中當前目錄下的 www 目錄掛載到容器的 /usr/local/apache2/htdocs/。

-v $PWD/conf/httpd.conf:/usr/local/apache2/conf/httpd.conf: 將主機中當前目錄下的 conf/httpd.conf 檔案掛載到容器的 /usr/local/apache2/conf/httpd.conf。

-v $PWD/logs/:/usr/local/apache2/logs/: 將主機中當前目錄下的 logs 目錄掛載到容器的 /usr/local/apache2/logs/。

檢視容器啟動情況:

[email protected]:~/apache$ docker ps
CONTAINER ID  IMAGE   COMMAND             ... PORTS               NAMES
79a97f2aac37  httpd   "httpd-foreground"  ... 0.0.0.0:80->80/tcp  sharp_swanson

通過瀏覽器訪問