Docker 装置 Apache


方式一、docker pull httpd

查找 快乐飞艇是犯法的吗:Docker Hub 上的 httpd 镜像:

能够经由过程 Sort by 检查其余版本的 httpd,默许是最新版本 httpd:latest

另外,咱们还能够用 docker search httpd 号令来检查可用版本:

jishudaxue@jishudaxue:~/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
...

这里咱们拉取官方的镜像

jishudaxue@jishudaxue:~/apache$ docker pull httpd

期待下载实现后,咱们就能够在本地镜像列表里查到REPOSITORY为httpd的镜像。

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

方式二、经由过程 Dockerfile 构建

建立 Dockerfile

起首,建立目次apache,用于寄存前面的相干工具。

jishudaxue@jishudaxue:~$ 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
# //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 //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 //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 文件可履行权限。

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

经由过程 Dockerfile 建立一个镜像,替代成你本身的名字。

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

建立实现后,咱们能够在本地的镜像列表里查找到方才建立的镜像。

jishudaxue@jishudaxue:~/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/。

检查容器启动环境:

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