方法一、docker pull httpd
查找 上的 httpd 镜像:
可以通过 sort by 查看其他版本的 httpd,默认是最新版本 httpd:latest。
此外,我们还可以用 docker search httpd 命令来查看可用版本:
coonote@coonote:~/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] ...
这里我们拉取官方的镜像
coonote@coonote:~/apache$ docker pull httpd
等待下载完成后,我们就可以在本地镜像列表里查到repository为httpd的镜像。
coonote@coonote:~/apache$ docker images httpd repository tag image id created size httpd latest da1536b4ef14 23 seconds ago 195.1 mb
方法二、通过 dockerfile 构建
创建 dockerfile
首先,创建目录apache,用于存放后面的相关东西。
coonote@coonote:~$ 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 文件可执行权限。
coonote@coonote:~/apache$ chmod x httpd-foreground
通过 dockerfile 创建一个镜像,替换成你自己的名字。
coonote@coonote:~/apache$ docker build -t httpd .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像。
coonote@coonote:~/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/。
查看容器启动情况:
coonote@coonote:~/apache$ docker ps container id image command ... ports names 79a97f2aac37 httpd "httpd-foreground" ... 0.0.0.0:80->80/tcp sharp_swanson
通过浏览器访问
it works! https://www.coonote.com