课程表

Docker 安装和使用

Docker 实例

Docker 参考手册

工具箱
速查手册

Docker 安装 Apache

当前位置:免费教程 » 程序设计 » Docker

方法一、通过 Dockerfile构建

创建Dockerfile

首先,创建目录apache,用于存放后面的相关东西。

  1. w3xue@w3xue:~$ mkdir -p ~/apache/www ~/apache/logs ~/apache/conf

www目录将映射为apache容器配置的应用程序目录

logs目录将映射为apache容器的日志目录

conf目录里的配置文件将映射为apache容器的配置文件

进入创建的apache目录,创建Dockerfile

  1. FROM debian:jessie
  2.  
  3. # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
  4. #RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data
  5.  
  6. ENV HTTPD_PREFIX /usr/local/apache2
  7. ENV PATH $PATH:$HTTPD_PREFIX/bin
  8. RUN mkdir -p "$HTTPD_PREFIX" && chown www-data:www-data "$HTTPD_PREFIX"
  9. WORKDIR $HTTPD_PREFIX
  10.  
  11. # install httpd runtime dependencies
  12. # https://httpd.apache.org/docs/2.4/install.html#requirements
  13. 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/*
  14.  
  15. ENV HTTPD_VERSION 2.4.20
  16. ENV HTTPD_BZ2_URL https://www.apache.org/dist/httpd/httpd-$HTTPD_VERSION.tar.bz2
  17.  
  18. 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
  19. && 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
  20.  
  21. COPY httpd-foreground /usr/local/bin/
  22.  
  23. EXPOSE 80
  24. CMD ["httpd-foreground"]

Dockerfile文件中 COPY httpd-foreground /usr/local/bin/ 是将当前目录下的httpd-foreground拷贝到镜像里,作为httpd服务的启动脚本,所以我们要在本地创建一个脚本文件httpd-foreground

  1. #!/bin/bash
  2. set -e
  3.  
  4. # Apache gets grumpy about PID files pre-existing
  5. rm -f /usr/local/apache2/logs/httpd.pid
  6.  
  7. exec httpd -DFOREGROUND

赋予httpd-foreground文件可执行权限

  1. w3xue@w3xue:~/apache$ chmod +x httpd-foreground

通过Dockerfile创建一个镜像,替换成你自己的名字

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

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

  1. w3xue@w3xue:~/apache$ docker images httpd
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. httpd latest da1536b4ef14 23 seconds ago 195.1 MB

方法二、docker pull httpd

查找Docker Hub上的httpd镜像

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

这里我们拉取官方的镜像

  1. w3xue@w3xue:~/apache$ docker pull httpd

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为httpd的镜像。


使用apache镜像

运行容器

  1. 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/

查看容器启动情况

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

通过浏览器访问

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号