记录一下公司PHP项目的容器化
YII框架,有一个队列任务需要运行
目录结构
.
├── README.md
├── docker-compose.yml
├── logs
├── mysql
├── mysql.env
├── nginx
├── php
├── queue
└── web
空文件夹需要放一个空文件才能被git上传到gitlab钟
需要在host中创建用户,对应到容器中的运行用户,还需要修改文件权限
useradd -g 33 -u 33 -s /sbin/nologin www-data
chown -R 33:33 /docker/ ##容器内是根据uid和gid来识别权限,在host中修改所有者也用uid和gid修改,host中的组名和用户名不影响在容器中的权限
docker-compose.yml
services:
nginx:
image: "nginx"
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
ports:
- "6380:80" # 左边可以改成任意没使用的端口
- "63443:443" # 左边可以改成任意没使用的端口
volumes:
- ./web:/var/www/html
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d:/etc/nginx/conf.d
- ./logs/nginx:/var/log/nginx
depends_on:
- php
- db
- redis
networks:
- web
php:
build: php
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
expose:
- "9000" # 不暴露公网,故没有写9000:9000
volumes:
- ./web:/var/www/html
- ./logs/php:/usr/local/var/log
- ./php/php.ini:/usr/local/etc/php/php.ini
- ./php/opcache.ini:/usr/local/etc/php/conf.d/opcache.ini
- ./php/php-fpm.conf:/usr/local/etc/php-fpm.conf
- ./php/www.conf:/usr/local/etc/php-fpm.d/www.conf
depends_on:
- db
- redis
networks:
- web
db:
image: mysql:5.7
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
ports:
- "3306:3306"
volumes:
- ./mysql/data:/var/lib/mysql
- ./logs/mysql:/var/log/mysql
- ./mysql/my.cnf:/etc/my.cnf
env_file:
- mysql.env
depends_on:
- redis
networks:
- web
redis:
image: redis:7.4.1-alpine3.20
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
expose:
- "6379"
networks:
- web
queue:
build: queue
restart: unless-stopped
environment:
- TZ=Asia/Shanghai
volumes:
- ./web:/var/www/html
- ./queue/conf/codex-q1.conf:/etc/supervisor/conf.d/codex-q1.conf
- ./logs/queue:/usr/local/var/log
depends_on:
- db
- redis
networks:
- web
networks:
web:
chmod -R 777 /docker/mysql
chmod -R 755 /docker/mysql/my.cnf
mysql配置文件
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'
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/var/log/mysql/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on
early-plugin-load = ""
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 1
innodb_write_io_threads = 1
[client]
#utf8mb4字符集可以存储emoji表情字符
#default-character-set=utf8
default-character-set=utf8mb4
[mysql]
#default-character-set=utf8
default-character-set=utf8mb4
mysql.env
MYSQL_ROOT_PASSWORD=codex
MYSQL_DATABASE=codex
MYSQL_USER=codex
MYSQL_PASSWORD=codex
nginx.conf
user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 51200;
multi_accept on;
}
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" '
'"$request_time" $upstream_response_time';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 1024m;
gzip on;
gzip_vary on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 4;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png;
include /etc/nginx/conf.d/*.conf;
}
default.conf
server {
listen 80 default_server;
root /var/www/html;
index index.php;
access_log /var/log/nginx/default_server.log main;
location / {
index index.php index.html index.htm;
}
location ~ \.php(.*)$ {
fastcgi_pass php:9000; #在docker内会自动把php解析成php所在pod的IP
fastcgi_index index.php;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
php/Dockerfile
FROM php:7.4.33-fpm
WORKDIR /var/www/html
RUN apt-get update \
&& apt install -y libpng-dev libjpeg-dev zlib1g-dev libzip-dev imagemagick libmagick++-dev libssl-dev libcurl4-openssl-dev \
&& pecl install redis imagick \
&& docker-php-ext-install pdo_mysql gd zip opcache\
&& docker-php-ext-enable redis imagick opcache \
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
queue/Dockerfile
FROM php:7.4.33-fpm
WORKDIR /var/www/html
RUN apt-get update \
&& apt install -y libpng-dev libjpeg-dev zlib1g-dev libzip-dev imagemagick libmagick++-dev libssl-dev libcurl4-openssl-dev supervisor \
&& pecl install redis \
&& docker-php-ext-install pdo_mysql gd zip opcache\
&& docker-php-ext-enable redis opcache \
&& mkdir /var/www/log/ \
&& apt-get autoremove -y && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
CMD ["/usr/bin/supervisord", "--nodaemon"]
参考
https://www.shiguopeng.cn/posts/2021102416/
https://www.shiguopeng.cn/posts/2022030118/
https://www.v2ex.com/t/873113
https://chivincent.net/blog/php-containerization/
https://github.com/goindow/php-nginx