window10本地docker-compose构建nginx+PHP+mysql环境

为什么用docker,想象一下你换了电脑或者服务器,或者新人接触你的项目,一个docker打包新电脑丢上去,还跟原版环境相同,它不快乐?,所谓工欲善其事必先利其器。

如果从安装Docker Desktop学起,请先读本文下面学习docker彩踩过的坑

创建目录docker,其下目录结构
window10本地docker-compose构建nginx+PHP+mysql环境_第1张图片

运行docker-composer up -d,等待执行成功后,本地访问127.0.0.1访问www根目录
访问bbb.cn,需在在本地host文件添加,127.0.0.1 bbb.cn

docker-compose.yml


version: '3'
networks:
    service-net:
        driver: bridge
services:
    yyl-nginx:
        container_name: yyl-nginx
        build: ./nginx
        ports:
            - 88:88
            - 80:80
            - 443:443
        volumes:
            - ./www:/usr/share/nginx/html
            - ./nginx/nginx.conf:/etc/nginx/nginx.conf
            - ./nginx/conf.d:/etc/nginx/conf.d
            - ./nginx/logs:/var/log/nginx
        depends_on:
            - yyl-php
        restart: always
        networks:
            - service-net
    yyl-php:
        container_name: yyl-php
        build: ./php
        depends_on:
            - yyl-redis
            - yyl-mysql
        volumes:
            - ./www:/usr/share/nginx/html
            - ./php/php.ini:/usr/local/etc/php/php.ini
            - ./php/logs:/usr/local/var/log
        restart: always
        networks:
            - service-net
    yyl-redis:
        image: redis:6.0.1
        ports:
            - 6379:6379
        container_name: yyl-redis
        restart: always
        networks:
            - service-net
    yyl-mysql:
        image: mysql:5.7.16
        container_name: yyl-mysql
        ports:
          - 3306:3306
        restart: always
        volumes:
          - ./mysql/conf/my.cnf:/etc/my.cnf
        environment:
          - "MYSQL_ROOT_PASSWORD=123456"
          - "TZ=Asia/Shanghai"
        networks:
            - service-net

文件my.cnf

[mysqld]
user=mysql
default-storage-engine=INNODB
#character-set-server=utf8
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
[client]
#utf8mb4字符集可以存储emoji表情字符
#default-character-set=utf8
default-character-set=utf8mb4
[mysql]
#default-character-set=utf8
default-character-set=utf8mb4

文件nginx文件夹下Dockerfile

FROM nginx:1.16.1
# set timezome
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

文件nginx文件夹下bbb.conf

server {
        listen 80;
        server_name  "bbb.cn";
         
        index index.html index.htm index.php;
        root /usr/share/nginx/html/bbb.cn;
         
        error_page   500 502 503 504  /50x.html;

        if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php?s=/$1 last;
        }

        location = /50x.html {
            root   /usr/share/nginx/html;
        }
        
        location ~ \.php$ {
            fastcgi_pass   yyl-php:9000;  #容器名:端口
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        access_log  /var/log/nginx/bbb.cn.log  main;
 }

文件nginx文件夹下default.cn

server {
        listen 80;
        server_name  "";
         
        index index.html index.htm index.php;
        root /usr/share/nginx/html/;
         
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
 
		if (!-e $request_filename){
          rewrite ^/(.*) /index.php last;
		}
		
        location ~ \.php$ {
            fastcgi_pass   yyl-php:9000;  #容器名:端口
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
 
        access_log  /var/log/nginx/default.log  main;
 }

nginx.conf

user  nginx;
worker_processes auto;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    keepalive_timeout  65;
 
    #gzip  on;
 
    include /etc/nginx/conf.d/*.conf;
}

php文件夹下Dockerfile文件

FROM php:7.2-fpm
 
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
 
# 更新安装依赖包和PHP核心拓展
RUN apt-get update && apt-get install -y \
        --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev curl libzip-dev \
        && rm -r /var/lib/apt/lists/* \
        && docker-php-ext-configure gd \
        && docker-php-ext-install -j$(nproc) gd bcmath opcache pdo_mysql gettext sockets zip mysqli dba pcntl calendar shmop exif sysvmsg sysvsem sysvshm 
 
# 安装 PECL 拓展,安装Redis,swoole
RUN pecl install redis \
	&& pecl install swoole \
    && docker-php-ext-enable redis swoole
 
# 安装 Composer
ENV COMPOSER_HOME /root/composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
ENV PATH $COMPOSER_HOME/vendor/bin:$PATH
WORKDIR /data

bbb.cn下index.php

www下index.php

在学习docker过程中踩过的坑:

  1. windows环境下,下载的Docker Desktop 会发现无法运行,解决办法分别为,开启虚拟化(请自行百度),下载低版本的docker,我这里的docker版本为2.3.0.2
  2. 挂载问题, 挂载失败,去setting里添加挂载盘,截图如下

window10本地docker-compose构建nginx+PHP+mysql环境_第2张图片

  1. 单纯的拉取PHP官方镜像,运行后无法连接数据库,各种扩展没有,在docker的PHP容器内是无法找到php.ini文件的,安装扩展和开启扩展是单独有docker-php-ext-install等一系列docker-php-ext命令有,请自行百度docker安装PHP扩展

你可能感兴趣的:(docker)