Docker 快速构建LNMP产品级别环境

Docker 快速搭建lnmp php环境

root@~ vi Dockerfile

Dockerfile Content(内容-PHP)

FROM centos:latest
MAINTAINER https://blog.51cto.com/andyxu
ENV TIME_ZOME Asia/Shanghai
ARG PV="php-7.2.11"

ADD $PV.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel bison \
    && mkdir /data \
    && cd /tmp/$PV \
    && ./configure --prefix=/data/php \
        --with-config-file-path=/data/php/etc \
        --with-gd --with-mysqli \
        --with-openssl --with-zlib --with-curl \
        --with-jpeg-dir --with-png-dir --with-iconv \
        --enable-fpm --enable-zip --enable-mbstring \
    && make -j 4 \
    && make install \
    && cp /data/php/etc/php-fpm.conf.default /data/php/etc/php-fpm.conf \
    && cp /data/php/etc/php-fpm.d/www.conf.default /data/php/etc/php-fpm.d/www.conf \
    && sed -i '/;daemonize/a\daemonize = no' /data/php/etc/php-fpm.conf \
    && sed -i 's/127.0.0.1/0.0.0.0/g' /data/php/etc/php-fpm.d/www.conf \
    && echo "${TIME_ZOME}" > /etc/timezone \
    && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
    && rm -rf /tmp/php* \
    && yum clean all \
    && yum -y remove gcc gcc-c++ make

WORKDIR /data/php/
EXPOSE 9000
CMD ["sbin/php-fpm","-c","etc/php-fpm.conf"]

构建nginx镜像

Dockerfiler Content (内容-NGINX)

FROM centos:latest
MAINTAINER https://blog.51cto.com/andyxu
ENV TIME_ZOME Asia/Shanghai
ARG NV="nginx-1.15.5"

COPY nginx.conf /data/nginx/conf/
ADD $NV.tar.gz /tmp
RUN yum -y install gcc gcc-c++ make openssl-devel pcre-devel \
        && mkdir -p /data \
        && cd /tmp/$NV \
        && ./configure --prefix=/data/nginx \
        && make -j 2 \
        && make install \
        && echo "${TIME_ZOME}" > /etc/timezone \
        && ln -sf /usr/share/zoneinfo/${TIME_ZOME} /etc/localtime \
        && rm -rf /tmp/nginx* \
        && yum clean all \
        && yum -y remove gcc gcc-c++ make

WORKDIR /data/nginx/
EXPOSE 80
CMD ["./sbin/nginx","-g","daemon off;"]

下载mysql镜像

root@~ docker pull mysql:8.0.12
~  
8.0.12: Pulling from library/mysql
802b00ed6f79: Pull complete 
30f19a05b898: Pull complete 
3e43303be5e9: Pull complete 
94b281824ae2: Pull complete 
51eb397095b1: Pull complete 
54567da6fdf0: Pull complete 
bc57ddb85cce: Pull complete 
d6cd3c7302aa: Pull complete 
d8263dad8dbb: Pull complete 
780f2f86056d: Pull complete 
8e0761cb58cd: Pull complete 
7588cfc269e5: Pull complete 
Digest: sha256:038f5f6ea8c8f63cfce1bce9c057ab3691cad867e18da8ad4ba6c90874d0537a
Status: Downloaded newer image for mysql:8.0.12
查看已经构建完成的镜像情况
root@~ docker images

创建并启动容器

(1)创建自定义网络lnmp
root@~  docker network create lnmp
(2)创建生成mysql、php、nginx容器的脚本

#!/bin/bash
function mysql()
{
    docker run --name mysql --restart=always --net lnmp -p 3306:3306 \
    -v /data/mysql/data:/var/lib/mysql \
    -v /data/mysql/conf:/etc/mysql/conf.d \
    -v /data/mysql/logs:/logs \
    -e MYSQL_ROOT_PASSWORD=test123456 \
    -d mysql:8.0.12 --character-set-server=utf8
}

function php()
{
    docker run --name php --restart=always --net lnmp \
    -v /data/nginx/html:/data/nginx/html \
    -v /data/php/log:/data/php/var/log \
    -d php:7.2.11
}

function nginx()
{
    docker run --name nginx --restart=always --net lnmp -p 80:80 \
    -v /data/nginx/html:/data/nginx/html \
    -v /data/nginx/logs:/data/nginx/logs \
    -d nginx:1.15.5
}
(3)启动mysql、php、nginx容器
[root@~]# sh docker_lnmp.sh mysql
[root@~]# sh docker_lnmp.sh php
[root@~]# sh docker_lnmp.sh nginx
(4)查看容器是否都成
docker images

你可能感兴趣的:(Docker 快速构建LNMP产品级别环境)