千家信息网

基于docker构建中间件容器应用环境

发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,1 背景介绍为了方便开发人员对自己的应用代码进行镜像封装,需要提供一个中间件基础镜像让他们直接在上面封装应用镜像,然后在docker编译服务器上面按镜像打包流程了封装镜像,采用流水式的方式完成所有操作
千家信息网最后更新 2025年02月03日基于docker构建中间件容器应用环境

1 背景介绍

为了方便开发人员对自己的应用代码进行镜像封装,需要提供一个中间件基础镜像让他们直接在上面封装应用镜像,然后在docker编译服务器上面按镜像打包流程了封装镜像,采用流水式的方式完成所有操作。

今天给大家介绍一下如何将传统nginx+php-fpm组合中间件软件在docker上面将其封装成中间件镜像,然后由让开发将其php应用代码部署到中间件容器上面运行起来。

2 镜像封装流程

  • 基础镜像:在构建中间件镜像前,需要准备一个干净的centos7镜像,方便在基础镜像上面安装中间件nginx和php-fpm等软件。

  • 构建中间件镜像:在构建应用镜像前,提前构建好中间件镜像,然而中间件镜像是基于centos基础镜像上面去安装nginx和php-fpm中间件软件,对其中间件性能参数优化配置,并定义中间件运行环境启动脚本,最后通过命令封装成中间件镜像。

  • 构建应用镜像:以中间件镜像做为基础镜像,编写dockerfile脚本在发布php代码和更新nginx配置,最后通过命令将其封装成应用镜像。

  • 启动应用容器:使用docker run命令将应用镜像启动。

3 镜像封装过程

3.1 环境准备

ü 镜像列表

镜像名

镜像说明

镜像来源

Centos:latest

Centos7操作系统镜像

公网下载

nginx-phpfpm70:v1.0

安装好了nginx和php中间件镜像

离线构建

acitivty_admin:v1.0

基于中间件镜像上面部署应用生成新的应用镜像

离线构建

ü 编译环境

1、 准备一台kernel 3.10以上版本的linux主机

2、 需要在主机上面安装docker 1.13.1软件。

3、 主机需要能上外网方便外网镜像下载和软件包下载

3.2 下载centos基础镜像

下面直接在编译主机上面用docker search centos命令查看外网镜像。下载星最多的centos镜像,然后用docker pull centos直接下载,最后会将镜像下载到本地。

3.3 构建中间件镜像

前面我们已经将centos7的镜像文件下载下来,现在构建中间件镜像可以直接在基于centos镜像上面安装nginx和php-fpm软件,最后在通过docker命令生成一个新的中间件镜像。

  • 以下是准备好的中间件构建镜像的dockerfile脚本:

# 一个干净的centos镜像

FROM centos: latest

# 镜像创建者

MAINTAINER jaymarco@shsnc.com

# 在centos镜像中配置php的yum源,然后安装php相关依赖包、php-fpm软件、php对应的扩展包

RUN rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -ivh http://rpms.remirepo.net/enterprise/remi-release-7.rpm && \

yum install -y php70-php-gd.x86_64 php70-php-mcrypt.x86_64 php70-php-fpm.x86_64 php70-php-pecl-redis.x86_64 python-setuptools \

php70-php-mbstring.x86_64 php70-php-snmp.x86_64 php70-php-pecl-zip.x86_64 \

php70-php-mysqlnd.x86_64 php70-php-pecl-mysql.x86_64 gcc gcc-c++ automake libtool make cmake openssl openssl-devel pcre-devel &&\

yum clean all

# 在centos镜像中安装nginx软件

RUN rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.10.3-1.el7.ngx.x86_64.rpm

#php 性能参数优化配置

RUN sed -e 's/127.0.0.1:9000/9000/' -e '/allowed_clients/d' -e '/catch_workers_output/s/^;//' -e '/error_log/d' -i /etc/opt/remi/php70/php-fpm.d/www.conf &&\

sed -e 's/daemonize = yes/daemonize = no/' -i /etc/opt/remi/php70/php-fpm.conf

# 配置镜像时区文件,方便镜像启动成容器后时区与本地主机一致

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\

echo 'Asia/Shanghai' >/etc/timezone

# 安装supervisor后台进程管理工具,方便容器启动多进程进行后台监控管理。

#Install supervisor

RUN easy_install supervisor && \

mkdir -p /var/log/supervisor && \

mkdir -p /var/run/sshd && \

mkdir -p /var/run/supervisord

# 将本地修改好的supervisord配置文件COPY到镜像中

ADD supervisord.conf /etc/supervisord.conf

# 将编写好的脚本从本地COPY到镜像中

ADD startserv.sh /startserv.sh

RUN chmod +x /startserv.sh

# 将容器中的80,9000端口映射成宿主机器中的某个端口

EXPOSE 80 9000

# 容器启动时启动nginx和php服务

CMD ["/startserv.sh"]

  • 配置文件supervisord.conf,下面标红部分属于服务启动配置。

[unix_http_server]

file=/tmp/supervisor.sock ; (the path to the socket file)

[supervisord]

logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)

logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)

logfile_backups=10 ; (num of main logfile rotation backups;default 10)

loglevel=info ; (log level;default info; others: debug,warn,trace)

pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)

nodaemon=true ; (start in foreground if true;default false)

minfds=1024 ; (min. avail startup file descriptors;default 1024)

minprocs=200 ; (min. avail process descriptors;default 200)

user=root ;

; the below section must remain in the config file for RPC

; (supervisorctl/web interface) to work, additional interfaces may be

; added by defining them in separate rpcinterface: sections

[rpcinterface:supervisor]

supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]

serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket

[program:php-fpm]

command=/opt/remi/php70/root/usr/sbin/php-fpm -F

[program:nginx]

command=/usr/sbin/nginx -c /etc/nginx/nginx.conf

服务启动脚本 startserv.sh

#!/bin/sh

/usr/bin/supervisord -n -c /etc/supervisord.conf

前面已经将dockerfile脚本和配置文件准备就绪后,接下来直接可以使用docker构建命令来构建一个新的中间件镜像。最后也会在本地生成一个nginx-phpfpm:v1.0镜像。

构建命令:

docker build -t nginx-phpfpm:v1.0 .

构建过程:

3.4 构建应用镜像

部署应用镜像需要准备两份东西一份是php应用代码,另一份是nginx配置文件。并将这两份东西打包到中间件镜像文件中,最后会构建一个新的应用镜像。

  • Nginx 配置参数

server {

listen 8080;

server_name 127.0.0.1;

large_client_header_buffers 4 16k;

client_max_body_size 300m;

client_body_buffer_size 128k;

proxy_connect_timeout 600;

proxy_read_timeout 600;

proxy_send_timeout 600;

proxy_buffer_size 64k;

proxy_buffers 32 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

location / {

root /var/www/cecrm_acitivtycenter;

index index.php;

try_files $uri $uri/ /index.php?$uri&$args;

}

location ~ ^.+.php {

include fastcgi_params;

root /var/www/cecrm_acitivtycenter;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

fastcgi_intercept_errors on;

fastcgi_connect_timeout 600;

fastcgi_send_timeout 1800;

fastcgi_read_timeout 6000;

fastcgi_buffer_size 128k;

fastcgi_buffers 4 256k;

fastcgi_busy_buffers_size 256k;

fastcgi_temp_file_write_size 256k;

}

}

  • php 应用程序构建dockerfile脚本

# 引用中间件镜像做为应用构建基础镜像

FROM nginx-phpfpm70:v1.0

MAINTAINER jaymarco@shsnc.com

# 发布 php 程序代码

ADD admin /var/www/cecrm_acitivtycenter/admin

#nginx 性能参数优化

ADD admindocker.conf /etc/nginx/conf.d/admindocker.conf

前面已经将dockerfile脚本和配置文件准备就绪后,接下来直接可以使用docker构建命令来构建一个新的应用镜像。最后也会在本地生成一个acitivty_admin:v1.0镜像。

构建命令:

docker build -t acitivty_admin:v1.0 .

构建过程:

我们可以看到应用镜像已经构建成功

3.5 容器启动与验证

启动应用容器

docker run -itd --name acitivty_admin -p 8090:8090 acitivty_admin:v1.0

查看容器中启动的进程,发现nginx和php-fpm进程已经成功启动

最后我们验证一下发布后的应用能正常访问

0