编写 DockerFile 构建 Nginx 与 Tengine 镜像

Nginx 镜像的 DockerFile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
FROM centos:7

MAINTAINER peter<peter@gmail.com>

# 安装软件
RUN yum -y update && yum -y install gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid openssl openssl-devel pcre pcre-devel

# 创建用户
RUN groupadd www
RUN useradd -g www www -s /bin/false

# 定义Nginx版本号
ENV VERSION 1.14.2

# 下载并解压文件
RUN mkdir -p /usr/local/src/
ADD http://nginx.org/download/nginx-$VERSION.tar.gz /usr/local/src
RUN tar -xvf /usr/local/src/nginx-$VERSION.tar.gz -C /usr/local/src/

# 创建安装目录
ENV NGINX_HOME /usr/local/nginx
RUN mkdir -p $NGINX_HOME
RUN chown -R www:www $NGINX_HOME

# 进入解压目录
WORKDIR /usr/local/src/nginx-$VERSION

# 编译安装
RUN ./configure \
--user=www \
--group=www \
--prefix=$NGINX_HOME \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module
RUN make
RUN make install

# 备份Nginx的配置文件
RUN mv $NGINX_HOME/conf/nginx.conf $NGINX_HOME/conf/nginx.conf.default

# 设置环境变量
ENV PATH $PATH:$NGINX_HOME/sbin

# 创建WebApp目录
ENV WEB_APP /usr/share/nginx/html
RUN mkdir -p $WEB_APP

# 设置默认工作目录
WORKDIR $WEB_APP

# 暴露端口
EXPOSE 80
EXPOSE 443

# 清理压缩包与解压文件
RUN rm -rf /usr/local/src/nginx*

CMD $NGINX_HOME/sbin/nginx -g 'daemon off;' -c $NGINX_HOME/conf/nginx.conf

构建 Nginx 镜像

1
2
# 构建Nginx镜像
# docker build -f docker-file -t peter/nginx:1.14.2 .

Docker-Compose 管理 Nginx 镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
version: "3.5"

services:
nginx:
image: peter/nginx:1.14.2
container_name: nginx-1.14.2
privileged: false
ports:
- 80:80
- 443:443
volumes:
- '/container/nginx/wwwroot:/usr/share/nginx/html'
- '/container/nginx/logs:/usr/local/nginx/logs'
- '/container/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf'

# 上面的配置是docker-compose.yml文件的内容,数据卷部分可以根据自己的实际情况进行修改
# 注意: 在/container/nginx/nginx.conf配置文件中,需要手动修改root的路径为/usr/share/nginx/html

创建并启动 Nginx 容器

1
2
3
4
5
# 创建并启动容器
# docker-compose up -d

# 查看容器的运行状态
# docker-compose ps

Tengine 镜像的 DockerFile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
FROM centos:7

MAINTAINER peter<peter@gmail.com>

# 安装软件
RUN yum -y update && yum -y install vim tree htop tmux net-tools telnet wget curl supervistor autoconf git gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

# 创建用户
RUN groupadd tengine
RUN useradd -g tengine tengine

# 定义Tengine版本号
ENV VERSION 2.2.3

# 下载并解压文件
RUN mkdir -p /usr/local/src/
ADD http://tengine.taobao.org/download/tengine-$VERSION.tar.gz /usr/local/src
RUN tar -xvf /usr/local/src/tengine-$VERSION.tar.gz -C /usr/local/src/

# 创建安装目录
ENV TENGINE_HOME /usr/local/tengine
RUN mkdir -p $TENGINE_HOME

# 进入解压目录
WORKDIR /usr/local/src/tengine-$VERSION

# 编译安装
RUN ./configure \
--user=tengine \
--group=tengine \
--prefix=$TENGINE_HOME \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_concat_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_upstream_consistent_hash_module
RUN make
RUN make install

# 备份Tengine的配置文件
RUN mv $TENGINE_HOME/conf/nginx.conf $TENGINE_HOME/conf/nginx.conf.default

# 设置环境变量
ENV PATH $PATH:$TENGINE_HOME/sbin

# 创建WebApp目录
ENV WEB_APP /usr/share/tengine/html
RUN mkdir -p $WEB_APP

# 设置默认工作目录
WORKDIR $WEB_APP

# 暴露端口
EXPOSE 80
EXPOSE 443

# 清理压缩包与解压文件
RUN rm -rf /usr/local/src/tengine*

CMD $TENGINE_HOME/sbin/nginx -g 'daemon off;' -c $TENGINE_HOME/conf/nginx.conf

构建 Tengine 镜像

1
2
# 构建Tengine镜像
# docker build -f docker-file -t peter/tengine:2.2.3 .

Docker-Compose 管理 Tengine 镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
version: "3.5"

services:
tengine:
image: peter/tengine:2.2.3
container_name: tengine:2.2.3
restart: always
privileged: false
ports:
- 80:80
- 443:443
volumes:
- '/container/tengine/wwwroot/:/usr/share/tengine/html'
- '/container/tengine/logs:/usr/local/tengine/logs'
- '/container/tengine/nginx.conf:/usr/local/tengine/conf/nginx.conf'

# 上面的配置是docker-compose.yml文件的内容,数据卷部分可以根据自己的实际情况进行修改
# 注意: 在/container/tengine/nginx.conf配置文件中需要手动修改root的路径为/usr/share/tengine/html

创建并启动 Tengine 容器

1
2
3
4
5
# 创建并启动容器
# docker-compose up -d

# 查看容器的运行状态
# docker-compose ps