简介

基于 docker-compose,一键构建 lnmp 开发环境。搭建成功后,包含的容器/软件版本如下:

容器名称 基础镜像 当前安装成功版本
web-php php:7.2-fpm 7.2.21
web-nginx nginx:stable 1.16.0
web-mysql mysql:5.7 5.7.27
web-redis redis:latest 5.0.5

每个容器都是基于官方镜像构建。由于 php-fpm 官方镜像默认很多扩展没装上,这里单独做了一个 Dockerfile 文件来补充安装。

 

目录结构

目录结构如下:

/dnmp
|
|   .env
|   docker-compose.yml
|
+---conf
|   +---mysql
|   |       my.cnf
|   |
|   +---nginx
|   |   |   nginx.conf
|   |   |
|   |   \---conf.d
|   |           default.conf
|   |
|   +---php
|   |       Dockerfile
|   |       php.ini
|   |
|   \---redis
|           redis.conf
|
+---data
|   +---mysql
|   +---nginx
|   +---php
|   \---redis
|
\---www
        a.php
        b.php
        c.php
        d.php

 

目录及文件说明

1、/dnmp/conf 此目录分别放各个软件的配置文件。其中,my.ini 配置了 sql_mode 和默认时区;php.ini 更改了 session 配置,使用 redis 接管。

2、/dnmp/data 各个容器运行生成的数据文件、日志文件。

3、/dnmp/www 此目录为web工作目录。

4、/dnmp/.env  定义了环境变量,内容如下:

#web工作目录
WORK_DIR=./www

#php日志的存放地
PHP_LOG_DIR=./data/php/

#nginx日志的存放地
NGINX_LOG_DIR=./data/nginx/

#mysql数据存储位置
MYSQL_DATA_DIR=./data/mysql

#redis持久化数据存放地
REDIS_DATA_DIR=./data/redis

#mysql密码
MYSQL_ROOT_PASSWORD=123456

5、/dnmp/docker-compose.yml 构建各个容器的配置文件,内容如下:

version: "2"


services:

    php:
        build:
          context: ./conf/php
        container_name: web-php
        volumes:
          - ${WORK_DIR}:/var/www/html
          - ${PHP_LOG_DIR}:/var/log/php
          - ./conf/php/php.ini:/usr/local/etc/php/php.ini
        expose:
          - 9000
        restart: unless-stopped
        networks:
          backends:
            ipv4_address: 172.8.0.2
        extra_hosts:
          - www.test.com:172.8.0.1

    nginx:
        image: nginx:stable
        container_name: web-nginx
        volumes:
          - ${WORK_DIR}:/var/www/html
          - ${NGINX_LOG_DIR}:/var/log/nginx
          - ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf
          - ./conf/nginx/conf.d:/etc/nginx/conf.d
        ports:
          - 80:80
          - 443:443
        restart: unless-stopped
        networks:
          backends:
            ipv4_address: 172.8.0.3

    mysql:
        image: mysql:5.7
        container_name: web-mysql
        volumes:
          - ${MYSQL_DATA_DIR}:/var/lib/mysql
          - ./conf/mysql/my.cnf:/etc/mysql/conf.d/mysql.cnf
        expose:
          - 3306
        restart: unless-stopped
        networks:
          backends:
            ipv4_address: 172.8.0.4
        environment:
          MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}

    redis:
        image: redis:latest
        container_name: web-redis
        volumes:
          - ./conf/redis/redis.conf:/etc/redis/redis.conf
          - ${REDIS_DATA_DIR}:/data
        restart: unless-stopped
        expose:
          - 6379
        networks:
          backends:
            ipv4_address: 172.8.0.5
        command: redis-server /etc/redis/redis.conf


networks:
  backends:
    driver: bridge
    ipam:
      driver: default
      config:
      - subnet: 172.8.0.0/24
        gateway: 172.8.0.1

6、/dnmp/conf/php/Dockerfile 基于官方 7.2-fpm 镜像,补充安装经常用到的拓展,如 gd、pdo_mysql、redis、swoole,及安装 composer。文件内容如下:

FROM php:7.2-fpm

# 官方版本默认安装扩展: 
# Core
# ctype
# curl
# date
# dom
# fileinfo
# filter
# ftp
# hash
# iconv
# json
# libxml
# mbstring
# mysqlnd
# openssl
# pcre
# PDO
# pdo_sqlite
# Phar
# posix
# readline
# Reflection
# session
# SimpleXML
# sodium
# SPL
# sqlite3
# standard
# tokenizer
# xml
# xmlreader
# xmlwriter
# zlib

RUN set -eux; \
    mv /etc/apt/sources.list /etc/apt/sources.list.bak; \
    echo "deb http://mirrors.aliyun.com/debian stable main contrib non-free" > /etc/apt/sources.list; \
    echo "deb http://mirrors.aliyun.com/debian stable-proposed-updates main contrib non-free" >> /etc/apt/sources.list; \
    echo "deb http://mirrors.aliyun.com/debian stable-updates main contrib non-free" >> /etc/apt/sources.list; \
    echo "deb-src http://mirrors.aliyun.com/debian stable main contrib non-free" >> /etc/apt/sources.list; \
    echo "deb-src http://mirrors.aliyun.com/debian stable-proposed-updates main contrib non-free" >> /etc/apt/sources.list; \
    echo "deb-src http://mirrors.aliyun.com/debian stable-updates main contrib non-free" >> /etc/apt/sources.list; \
    \
    apt-get update; \
    \
    apt-get install -y --no-install-recommends \
    libbz2-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libxml2-dev \
    libtidy-dev \
    libxslt1-dev \
    libzip-dev \
    libldap2-dev \
    libc-client-dev \
    libkrb5-dev \
    libicu-dev; \
    \
    docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm; \
    \
    docker-php-ext-install -j$(nproc) bz2; \
    \
    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \
    docker-php-ext-install -j$(nproc) gd; \
    \
    docker-php-ext-install -j$(nproc) soap wddx xmlrpc tidy xsl; \
    \
    docker-php-ext-install -j$(nproc) zip; \
    \
    docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu && \
    docker-php-ext-install -j$(nproc) ldap; \
    \
    docker-php-ext-configure imap --with-kerberos --with-imap-ssl && \
    docker-php-ext-install -j$(nproc) imap; \
    \
    docker-php-ext-configure opcache --enable-opcache && \
    docker-php-ext-install opcache; \
    \
    curl -o /usr/lib/ssl/cert.pem https://curl.haxx.se/ca/cacert.pem; \
    \
    printf "\n\n" | pecl install http://pecl.php.net/get/redis-5.0.2.tgz && \
    docker-php-ext-enable redis; \
    \
    printf "yes\nyes\nyes\n" | pecl install http://pecl.php.net/get/swoole-4.4.2.tgz && \
    docker-php-ext-enable swoole; \
    \
    rm -r /var/lib/apt/lists/* /tmp/pear/download/*; \
    \
    php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"; \
    php composer-setup.php --install-dir=/usr/local/bin --filename=composer; \
    php -r "unlink('composer-setup.php');"; \
    composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

 

运行说明

控制台切换到 docker-compose.yml 所在目录, 使用 docker-compose 指令运行:

# 构建容器并运行,会在前台打印运行日志
docker-compose up

# 构建容器并在后台运行,即不打印运行日志
docker-compose up -d

# 停止运行并删除容器
docker-compose down

# 启动容器
docker-compose start

# 停止运行容器
docker-compose stop

# 重启容器
docker-compose restart

刚开始接触 docker-compose 的时候,同事告诉我用 docker-compse up -ddocker-compse down 来启动和停止容器,后来看文档才知道 up 和 down 有些粗暴!注意,down 和 stop 的区别,down 会删除容器,下次必须用 up 重新构建容器;stop 仅仅是停止运行,使用 start 就可以重新启动容器。比方说,你在容器里安装了一些软件,希望下次重启电脑的时候还要用,那就不能用 down 指令,要用 stop。

 

成品下载

下载链接:/upfiles/files/201908/dockerlnmp.zip

 

php Dockerfile 参考自:https://www.jianshu.com/p/20fcca06e27e