使用 docker 部署 PHP 环境

Dockerfile

FROM centos:7.2.1511
MAINTAINER liuchao "thesedays@126.com"
ENV PHP_VERSION 7.0.4

# add epel
RUN yum install -y epel-release

# update OS
RUN yum update -y
# install deps
RUN yum install -y \
    wget \
    git \
    ntpdate \
    gcc gcc-c++ \
    autoconf \
    iproute \
    libcurl libcurl-devel \
    libxml2 libxml2-devel \
    openssl openssl-devel \
    pcre pcre-devel \
    zlib zlib-devel \
    libpng libpng-devel \
    freetype freetype-devel \
    libjpeg-turbo libjpeg-turbo-devel \
    libvpx libvpx-devel \
    fontconfig fontconfig-devel \
    libXpm libXpm-devel \
    libtiff libtiff-devel \
    libmcrypt libmcrypt-devel \
    gd gd-devel \
    libicu libicu-devel

# install PHP
RUN mkdir -p /data/
RUN wget http://hk1.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror \
    -O /data/php-$PHP_VERSION.tar.gz
RUN cd /data/ \
 && tar zxf php-$PHP_VERSION.tar.gz
RUN cd /data/php-$PHP_VERSION/ \
 && ./configure \
    --prefix=/opt/php-$PHP_VERSION \
    --enable-fpm \
    --with-fpm-user=nobody \
    --with-fpm-group=nobody \
    --with-pear \
    --with-zlib \
    --with-pcre-regex \
    --with-gd \
    --with-mysqli=mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --enable-xml \
    --enable-bcmath \
    --with-curl \
    --with-mcrypt \
    --enable-sockets \
    --with-xmlrpc \
    --enable-zip \
    --enable-soap \
    --enable-mbstring \
    --enable-exif \
    --enable-pcntl \
    --enable-intl \
    --with-openssl \
    --with-freetype-dir=/usr/include/freetype2 \
 && make \
 && make install
RUN ln -s /opt/php-$PHP_VERSION /opt/php

# install PHP redis
RUN cd /data/ \
 && git clone https://github.com/phpredis/phpredis.git --branch php7
RUN cd /data/phpredis \
 && /opt/php/bin/phpize \
 && ./configure --with-php-config=/opt/php/bin/php-config \
 && make \
 && make install

# init config files
COPY php-fpm.conf /opt/php/etc/php-fpm.conf
COPY php.ini /opt/php/lib/php.ini
COPY cert.pem /opt/php/etc/cert.pem

构建镜像

切换到 Dockerfile 所在目录,运行以下命令构建镜像:

# docker build -t "php/base:v1.0" .

构建完成后就可以使用 php/base:v1.0 这个镜像了:

# docker images
REPOSITORY    TAG     IMAGE ID        CREATED          SIZE
php/base      v1.0    78f156daef65    8 seconds ago    1.472 GB

打包镜像

# docker save php/base:v1.0 > php_base_v1.0.tar

使用打包镜像

# docker load -i php_base_v1.0.tar

运行 docker 镜像

# docker run \
    -it \
    -d \
    -p 10.1.1.1:12306:9000 \
    -v 本地代码目录:镜像内代码目录 \
    -v 本地log目录:镜像内log目录 \
    php/base:v1.0 \
    /opt/php/sbin/php-fpm

最后,经过前端的反向代理将请求转发至 10.1.1.1:12306 就可以提供服务了。

Leave a Reply

Your email address will not be published. Required fields are marked *

*