docker本地环境搭建

运行环境:nginx,php,mysql,redis
文件目录

lnmp
 - mysql
 - - my.cny
 - php
 - - php74
 - - - etc
 - - - - php
 - - - - - conf.d    # 扩展配置文件
 - - - - - php.ini
 - - - - - php.ini-development
 - - - - - php.ini-production
 - - - - php-fpm
 - - - - - docker.conf
 - - - - - www.conf
 - - - - - www.conf.default
 - - - - - zz-docker.conf
 - - - - pear.conf
 - - - - php-fpm.conf
 - - - - php-fpm.conf.default
 - - - - Dockerfile
 - nginx
 - - conf
 - - - nginx.conf
 - - conf.d
 - - - default.conf
 - redis
 - - conf
 - - - redis.conf
 - docker-compose.yml

docker-compose.yml

直接贴代码

version: '3.8'
services:
  nginx:
    # 容器名称
    container_name: compose-nginx
    image: nginx:latest
    restart: always    # 重启策略
    # 端口映射
    ports:
      - "80:80"
      - "443:443"
    environment:
      TZ: Asia/Shanghai
    # 依赖关系,需要先运行php
    depends_on:
      - php
    volumes:
      - "${PWD}/nginx/conf/nginx.conf:/etc/nginx/nginx.conf"
      - "${PWD}/nginx/conf.d:/etc/nginx/conf.d"    #将宿主机上nginx配置文件映射到容器中
      #- "${PWD}/nginx/cert:/etc/nginx/cert"
      - "${PWD}/../../code:/var/www/html:cached"
      - "${PWD}/nginx/log:/var/log/nginx"          #默认日志地址
    networks:
      app_net:
        ipv4_address: 172.18.0.5
  php:
    #指定build Dockerfile生成镜像
    build: "${PWD}/php/php74"
    image: php:7.4
    container_name: compose-php
    restart: always
    ports:
      - "9000:9000"
      - "8282:8282"
    environment:
      TZ: Asia/Shanghai
    volumes:
      - "${PWD}/../../code:/var/www/html:cached"
      - "${PWD}/php/php74/etc:/usr/local/etc"
      - "${PWD}/php/php74/log:/var/log/php"
    stdin_open: true
    tty: true   #这两条是防止启动php失败
    links:
      - "mysql"
    networks:
      app_net:
        ipv4_address: 172.18.0.4
  mysql:
    image: mysql:5.7
    container_name: compose-mysql
    restart: always
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - "${PWD}/mysql/my.cnf:/etc/mysql/my.cnf"
      - "${PWD}/mysql/data:/var/lib/mysql"
      - "${PWD}/mysql/logs:/data/mysql/logs"
    ports:
      - "3306:3306"
    networks:
      app_net:
        ipv4_address: 172.18.0.3
  redis:
    image: redis:5.0
    container_name: compose-redis
    restart: always
    environment:
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
    volumes:
      - "${PWD}/redis/conf/redis.conf:/etc/redis/redis.conf"
      - "${PWD}/redis/data/:/data"
    ports:
      - 6379:6379
    command:
      # 执行的命令
      redis-server /etc/redis/redis.conf --requirepass 123456 --appendonly yes
    networks:
      app_net:
        ipv4_address: 172.18.0.2
networks:             # 配置docker 网络
  app_net:
    name: app_net     # 设置网络自定义名称
    driver: bridge    #
    ipam:
      config:
        - subnet: 172.18.0.0/16

code:是本人代码文件目录
其中mysql和redis日志目录没用,没去仔细研究
每次新增本地虚拟站点,在nginx/conf.d/里面新建文件就行

nginx/conf.d/default

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

PHP Dockerfile构建文件

FROM php:7.4-fpm
RUN echo "deb http://mirrors.aliyun.com/debian/ buster main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib \n \
deb http://mirrors.aliyun.com/debian-security buster/updates main \n \
deb-src http://mirrors.aliyun.com/debian-security buster/updates main \n \
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib \n \
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib \n \
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib" > /etc/apt/sources.list \
    # 更新及安装库
    && apt-get update \
    && apt-get install -y \
    vim net-tools \
    build-essential \
    libmagickcore-dev \
    libmagickwand-dev \
    imagemagick \
    libmcrypt-dev \
    libzip-dev \
    libmemcached-dev \
    # GD库扩展
    && apt-get install -y \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    && docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
    # PHP扩展安装
    && docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv \
    && pecl install memcached-3.1.4 \
    && pecl install redis-5.2.2 \
    && pecl install imagick mcrypt zip swoole \
    && docker-php-ext-enable memcached redis imagick mcrypt zip swoole

# 镜像信息
LABEL Author="vance"
LABEL Version="2020.8"
LABEL Description="PHP 7.4 开发环境镜像."

这样,根据docker-compose.yml里面的ip以及端口,本地就可以使用了

本作品采用《CC 协议》,转载必须注明作者和本文链接
vance
vance
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 2

这个能用多站点吗,就是本地修改 hosts 文件,像 homestead 那样使用。

3年前 评论
vance

@xiayin 可以的,这个只是搭建本地环境,多个站点目录设置php和nginx挂载设置

- "${PWD}/../../code:/var/www/html:cached"

本地域名配置目录 nginx挂载设置

- "${PWD}/nginx/conf.d:/etc/nginx/conf.d"

conf.d目录里面default.conf复制出来一个比如baidu.conf,里面配置百度的域名和端口,以及站点目录路径

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!