《 Docker实例一Docker安装Apache实例 》
前言
在前面的文章中,已经完成的内容包括:Docker的基本理论概述、Docker服务器的安装、Docker常用命令的详细介绍(容器命令,镜像命令等)、Docker镜像的基本理论概述、Docker镜像的操作、Docker容器数据卷基本理论概述、容器卷的使用实战、数据卷容器的概述与实操、Dockerfile基本理论介绍、Dockerfile构建镜像过程介绍、Dockerfile指令大全介绍与综合实战,接下来将通过Docker常用的一些安装实例,更加深入的认识Docker,其简洁的,快速的构建应用环境等。
关于Docker镜像的获取方式分为两种:
一、即为 docker pull 镜像名(简单明了,一条命令搞定)。
二、即为通过 Dockerfile 构建Docker镜像(步骤较为复杂,需要定义Dockerfile文件,然后再根据文件进行构建镜像,所以在没有业务要求的限制,尽量采用第一种方式)。
Docker镜像获取总共分为以下几个步骤:
1、检索需要的镜像(获取特定TAG的镜像);
2、拉取镜像文件到本地;
3、列出本地所有的镜像;
4、使用镜像(启动镜像);
5、容器使用完毕(停止容器);
6、移除废的容器(移除前注意备份好数据,否则后果严重);
Docker实例一Docker安装Apache实例
第一种:使用 docker pull 镜像名 拉取镜像
1、查询星级数大于300的Apache镜像(注:星级数越高,越受欢迎的,默认在 Docker Hub 查询的),
输入命令 “ docker search -s 300 apache ” ,查询内容如下:
[root@localhost ~]# docker search -s 300 apache
Flag --stars has been deprecated, use --filter=stars=3 instead
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
httpd The Apache HTTP Server Project 2204 [OK]
tomcat Apache Tomcat is an open source implementati… 2197 [OK]
cassandra Apache Cassandra is an open-source distribut… 903 [OK]
maven Apache Maven is a software project managemen… 755 [OK]
solr Solr is the popular, blazing-fast, open sour… 613 [OK]
zookeeper Apache ZooKeeper is an open-source server wh… 532 [OK]
2、拉取第一个镜像,也是官方版的镜像,输入命令 “ docker pull httpd ”,默认拉取的为最新版本 latest ,内容如下:
[root@localhost ~]# docker pull httpd
Using default tag: latest
latest: Pulling from library/httpd
f17d81b4b692: Pull complete
06fe09255c64: Pull complete
0baf8127507d: Pull complete
07b9730387a3: Pull complete
6dbdee9d6fa5: Pull complete
Digest: sha256:90b34f4370518872de4ac1af696a90d982fe99b0f30c9be994964f49a6e2f421
Status: Downloaded newer image for httpd:latest
3、拉取完成后,输入命令 “ docker images httpd ”,可以在本地镜像列表里查到REPOSITORY为httpd的镜像,内容如下:
[root@localhost ~]# docker images httpd
REPOSITORY TAG IMAGE ID CREATED SIZE
httpd latest 55a118e2a010 5 weeks ago 132MB
第二种:通过 Dockerfile 构建Docker镜像
1、新建Apache的Dockerfile文件存放目录,输入命令 “ mkdir -p /opt/dockerfile/apache ” ,并进入到目录下。
[root@localhost ~]# mkdir -p /opt/dockerfile/apache
[root@localhost ~]# cd /opt/dockerfile/apache/
[root@localhost apache]#
2、创建dockerfile文件,输入命令 “ vi apachedockerfile ” ,并键入如下内容:
Ubuntu版本:
# Base 基础镜像
FROM debian:stretch-slim
# 首先添加我们的用户和组,以确保一致地分配他们的id,而不管添加了什么依赖项
#RUN groupadd -r www-data && useradd -r --create-home -g www-data www-data
# 配置Apache环境变量
ENV HTTPD_PREFIX /usr/local/apache2
ENV PATH $HTTPD_PREFIX/bin:$PATH
RUN mkdir -p "$HTTPD_PREFIX" \
&& chown www-data:www-data "$HTTPD_PREFIX"
WORKDIR $HTTPD_PREFIX
# 安装httpd运行时依赖项
# https://httpd.apache.org/docs/2.4/install.html#requirements
RUN set -eux; \
apt-get update; \
apt-get install -y --no-install-recommends \
libapr1-dev \
libaprutil1-dev \
libaprutil1-ldap \
; \
rm -rf /var/lib/apt/lists/*
ENV HTTPD_VERSION 2.4.37
ENV HTTPD_SHA256 3498dc5c6772fac2eb7307dc7963122ffe243b5e806e0be4fb51974ff759d726
# https://httpd.apache.org/security/vulnerabilities_24.html
ENV HTTPD_PATCHES=""
ENV APACHE_DIST_URLS \
# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
https://www.apache.org/dyn/closer.cgi?action=download&filename= \
# 如果版本已经过时(或者需要获取.asc文件),则必须从 dist/archive:/ 中拉取
https://www-us.apache.org/dist/ \
https://www.apache.org/dist/ \
https://archive.apache.org/dist/
# see https://httpd.apache.org/docs/2.4/install.html#requirements
RUN set -eux; \
\
# mod_http2 mod_lua mod_proxy_html mod_xml2enc 这儿不作过多解释,都是些配置文件
# https://anonscm.debian.org/cgit/pkg-apache/apache2.git/tree/debian/control?id=adb6f181257af28ee67af15fc49d2699a0080d4c
savedAptMark="$(apt-mark showmanual)"; \
apt-get update; \
apt-get install -y --no-install-recommends \
bzip2 \
ca-certificates \
dirmngr \
dpkg-dev \
gcc \
gnupg \
liblua5.2-dev \
libnghttp2-dev \
libpcre3-dev \
libssl-dev \
libxml2-dev \
make \
wget \
zlib1g-dev \
; \
rm -r /var/lib/apt/lists/*; \
\
ddist() { \
local f="$1"; shift; \
local distFile="$1"; shift; \
local success=; \
local distUrl=; \
for distUrl in $APACHE_DIST_URLS; do \
if wget -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \
success=1; \
break; \
fi; \
done; \
[ -n "$success" ]; \
}; \
\
ddist 'httpd.tar.bz2' "httpd/httpd-$HTTPD_VERSION.tar.bz2"; \
echo "$HTTPD_SHA256 *httpd.tar.bz2" | sha256sum -c -; \
\
# see https://httpd.apache.org/download.cgi#verify
ddist 'httpd.tar.bz2.asc' "httpd/httpd-$HTTPD_VERSION.tar.bz2.asc"; \
export GNUPGHOME="$(mktemp -d)"; \
for key in \
# gpg: key 791485A8: public key "Jim Jagielski (Release Signing Key) <jim@apache.org>" imported
A93D62ECC3C8EA12DB220EC934EA76E6791485A8 \
# gpg: key 995E35221AD84DFF: public key "Daniel Ruggeri (http://home.apache.org/~druggeri/) <druggeri@apache.org>" imported
B9E8213AEFB861AF35A41F2C995E35221AD84DFF \
; do \
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
done; \
gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2; \
command -v gpgconf && gpgconf --kill all || :; \
rm -rf "$GNUPGHOME" httpd.tar.bz2.asc; \
\
mkdir -p src; \
tar -xf httpd.tar.bz2 -C src --strip-components=1; \
rm httpd.tar.bz2; \
cd src; \
\
patches() { \
while [ "$#" -gt 0 ]; do \
local patchFile="$1"; shift; \
local patchSha256="$1"; shift; \
ddist "$patchFile" "httpd/patches/apply_to_$HTTPD_VERSION/$patchFile"; \
echo "$patchSha256 *$patchFile" | sha256sum -c -; \
patch -p0 < "$patchFile"; \
rm -f "$patchFile"; \
done; \
}; \
patches $HTTPD_PATCHES; \
\
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
./configure \
--build="$gnuArch" \
--prefix="$HTTPD_PREFIX" \
--enable-mods-shared=reallyall \
--enable-mpms-shared=all \
; \
make -j "$(nproc)"; \
make install; \
\
cd ..; \
rm -r src man manual; \
\
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-mark 的 “ manual ” 列表,以便 “purge --auto-remove” 将删除所有构建依赖项(节省内存开销,这个比较可以的!!!)
apt-mark auto '.*' > /dev/null; \
[ -z "$savedAptMark" ] || apt-mark manual $savedAptMark; \
find /usr/local -type f -executable -exec ldd '{}' ';' \
| awk '/=>/ { print $(NF-1) }' \
| sort -u \
| xargs -r dpkg-query --search \
| cut -d: -f1 \
| sort -u \
| xargs -r apt-mark manual \
; \
apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
\
# 测试是否可用
httpd -v
COPY httpd-foreground /usr/local/bin/
# 将80端口暴露出去(只要不冲突,可自定义)
EXPOSE 80
CMD ["httpd-foreground"]
Linux版本:
FROM alpine:3.9
# ensure www-data user exists
RUN set -x \
&& addgroup -g 82 -S www-data \
&& adduser -u 82 -D -S -G www-data www-data
# 82 is the standard uid/gid for "www-data" in Alpine
# https://git.alpinelinux.org/cgit/aports/tree/main/apache2/apache2.pre-install?h=v3.8.1
# https://git.alpinelinux.org/cgit/aports/tree/main/lighttpd/lighttpd.pre-install?h=v3.8.1
# https://git.alpinelinux.org/cgit/aports/tree/main/nginx/nginx.pre-install?h=v3.8.1
ENV HTTPD_PREFIX /usr/local/apache2
ENV PATH $HTTPD_PREFIX/bin:$PATH
RUN mkdir -p "$HTTPD_PREFIX" \
&& chown www-data:www-data "$HTTPD_PREFIX"
WORKDIR $HTTPD_PREFIX
ENV HTTPD_VERSION 2.4.39
ENV HTTPD_SHA256 b4ca9d05773aa59b54d66cd8f4744b945289f084d3be17d7981d1783a5decfa2
# https://httpd.apache.org/security/vulnerabilities_24.html
ENV HTTPD_PATCHES=""
ENV APACHE_DIST_URLS \
# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
https://www.apache.org/dyn/closer.cgi?action=download&filename= \
# if the version is outdated (or we're grabbing the .asc file), we might have to pull from the dist/archive :/
https://www-us.apache.org/dist/ \
https://www.apache.org/dist/ \
https://archive.apache.org/dist/
# see https://httpd.apache.org/docs/2.4/install.html#requirements
RUN set -eux; \
\
runDeps=' \
apr-dev \
apr-util-dev \
apr-util-ldap \
perl \
'; \
apk add --no-cache --virtual .build-deps \
$runDeps \
ca-certificates \
coreutils \
dpkg-dev dpkg \
gcc \
gnupg \
libc-dev \
# mod_proxy_html mod_xml2enc
libxml2-dev \
# mod_lua
lua-dev \
make \
# mod_http2
nghttp2-dev \
# mod_session_crypto
openssl \
openssl-dev \
pcre-dev \
tar \
# mod_deflate
zlib-dev \
; \
\
ddist() { \
local f="$1"; shift; \
local distFile="$1"; shift; \
local success=; \
local distUrl=; \
for distUrl in $APACHE_DIST_URLS; do \
if wget -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \
success=1; \
break; \
fi; \
done; \
[ -n "$success" ]; \
}; \
\
ddist 'httpd.tar.bz2' "httpd/httpd-$HTTPD_VERSION.tar.bz2"; \
echo "$HTTPD_SHA256 *httpd.tar.bz2" | sha256sum -c -; \
\
# see https://httpd.apache.org/download.cgi#verify
ddist 'httpd.tar.bz2.asc' "httpd/httpd-$HTTPD_VERSION.tar.bz2.asc"; \
export GNUPGHOME="$(mktemp -d)"; \
for key in \
# gpg: key 791485A8: public key "Jim Jagielski (Release Signing Key) <jim@apache.org>" imported
A93D62ECC3C8EA12DB220EC934EA76E6791485A8 \
# gpg: key 995E35221AD84DFF: public key "Daniel Ruggeri (https://home.apache.org/~druggeri/) <druggeri@apache.org>" imported
B9E8213AEFB861AF35A41F2C995E35221AD84DFF \
; do \
gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
done; \
gpg --batch --verify httpd.tar.bz2.asc httpd.tar.bz2; \
command -v gpgconf && gpgconf --kill all || :; \
rm -rf "$GNUPGHOME" httpd.tar.bz2.asc; \
\
mkdir -p src; \
tar -xf httpd.tar.bz2 -C src --strip-components=1; \
rm httpd.tar.bz2; \
cd src; \
\
patches() { \
while [ "$#" -gt 0 ]; do \
local patchFile="$1"; shift; \
local patchSha256="$1"; shift; \
ddist "$patchFile" "httpd/patches/apply_to_$HTTPD_VERSION/$patchFile"; \
echo "$patchSha256 *$patchFile" | sha256sum -c -; \
patch -p0 < "$patchFile"; \
rm -f "$patchFile"; \
done; \
}; \
patches $HTTPD_PATCHES; \
\
gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
./configure \
--build="$gnuArch" \
--prefix="$HTTPD_PREFIX" \
--enable-mods-shared=reallyall \
--enable-mpms-shared=all \
; \
make -j "$(nproc)"; \
make install; \
\
cd ..; \
rm -r src man manual; \
\
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' \
-e 's!^(\s*TransferLog)\s+\S+!\1 /proc/self/fd/1!g' \
"$HTTPD_PREFIX/conf/httpd.conf" \
"$HTTPD_PREFIX/conf/extra/httpd-ssl.conf" \
; \
\
runDeps="$runDeps $( \
scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
| tr ',' '\n' \
| sort -u \
| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
)"; \
apk add --virtual .httpd-rundeps $runDeps; \
apk del .build-deps; \
\
# smoke test
httpd -v
COPY httpd-foreground /usr/local/bin/
EXPOSE 80
CMD ["httpd-foreground"]
3、通过 Dockerfile 的 docker build 构建一个 Apache 镜像,这儿可以自定义镜像名,输入命令 “ docker build -f /opt/dockerfile/apache/apachedockerfile -t huazai/web/httpd:v1.0 . ” 进行构建,部分构建内容如下:
[root@localhost apache]# docker build -f /opt/dockerfile/apache/apachedockerfile -t huazai/web/httpd:v1.0 .
Sending build context to Docker daemon 6.656kB
Step 1/14 : FROM debian:stretch-slim
---> 9a4a82cec2d2
Step 2/14 : ENV HTTPD_PREFIX /usr/local/apache2
---> Running in 446c3f8ff922
Removing intermediate container 446c3f8ff922
---> 2161791cd41e
Step 3/14 : ENV PATH $HTTPD_PREFIX/bin:$PATH
---> Running in 397375e828cd
Removing intermediate container 397375e828cd
---> a57eb06abecf
Step 4/14 : RUN mkdir -p "$HTTPD_PREFIX" && chown www-data:www-data "$HTTPD_PREFIX"
---> Running in 92c401f96407
Removing intermediate container 92c401f96407
---> 6b92ebe17bde
Step 5/14 : WORKDIR $HTTPD_PREFIX
---> Running in 4052e19272cb
Removing intermediate container 4052e19272cb
---> 101d3cb7502a
# 次出省去 N 行
构建成功后,输入命令 “ docker images huazai/web/httpd ” ,可以在本地的镜像列表种查找到方才构建 apache 镜像,内容如下:
好了,关于 Docker系列(十九)——Docker实例一Docker安装Apache实例 就写到这儿了,如果还有什么疑问或遇到什么问题欢迎扫码提问,也可以给我留言哦,我会一一详细的解答的。
歇后语:“ 共同学习,共同进步 ”,也希望大家多多关注CSND的IT社区。
作 者: | 华 仔 |
联系作者: | who.seek.me@java98k.vip |
来 源: | CSDN (Chinese Software Developer Network) |
原 文: |
https://blog.csdn.net/Hello_World_QWP/article/details/84762704 |
版权声明: | 本文为博主原创文章,请在转载时务必注明博文出处! |