Write the Code. Change the World.

12月 11

docker 的确是个好东西。该早一点就用起来。当下还在熟悉和练习中。

渐渐去丰富和完善。

.env

# 时区
TZ=Asia/Shanghai

# 项目对应文件夹
SOURCE_DIR=./www

# nginx https://nginx.org/
NGINX_NAME=nginx
NGINX_VERSION=1.25.3
NGINX_HTTP_HOST_PORT=80
NGINX_HTTPS_HOST_PORT=443
NGINX_CONFD_DIR=./conf/nginx/conf.d
NGINX_CONF_FILE=./conf/nginx/nginx.conf
NGINX_SSL_CERTIFICATE_DIR=./conf/nginx/ssl
NGINX_LOG_DIR=./logs/nginx

# php8.3
PHP_LOG_DIR=./logs/php8.3

# mysql
MYSQL_DATA=./data/mysql8.2-data
MYSQL_PORT=3306

# redis
REDIS_PORT=6379

docker-compose.yml

version: "3.9"
services:
  nginx:
    image: nginx:${NGINX_VERSION}
    container_name: nginx${NGINX_VERSION}
    restart: always
    privileged: false
    ports:
      - ${NGINX_HTTP_HOST_PORT}:80
      - ${NGINX_HTTPS_HOST_PORT}:443
    volumes:
      - ${NGINX_CONF_FILE}:/etc/nginx/nginx.conf:ro
      - ${NGINX_CONFD_DIR}:/etc/nginx/conf.d/:rw
      - ${NGINX_SSL_CERTIFICATE_DIR}:/etc/nginx/ssl:rw
      - ${LOG_DIR}/nginx${NGINX_VERSION}:/var/log/nginx
      - ${SOURCE_DIR}:/www/:rw
    environment:
      - TZ:${TZ}
    extra_hosts:
      - "holovision.com:172.25.224.1"
    depends_on:
      - php82

  php82:
    build:
      context: ./service/php8.2
      args:
        PHP_IMAGE: php:8.2-fpm
        PHP_EXTENSIONS: redis,gd,exif
    container_name: php8.2
    restart: always
    privileged: true
    volumes:
      - ${SOURCE_DIR}:/www/:rw
      - ${LOG_DIR}/php8.2:/var/log/php
    environment:
      - TZ:${TZ}
    command: bash -c "pecl install redis && docker-php-ext-enable redis && php-fpm"
    depends_on:
      - mysql
      - redis
    cap_add:
      - SYS_PTRACE

  mysql:
    image: mysql:${MYSQL_VERSION}
    container_name: mysql${MYSQL_VERSION}
    restart: always
    privileged: false
    volumes:
      - ${DATA_DIR}/mysql${MYSQL_VERSION}:/var/lib/mysql
    ports:
      - ${MYSQL_PORT}:3306
    environment:
      TZ: ${TZ}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --lower_case_table_names=1

  redis:
    image: redis:${REDIS_VERSION}
    container_name: redis${REDIS_VERSION}
    restart: always
    privileged: false
    volumes:
      - ./conf/redis/redis.conf:/etc/redis/redis.conf
      - ${DATA_DIR}/redis:/data
    ports:
      - ${REDIS_PORT}:6379
    environment:
      - TZ:${TZ}
    command: redis-server /etc/redis/redis.conf

networks:
  default:
    driver: bridge
    ipam:
      driver: default
      # 解除下面的注释可以设置网段,用于nginx等容器固定容器IP
      #config:
      #  - subnet: 10.0.0.0/24

php Dockerfile

ARG PHP_IMAGE
FROM ${PHP_IMAGE}

# 安装扩展
ENV PHP_VERSION php8.2
ARG PHP_EXTENSIONS
# ENV PHP_EXTENSIONS redis,gd,imagick,exif
# COPY ./extensions /tmp/extensions
# WORKDIR /tmp/extensions
# RUN chmod +x install.sh \
#     && sh install.sh \
#     && rm -rf /tmp/extensions

RUN apt-get update \
    # 相关依赖必须手动安装
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    # 安装扩展
    && docker-php-ext-install iconv \
    # 如果安装的扩展需要自定义配置时
    && docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg \
    && docker-php-ext-install gd

RUN docker-php-ext-install pdo pdo_mysql

RUN docker-php-ext-install mysqli

# 安装 composer
RUN curl -o /usr/bin/composer https://mirrors.aliyun.com/composer/composer.phar && chmod +x /usr/bin/composer

WORKDIR /www

nginx

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;
    root   /www/default;
    index  index.php index.html index.htm;

    #access_log  /var/log/nginx/host.access.log  main;
    access_log  /var/log/nginx/default.log  main;
    error_log  /var/log/nginx/default.error.log  warn;

    location ~* ^\/admin\/((?!api\/).) {
        try_files $uri $uri/ /admin/index.html;
    }

    location ~ \.php$ {
        fastcgi_pass php82:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    #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           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #}
}

default.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;

    # hide verson string
    server_tokens  off;

    client_max_body_size 300M;

    include /etc/nginx/conf.d/*.conf;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注